1
1
# Jobs
2
2
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 )
3
8
* [ Unique job] ( #unique-job )
4
9
* [ Sub jobs] ( #sub-jobs )
10
+ * [ Dependent Job] ( #dependent-job )
5
11
12
+ ## Installation
6
13
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
+ ```
10
73
11
74
## Unique job
12
75
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:
14
82
15
83
``` php
16
84
<?php
85
+ namespace App\Queue;
86
+
17
87
use Interop\Queue\PsrMessage;
18
88
use Interop\Queue\PsrProcessor;
19
89
use Interop\Queue\PsrContext;
20
90
use Enqueue\Util\JSON;
21
91
use Enqueue\JobQueue\JobRunner;
22
92
use Enqueue\JobQueue\Job;
93
+ use Enqueue\Client\CommandSubscriberInterface;
23
94
24
- class ReindexProcessor implements PsrProcessor
95
+ class SearchReindexProcessor implements PsrProcessor, CommandSubscriberInterface
25
96
{
26
- /**
27
- * @var JobRunner
28
- */
29
97
private $jobRunner;
98
+
99
+ public function __construct(JobRunner $jobRunner)
100
+ {
101
+ $this->jobRunner = $jobRunner;
102
+ }
30
103
31
104
public function process(PsrMessage $message, PsrContext $context)
32
105
{
@@ -44,12 +117,42 @@ class ReindexProcessor implements PsrProcessor
44
117
45
118
return $result ? self::ACK : self::REJECT;
46
119
}
120
+
121
+ public static function getSubscribedCommand()
122
+ {
123
+ return 'search_reindex';
124
+ }
47
125
}
48
126
```
49
127
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
+
50
153
## Sub jobs
51
154
52
- Run several sub jobs in parallel.
155
+ Run several sub jobs in parallel. The steps are the same as we described above.
53
156
54
157
``` php
55
158
<?php
@@ -133,6 +236,7 @@ class Step2Processor implements PsrProcessor
133
236
134
237
Use dependent job when your job flow has several steps but you want to send new message
135
238
just after all steps are finished.
239
+ The steps are the same as we described above.
136
240
137
241
``` php
138
242
<?php
0 commit comments