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