1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\QueuedJobs\Services; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
6
|
|
|
use SilverStripe\Subsites\State\SubsiteState; |
7
|
|
|
use stdClass; |
8
|
|
|
use SilverStripe\Core\Config\Config; |
9
|
|
|
use SilverStripe\ORM\DataObject; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A base implementation of a queued job that provides some convenience for implementations |
13
|
|
|
* |
14
|
|
|
* This implementation assumes that when you created your job class, you initialised the |
15
|
|
|
* jobData with relevant variables needed to process() your job later on in execution. If you do not, |
16
|
|
|
* please ensure you do before you queueJob() the job, to ensure the signature that is generated is 'correct'. |
17
|
|
|
* |
18
|
|
|
* @author Marcus Nyeholt <[email protected]> |
19
|
|
|
* @license BSD http://silverstripe.org/bsd-license/ |
20
|
|
|
* @skipUpgrade |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractQueuedJob implements QueuedJob |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var stdClass |
26
|
|
|
*/ |
27
|
|
|
protected $jobData; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $messages = array(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
protected $totalSteps = 0; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
protected $currentStep = 0; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var boolean |
46
|
|
|
*/ |
47
|
|
|
protected $isComplete = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Extensions can have a construct but don't have too. |
51
|
|
|
* Without a construct, it's impossible to create a job in the CMS |
52
|
|
|
* @var array params |
53
|
|
|
*/ |
54
|
|
|
public function __construct($params = array()) |
55
|
|
|
{ |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
abstract public function getTitle(); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Sets a data object for persisting by adding its id and type to the serialised vars |
65
|
|
|
* |
66
|
|
|
* @param DataObject $object |
67
|
|
|
* @param string $name A name to give it, if you want to store more than one |
68
|
|
|
*/ |
69
|
|
|
protected function setObject(DataObject $object, $name = 'Object') |
70
|
|
|
{ |
71
|
|
|
$this->{$name . 'ID'} = $object->ID; |
72
|
|
|
$this->{$name . 'Type'} = $object->ClassName; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $name |
77
|
|
|
* @return DataObject|null |
78
|
|
|
*/ |
79
|
|
|
protected function getObject($name = 'Object') |
80
|
|
|
{ |
81
|
|
|
$id = $this->{$name . 'ID'}; |
82
|
|
|
$type = $this->{$name . 'Type'}; |
83
|
|
|
if ($id) { |
84
|
|
|
return DataObject::get_by_id($type, $id); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return a signature for this queued job |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getSignature() |
94
|
|
|
{ |
95
|
|
|
return md5(get_class($this) . serialize($this->jobData)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Generate a somewhat random signature |
100
|
|
|
* |
101
|
|
|
* useful if you're want to make sure something is always added |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
protected function randomSignature() |
106
|
|
|
{ |
107
|
|
|
return md5(get_class($this) . DBDatetime::now()->getTimestamp() . mt_rand(0, 100000)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* By default jobs should just go into the default processing queue |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getJobType() |
116
|
|
|
{ |
117
|
|
|
return QueuedJob::QUEUED; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getRunAsMemberID() |
121
|
|
|
{ |
122
|
|
|
return null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Performs setup tasks the first time this job is run. |
127
|
|
|
* |
128
|
|
|
* This is only executed once for every job. If you want to run something on every job restart, use the |
129
|
|
|
* {@link prepareForRestart} method. |
130
|
|
|
*/ |
131
|
|
|
public function setup() |
132
|
|
|
{ |
133
|
|
|
$this->loadCustomConfig(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Run when an already setup job is being restarted. |
138
|
|
|
*/ |
139
|
|
|
public function prepareForRestart() |
140
|
|
|
{ |
141
|
|
|
$this->loadCustomConfig(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Do some processing yourself! |
146
|
|
|
*/ |
147
|
|
|
abstract public function process(); |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Method for determining whether the job is finished - you may override it if there's |
151
|
|
|
* more to it than just this |
152
|
|
|
*/ |
153
|
|
|
public function jobFinished() |
154
|
|
|
{ |
155
|
|
|
return $this->isComplete; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Called when the job is determined to be 'complete' |
160
|
|
|
*/ |
161
|
|
|
public function afterComplete() |
162
|
|
|
{ |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return stdClass |
167
|
|
|
*/ |
168
|
|
|
public function getJobData() |
169
|
|
|
{ |
170
|
|
|
// okay, we NEED to store the subsite ID if there's one available |
171
|
|
|
if (!$this->SubsiteID && class_exists(SubsiteState::class)) { |
|
|
|
|
172
|
|
|
$this->SubsiteID = SubsiteState::singleton()->getSubsiteId(); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$data = new stdClass(); |
176
|
|
|
$data->totalSteps = $this->totalSteps; |
177
|
|
|
$data->currentStep = $this->currentStep; |
178
|
|
|
$data->isComplete = $this->isComplete; |
179
|
|
|
$data->jobData = $this->jobData; |
180
|
|
|
$data->messages = $this->messages; |
181
|
|
|
|
182
|
|
|
return $data; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param int $totalSteps |
187
|
|
|
* @param int $currentStep |
188
|
|
|
* @param boolean $isComplete |
189
|
|
|
* @param stdClass $jobData |
190
|
|
|
* @param array $messages |
191
|
|
|
*/ |
192
|
|
|
public function setJobData($totalSteps, $currentStep, $isComplete, $jobData, $messages) |
193
|
|
|
{ |
194
|
|
|
$this->totalSteps = $totalSteps; |
195
|
|
|
$this->currentStep = $currentStep; |
196
|
|
|
$this->isComplete = $isComplete; |
197
|
|
|
$this->jobData = $jobData; |
198
|
|
|
$this->messages = $messages; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Gets custom config settings to use when running the job. |
203
|
|
|
* |
204
|
|
|
* @return array|null |
205
|
|
|
*/ |
206
|
|
|
public function getCustomConfig() |
207
|
|
|
{ |
208
|
|
|
return $this->CustomConfig; |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Sets custom config settings to use when the job is run. |
213
|
|
|
* |
214
|
|
|
* @param array $config |
215
|
|
|
*/ |
216
|
|
|
public function setCustomConfig(array $config) |
217
|
|
|
{ |
218
|
|
|
$this->CustomConfig = $config; |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Sets custom configuration settings from the job data. |
223
|
|
|
*/ |
224
|
|
|
private function loadCustomConfig() |
225
|
|
|
{ |
226
|
|
|
$custom = $this->getCustomConfig(); |
227
|
|
|
|
228
|
|
|
if (!is_array($custom)) { |
229
|
|
|
return; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
foreach ($custom as $class => $settings) { |
233
|
|
|
foreach ($settings as $setting => $value) { |
234
|
|
|
Config::modify()->set($class, $setting, $value); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param string $message |
241
|
|
|
* @param string $severity |
242
|
|
|
*/ |
243
|
|
|
public function addMessage($message, $severity = 'INFO') |
244
|
|
|
{ |
245
|
|
|
$severity = strtoupper($severity); |
246
|
|
|
$this->messages[] = '[' . DBDatetime::now()->Rfc2822() . "][$severity] $message"; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Convenience methods for setting and getting job data |
251
|
|
|
* |
252
|
|
|
* @param mixed $name |
253
|
|
|
* @param mixed $value |
254
|
|
|
*/ |
255
|
|
|
public function __set($name, $value) |
256
|
|
|
{ |
257
|
|
|
if (!$this->jobData) { |
258
|
|
|
$this->jobData = new stdClass(); |
259
|
|
|
} |
260
|
|
|
$this->jobData->$name = $value; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Retrieve some job data |
265
|
|
|
* |
266
|
|
|
* @param mixed $name |
267
|
|
|
* @return mixed |
268
|
|
|
*/ |
269
|
|
|
public function __get($name) |
270
|
|
|
{ |
271
|
|
|
return isset($this->jobData->$name) ? $this->jobData->$name : null; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.