Skip to content

Commit c8b4b0d

Browse files
committed
[job-queue] Update doc.
1 parent 7bfa280 commit c8b4b0d

File tree

1 file changed

+113
-9
lines changed

1 file changed

+113
-9
lines changed

docs/bundle/job_queue.md

+113-9
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,105 @@
11
# Jobs
22

3+
Use jobs when your message flow has several steps(tasks) which run one after another.
4+
Also jobs guaranty that job is unique i.e. you cant start new job with same name
5+
until previous job has finished.
6+
7+
* [Installation](#installation)
38
* [Unique job](#unique-job)
49
* [Sub jobs](#sub-jobs)
10+
* [Dependent Job](#dependent-job)
511

12+
## Installation
613

7-
Use jobs when your message flow has several steps(tasks) which run one after another.
8-
Also jobs guaranty that job is unique i.e. you cant start new job with same name
9-
until previous job has finished.
14+
The easiest way to install Enqueue's job queues is to by requiring a `enqueue/job-queue-pack` pack.
15+
It installs installs everything you need. It also configures everything for you If you are on Symfony Flex.
16+
17+
```bash
18+
$ composer require enqueue/job-queue-pack=^0.8
19+
```
20+
21+
_**Note:** As long as you are on Symfony Flex you are done. If not, keep reading the installation chapter._
22+
23+
* Register installed bundles
24+
25+
```php
26+
<?php
27+
// app/AppKernel.php
28+
29+
use Symfony\Component\HttpKernel\Kernel;
30+
31+
class AppKernel extends Kernel
32+
{
33+
public function registerBundles()
34+
{
35+
$bundles = [
36+
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
37+
new Enqueue\Bundle\EnqueueBundle(),
38+
];
39+
40+
return $bundles;
41+
}
42+
}
43+
````
44+
45+
* Configure installed bundles:
46+
47+
```yaml
48+
# app/config/config.yml
49+
50+
enqueue:
51+
# plus basic bundle configuration
52+
53+
job: true
54+
55+
doctrine:
56+
# plus basic bundle configuration
57+
58+
orm:
59+
mappings:
60+
EnqueueJobQueue:
61+
is_bundle: false
62+
type: xml
63+
dir: '%kernel.project_dir%/vendor/enqueue/job-queue/Doctrine/mapping'
64+
prefix: 'Enqueue\JobQueue\Doctrine\Entity'
65+
66+
```
67+
68+
* Run doctrine schema update command
69+
70+
```bash
71+
$ bin/console doctrine:schema:update
72+
```
1073

1174
## Unique job
1275

13-
Guaranty that there is only single job running with such name.
76+
Guarantee that there is only one job with such name running at a time.
77+
For example you have a task that builds a search index.
78+
It takes quite a lot of time and you dont want another instance of same task working at the same time.
79+
Here's how to do it:
80+
81+
* Write a job processor class:
1482

1583
```php
1684
<?php
85+
namespace App\Queue;
86+
1787
use Interop\Queue\PsrMessage;
1888
use Interop\Queue\PsrProcessor;
1989
use Interop\Queue\PsrContext;
2090
use Enqueue\Util\JSON;
2191
use Enqueue\JobQueue\JobRunner;
2292
use Enqueue\JobQueue\Job;
93+
use Enqueue\Client\CommandSubscriberInterface;
2394

24-
class ReindexProcessor implements PsrProcessor
95+
class SearchReindexProcessor implements PsrProcessor, CommandSubscriberInterface
2596
{
26-
/**
27-
* @var JobRunner
28-
*/
2997
private $jobRunner;
98+
99+
public function __construct(JobRunner $jobRunner)
100+
{
101+
$this->jobRunner = $jobRunner;
102+
}
30103

31104
public function process(PsrMessage $message, PsrContext $context)
32105
{
@@ -44,12 +117,42 @@ class ReindexProcessor implements PsrProcessor
44117

45118
return $result ? self::ACK : self::REJECT;
46119
}
120+
121+
public static function getSubscribedCommand()
122+
{
123+
return 'search_reindex';
124+
}
47125
}
48126
```
49127

128+
* Register it
129+
130+
```yaml
131+
services:
132+
app_queue_search_reindex_processor:
133+
class: 'App\Queue\SearchReindexProcessor'
134+
arguments: ['@Enqueue\JobQueue\JobRunner']
135+
tags:
136+
- { name: 'enqueue.client.processor' }
137+
```
138+
139+
* Schedule command
140+
141+
```php
142+
<?php
143+
use Symfony\Component\DependencyInjection\ContainerInterface;
144+
use Enqueue\Client\ProducerInterface;
145+
146+
/** @var ContainerInterface $container */
147+
148+
$producer = $container->get(ProducerInterface::class);
149+
150+
$producer->sendCommand('search_reindex');
151+
```
152+
50153
## Sub jobs
51154

52-
Run several sub jobs in parallel.
155+
Run several sub jobs in parallel. The steps are the same as we described above.
53156

54157
```php
55158
<?php
@@ -133,6 +236,7 @@ class Step2Processor implements PsrProcessor
133236

134237
Use dependent job when your job flow has several steps but you want to send new message
135238
just after all steps are finished.
239+
The steps are the same as we described above.
136240

137241
```php
138242
<?php

0 commit comments

Comments
 (0)