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