1 | <?php |
||
10 | class JobManager extends AbstractJobManager |
||
11 | { |
||
12 | const DEFAULT_RESERVE_TIMEOUT = 5; // seconds |
||
13 | |||
14 | /** @var Pheanstalk */ |
||
15 | protected $beanstalkd; |
||
16 | |||
17 | protected $tube; |
||
18 | |||
19 | protected $reserveTimeout = self::DEFAULT_RESERVE_TIMEOUT; |
||
20 | |||
21 | 1 | public function setBeanstalkd(Pheanstalk $beanstalkd) |
|
25 | |||
26 | public function setTube($tube) |
||
30 | |||
31 | public function setReserveTimeout($timeout) |
||
35 | |||
36 | 4 | public function save(\Dtc\QueueBundle\Model\Job $job) |
|
37 | { |
||
38 | /** @var Job $job */ |
||
39 | 4 | $message = $job->toMessage(); |
|
40 | 4 | $arguments = [$message, $job->getPriority(), $job->getDelay(), $job->getTtr()]; |
|
41 | 4 | $method = 'put'; |
|
42 | 4 | if ($this->tube) { |
|
43 | array_unshift($arguments, $this->tube); |
||
44 | $method .= 'InTube'; |
||
45 | } |
||
46 | 4 | $jobId = call_user_func_array([$this->beanstalkd, $method], $arguments); |
|
47 | 4 | $job->setId($jobId); |
|
48 | |||
49 | // Ideally we should get this from beanstalk, but to save the roundtrip time, we do this here |
||
50 | 4 | $job->setBeanJob($this->getBeanJob($jobId, $message)); |
|
51 | |||
52 | 4 | return $job; |
|
53 | } |
||
54 | |||
55 | 4 | public function getBeanJob($jobId, $data) |
|
59 | |||
60 | /** |
||
61 | * @param string|null $workerName |
||
62 | * @param string|null $methodName |
||
63 | * @param bool $prioritize |
||
64 | * @param int|string|null $runId |
||
65 | * |
||
66 | * @return Job|null |
||
67 | * |
||
68 | * @throws UnsupportedException |
||
69 | */ |
||
70 | 5 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
71 | { |
||
72 | 5 | if (null !== $methodName) { |
|
73 | throw new UnsupportedException('Unsupported'); |
||
74 | } |
||
75 | 5 | if (null !== $workerName) { |
|
76 | 1 | throw new UnsupportedException('Unsupported'); |
|
77 | } |
||
78 | |||
79 | 4 | $beanstalkd = $this->beanstalkd; |
|
80 | 4 | if ($this->tube) { |
|
81 | $beanstalkd = $this->beanstalkd->watch($this->tube); |
||
82 | } |
||
83 | |||
84 | do { |
||
85 | 4 | $expiredJob = false; |
|
86 | 4 | $job = $this->findJob($beanstalkd, $expiredJob, $runId); |
|
87 | 4 | } while ($expiredJob); |
|
88 | |||
89 | 4 | return $job; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param Pheanstalk $beanstalkd |
||
94 | * @param bool $expiredJob |
||
95 | * @param int|string|null $runId |
||
96 | * |
||
97 | * @return Job|null |
||
98 | */ |
||
99 | 4 | protected function findJob(Pheanstalk $beanstalkd, &$expiredJob, $runId) |
|
121 | |||
122 | 2 | public function deleteJob(\Dtc\QueueBundle\Model\Job $job) |
|
127 | |||
128 | // Save History get called upon completion of the job |
||
129 | public function saveHistory(\Dtc\QueueBundle\Model\Job $job) |
||
130 | { |
||
131 | if (BaseJob::STATUS_SUCCESS === $job->getStatus()) { |
||
132 | $this->beanstalkd |
||
133 | ->delete($job); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | 1 | public function getStats() |
|
141 | } |
||
142 |