1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Model; |
4
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\Manager\JobManagerInterface; |
6
|
|
|
use Dtc\QueueBundle\Util\Util; |
7
|
|
|
|
8
|
|
|
abstract class BaseJob |
9
|
|
|
{ |
10
|
|
|
const STATUS_SUCCESS = 'success'; |
11
|
|
|
const STATUS_EXCEPTION = 'exception'; |
12
|
|
|
const STATUS_NEW = 'new'; |
13
|
|
|
const STATUS_RUNNING = 'running'; |
14
|
|
|
const STATUS_FAILURE = 'failure'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var JobManagerInterface |
18
|
|
|
*/ |
19
|
|
|
protected $jobManager; |
20
|
|
|
protected $worker; |
21
|
|
|
protected $workerName; |
22
|
|
|
protected $className; |
23
|
|
|
protected $args; |
24
|
|
|
protected $batch; |
25
|
|
|
protected $method; |
26
|
|
|
protected $priority; |
27
|
|
|
protected $whenAt; |
28
|
|
|
protected $status; |
29
|
|
|
protected $createdAt; |
30
|
|
|
|
31
|
107 |
|
public function __construct(Worker $worker = null, $batch = false, $priority = 10, \DateTime $whenAt = null) |
32
|
|
|
{ |
33
|
107 |
|
if ($worker) { |
34
|
93 |
|
$this->setWorker($worker); |
35
|
93 |
|
if ($jobManager = $worker->getJobManager()) { |
36
|
90 |
|
$this->setJobManager($jobManager); |
37
|
|
|
} |
38
|
93 |
|
$this->setClassName(get_class($worker)); |
39
|
93 |
|
$this->setWorkerName($worker->getName()); |
40
|
|
|
} |
41
|
|
|
|
42
|
107 |
|
if ($whenAt) { |
43
|
23 |
|
$this->setWhenAt($whenAt); |
44
|
|
|
} |
45
|
107 |
|
$this->setBatch($batch ? true : false); |
46
|
107 |
|
$this->setPriority($priority); |
47
|
107 |
|
$this->setStatus(self::STATUS_NEW); |
48
|
107 |
|
$dateTime = Util::getMicrotimeDateTime(); |
49
|
107 |
|
$this->setCreatedAt($dateTime); |
50
|
107 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $status The status of the job |
54
|
|
|
*/ |
55
|
107 |
|
public function setStatus($status) |
56
|
|
|
{ |
57
|
107 |
|
$this->status = $status; |
58
|
|
|
|
59
|
107 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string The status of the job |
64
|
|
|
*/ |
65
|
55 |
|
public function getStatus() |
66
|
|
|
{ |
67
|
55 |
|
return $this->status; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return \DateTime|null |
72
|
|
|
*/ |
73
|
44 |
|
public function getWhenAt() |
74
|
|
|
{ |
75
|
44 |
|
return $this->whenAt; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param \DateTime $whenAt |
80
|
|
|
*/ |
81
|
56 |
|
public function setWhenAt(\DateTime $whenAt) |
82
|
|
|
{ |
83
|
56 |
|
$this->whenAt = $whenAt; |
84
|
|
|
|
85
|
56 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return Worker |
90
|
|
|
*/ |
91
|
40 |
|
public function getWorker() |
92
|
|
|
{ |
93
|
40 |
|
return $this->worker; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param Worker $worker |
98
|
|
|
*/ |
99
|
100 |
|
public function setWorker($worker) |
100
|
|
|
{ |
101
|
100 |
|
$this->worker = $worker; |
102
|
|
|
|
103
|
100 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
72 |
|
public function getBatch() |
110
|
|
|
{ |
111
|
72 |
|
return $this->batch; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param bool $batch |
116
|
|
|
*/ |
117
|
107 |
|
public function setBatch($batch) |
118
|
|
|
{ |
119
|
107 |
|
$this->batch = $batch; |
120
|
|
|
|
121
|
107 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
96 |
|
public function getArgs() |
128
|
|
|
{ |
129
|
96 |
|
return $this->args; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param $args |
134
|
|
|
*/ |
135
|
104 |
|
public function setArgs($args) |
136
|
|
|
{ |
137
|
104 |
|
if (!$this->validateArgs($args)) { |
138
|
5 |
|
throw new \InvalidArgumentException('Args must not contain object'); |
139
|
|
|
} |
140
|
|
|
|
141
|
104 |
|
$this->args = $args; |
142
|
|
|
|
143
|
104 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
104 |
|
protected function validateArgs($args) |
147
|
|
|
{ |
148
|
104 |
|
if (is_array($args)) { |
149
|
74 |
|
foreach ($args as $key => $value) { |
150
|
74 |
|
if (!$this->validateArgs($value)) { |
151
|
74 |
|
return false; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
74 |
|
return true; |
156
|
|
|
} else { |
157
|
104 |
|
return !is_object($args); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
92 |
|
public function getMethod() |
165
|
|
|
{ |
166
|
92 |
|
return $this->method; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $method |
171
|
|
|
*/ |
172
|
65 |
|
public function setMethod($method) |
173
|
|
|
{ |
174
|
65 |
|
$this->method = $method; |
175
|
|
|
|
176
|
65 |
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return int |
181
|
|
|
*/ |
182
|
90 |
|
public function getPriority() |
183
|
|
|
{ |
184
|
90 |
|
return $this->priority; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param int $priority |
189
|
|
|
*/ |
190
|
107 |
|
public function setPriority($priority) |
191
|
|
|
{ |
192
|
107 |
|
$this->priority = $priority; |
193
|
|
|
|
194
|
107 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param string $workerName |
199
|
|
|
*/ |
200
|
100 |
|
public function setWorkerName($workerName) |
201
|
|
|
{ |
202
|
100 |
|
$this->workerName = $workerName; |
203
|
|
|
|
204
|
100 |
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param string $className |
209
|
|
|
*/ |
210
|
100 |
|
public function setClassName($className) |
211
|
|
|
{ |
212
|
100 |
|
$this->className = $className; |
213
|
|
|
|
214
|
100 |
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
98 |
|
public function getWorkerName() |
221
|
|
|
{ |
222
|
98 |
|
return $this->workerName; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
44 |
|
public function getClassName() |
229
|
|
|
{ |
230
|
44 |
|
return $this->className; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return JobManagerInterface |
235
|
|
|
*/ |
236
|
33 |
|
public function getJobManager() |
237
|
|
|
{ |
238
|
33 |
|
return $this->jobManager; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return \DateTime |
243
|
|
|
*/ |
244
|
91 |
|
public function getCreatedAt() |
245
|
|
|
{ |
246
|
91 |
|
return $this->createdAt; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param \DateTime $createdAt |
251
|
|
|
*/ |
252
|
107 |
|
public function setCreatedAt(\DateTime $createdAt) |
253
|
|
|
{ |
254
|
107 |
|
$this->createdAt = $createdAt; |
255
|
|
|
|
256
|
107 |
|
return $this; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|