|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Beanstalkd; |
|
4
|
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\Model\Job as BaseJob; |
|
6
|
|
|
use Dtc\QueueBundle\Model\JobManagerInterface; |
|
7
|
|
|
use Pheanstalk\Pheanstalk; |
|
8
|
|
|
|
|
9
|
|
|
class JobManager implements JobManagerInterface |
|
10
|
|
|
{ |
|
11
|
|
|
const DEFAULT_RESERVE_TIMEOUT = 5; // seconds |
|
12
|
|
|
|
|
13
|
|
|
/** @var Pheanstalk */ |
|
14
|
|
|
protected $beanstalkd; |
|
15
|
|
|
|
|
16
|
|
|
protected $tube; |
|
17
|
|
|
|
|
18
|
|
|
protected $reserveTimeout = self::DEFAULT_RESERVE_TIMEOUT; |
|
19
|
|
|
|
|
20
|
|
|
public function setBeanstalkd(Pheanstalk $beanstalkd) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->beanstalkd = $beanstalkd; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function setTube($tube) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->tube = $tube; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function setReserveTimeout($timeout) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->reserveTimeout = $timeout; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function save(\Dtc\QueueBundle\Model\Job $job) |
|
36
|
|
|
{ |
|
37
|
|
|
/** @var Job $job */ |
|
38
|
|
|
$arguments = [$job->toMessage(), $job->getPriority(), $job->getDelay(), $job->getTtr()]; |
|
39
|
|
|
$method = 'put'; |
|
40
|
|
|
if ($this->tube) { |
|
41
|
|
|
array_unshift($arguments, $this->tube); |
|
42
|
|
|
$method .= 'InTube'; |
|
43
|
|
|
} |
|
44
|
|
|
var_dump(get_class($this->beanstalkd)); |
|
|
|
|
|
|
45
|
|
|
var_dump($method); |
|
46
|
|
|
$jobId = call_user_func_array([$this->beanstalkd, $method], $arguments); |
|
47
|
|
|
$job->setId($jobId); |
|
48
|
|
|
|
|
49
|
|
|
return $job; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getJob($workerName = null, $methodName = null, $prioritize = true) |
|
53
|
|
|
{ |
|
54
|
|
|
if ($methodName) { |
|
55
|
|
|
throw new \Exception('Unsupported'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$beanJob = $this->beanstalkd; |
|
59
|
|
|
if ($this->tube) { |
|
60
|
|
|
$beanJob = $beanJob->watch($this->tube); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$beanJob = $beanJob->reserve($this->reserveTimeout); |
|
64
|
|
|
if ($beanJob) { |
|
65
|
|
|
$job = new Job(); |
|
66
|
|
|
$job->fromMessage($beanJob->getData()); |
|
67
|
|
|
$job->setId($beanJob->getId()); |
|
68
|
|
|
|
|
69
|
|
|
return $job; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function deleteJob(\Dtc\QueueBundle\Model\Job $job) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->beanstalkd |
|
76
|
|
|
->delete($job); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Save History get called upon completion of the job |
|
80
|
|
|
public function saveHistory(\Dtc\QueueBundle\Model\Job $job) |
|
81
|
|
|
{ |
|
82
|
|
|
if ($job->getStatus() === BaseJob::STATUS_SUCCESS) { |
|
83
|
|
|
$this->beanstalkd |
|
84
|
|
|
->delete($job); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// @Todo Need a strategy for buried jobs, if any? |
|
|
|
|
|
|
88
|
|
|
// else { |
|
89
|
|
|
// $this->beanstalkd |
|
90
|
|
|
// ->bury($job); |
|
91
|
|
|
// } |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
View Code Duplication |
public function getJobCount($workerName = null, $methodName = null) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
if ($methodName) { |
|
97
|
|
|
throw new \Exception('Unsupported'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($workerName) { |
|
101
|
|
|
throw new \Exception('Unsupported'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// @Todo - use statistics |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function getStats() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->beanstalkd->stats(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function resetErroneousJobs($workerName = null, $methodName = null) |
|
113
|
|
|
{ |
|
114
|
|
|
throw new \Exception('Unsupported'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function pruneErroneousJobs($workerName = null, $methodName = null) |
|
118
|
|
|
{ |
|
119
|
|
|
throw new \Exception('Unsupported'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getStatus() |
|
123
|
|
|
{ |
|
124
|
|
|
throw new \Exception('Unsupported'); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|