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