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