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