1 | <?php |
||
26 | abstract class AbstractQueue extends Component implements QueueInterface |
||
27 | { |
||
28 | /** |
||
29 | * Event executed before a job is posted to the queue. |
||
30 | */ |
||
31 | const EVENT_BEFORE_POST = 'beforePost'; |
||
32 | |||
33 | /** |
||
34 | * Event executed before a job is posted to the queue. |
||
35 | */ |
||
36 | const EVENT_AFTER_POST = 'afterPost'; |
||
37 | |||
38 | /** |
||
39 | * Event executed before a job is being fetched from the queue. |
||
40 | */ |
||
41 | const EVENT_BEFORE_FETCH = 'beforeFetch'; |
||
42 | |||
43 | /** |
||
44 | * Event executed after a job is being fetched from the queue. |
||
45 | */ |
||
46 | const EVENT_AFTER_FETCH = 'afterFetch'; |
||
47 | |||
48 | /** |
||
49 | * Event executed before a job is being deleted from the queue. |
||
50 | */ |
||
51 | const EVENT_BEFORE_DELETE = 'beforeDelete'; |
||
52 | |||
53 | /** |
||
54 | * Event executed after a job is being deleted from the queue. |
||
55 | */ |
||
56 | const EVENT_AFTER_DELETE = 'afterDelete'; |
||
57 | |||
58 | /** |
||
59 | * Event executed before a job is being released from the queue. |
||
60 | */ |
||
61 | const EVENT_BEFORE_RELEASE = 'beforeRelease'; |
||
62 | |||
63 | /** |
||
64 | * Event executed after a job is being released from the queue. |
||
65 | */ |
||
66 | const EVENT_AFTER_RELEASE = 'afterRelease'; |
||
67 | |||
68 | /** |
||
69 | * Event executed before a job is being executed. |
||
70 | */ |
||
71 | const EVENT_BEFORE_RUN = 'beforeRun'; |
||
72 | |||
73 | /** |
||
74 | * Event executed after a job is being executed. |
||
75 | */ |
||
76 | const EVENT_AFTER_RUN = 'afterRun'; |
||
77 | |||
78 | /** |
||
79 | * This will release automatically on execution failure. i.e. when |
||
80 | * the `run` method returns false or catch exception. |
||
81 | * |
||
82 | * @var bool |
||
83 | */ |
||
84 | public $releaseOnFailure = true; |
||
85 | |||
86 | /** |
||
87 | * Post new job to the queue. Override this for queue implementation. |
||
88 | * |
||
89 | * @param JobInterface $job |
||
90 | * @param array $options |
||
91 | * @return bool |
||
92 | */ |
||
93 | abstract protected function postJob(JobInterface $job, array $options = []): bool; |
||
94 | |||
95 | /** |
||
96 | * @inheritdoc |
||
97 | */ |
||
98 | public function post(JobInterface $job, array $options = []): bool |
||
120 | |||
121 | /** |
||
122 | * @inheritdoc |
||
123 | */ |
||
124 | public function fetch() |
||
139 | |||
140 | /** |
||
141 | * Return next job from the queue. Override this for queue implementation. |
||
142 | * |
||
143 | * @return JobInterface|bool |
||
144 | */ |
||
145 | abstract protected function fetchJob(); |
||
146 | |||
147 | /** |
||
148 | * @inheritdoc |
||
149 | */ |
||
150 | public function run(JobInterface $job): bool |
||
200 | |||
201 | /** |
||
202 | * @inheritdoc |
||
203 | */ |
||
204 | public function delete(JobInterface $job): bool |
||
225 | |||
226 | /** |
||
227 | * Delete the job. Override this for the queue implementation. |
||
228 | * |
||
229 | * @param JobInterface $job |
||
230 | * @return bool |
||
231 | */ |
||
232 | abstract protected function deleteJob(JobInterface $job): bool; |
||
233 | |||
234 | /** |
||
235 | * @inheritdoc |
||
236 | */ |
||
237 | public function release(JobInterface $job): bool |
||
258 | |||
259 | /** |
||
260 | * Release the job. Override this for the queue implementation. |
||
261 | * |
||
262 | * @param JobInterface $job |
||
263 | * @return bool |
||
264 | */ |
||
265 | abstract protected function releaseJob(JobInterface $job): bool; |
||
266 | |||
267 | /** |
||
268 | * @param string $message |
||
269 | * @return JobInterface |
||
270 | * @throws Exception |
||
271 | */ |
||
272 | protected function deserialize($message): JobInterface |
||
279 | |||
280 | /** |
||
281 | * @param JobInterface $job The job to serialize. |
||
282 | * @return string JSON string. |
||
283 | */ |
||
284 | protected function serialize(JobInterface $job) |
||
288 | } |
||
289 |