|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Symbiote\QueuedJobs\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use SilverStripe\Control\Controller; |
|
7
|
|
|
use SilverStripe\Control\Director; |
|
8
|
|
|
use SilverStripe\Control\Email\Email; |
|
9
|
|
|
use SilverStripe\Control\Session; |
|
10
|
|
|
use SilverStripe\Core\Config\Config; |
|
11
|
|
|
use SilverStripe\Core\Config\Configurable; |
|
12
|
|
|
use SilverStripe\Core\Convert; |
|
13
|
|
|
use SilverStripe\Core\Extensible; |
|
14
|
|
|
use SilverStripe\Core\Injector\Injectable; |
|
15
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
16
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
17
|
|
|
use SilverStripe\ORM\DataList; |
|
18
|
|
|
use SilverStripe\ORM\DataObject; |
|
19
|
|
|
use SilverStripe\ORM\DB; |
|
20
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
|
21
|
|
|
use SilverStripe\Security\Member; |
|
22
|
|
|
use SilverStripe\Security\Permission; |
|
23
|
|
|
use SilverStripe\Security\Security; |
|
24
|
|
|
use Psr\Log\LoggerInterface; |
|
25
|
|
|
use SilverStripe\Subsites\Model\Subsite; |
|
26
|
|
|
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor; |
|
27
|
|
|
use Symbiote\QueuedJobs\QJUtils; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* A service that can be used for starting, stopping and listing queued jobs. |
|
31
|
|
|
* |
|
32
|
|
|
* When a job is first added, it is initialised, its job type determined, then persisted to the database |
|
33
|
|
|
* |
|
34
|
|
|
* When the queues are scanned, a job is reloaded and processed. Ignoring the persistence and reloading, it looks |
|
35
|
|
|
* something like |
|
36
|
|
|
* |
|
37
|
|
|
* job->getJobType(); |
|
38
|
|
|
* job->getJobData(); |
|
39
|
|
|
* data->write(); |
|
40
|
|
|
* job->setup(); |
|
41
|
|
|
* while !job->isComplete |
|
42
|
|
|
* job->process(); |
|
43
|
|
|
* job->getJobData(); |
|
44
|
|
|
* data->write(); |
|
45
|
|
|
* |
|
46
|
|
|
* |
|
47
|
|
|
* @author Marcus Nyeholt <[email protected]> |
|
48
|
|
|
* @license BSD http://silverstripe.org/bsd-license/ |
|
49
|
|
|
*/ |
|
50
|
|
|
class QueuedJobService |
|
51
|
|
|
{ |
|
52
|
|
|
use Configurable; |
|
53
|
|
|
use Injectable; |
|
54
|
|
|
use Extensible; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @config |
|
58
|
|
|
* @var int |
|
59
|
|
|
*/ |
|
60
|
|
|
private static $stall_threshold = 3; |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* How much ram will we allow before pausing and releasing the memory? |
|
64
|
|
|
* |
|
65
|
|
|
* For instance, set to 268435456 (256MB) to pause this process if used memory exceeds |
|
66
|
|
|
* this value. This needs to be set to a value lower than the php_ini max_memory as |
|
67
|
|
|
* the system will otherwise crash before shutdown can be handled gracefully. |
|
68
|
|
|
* |
|
69
|
|
|
* This was increased to 256MB for SilverStripe 4.x as framework uses more memory than 3.x |
|
70
|
|
|
* |
|
71
|
|
|
* @var int |
|
72
|
|
|
* @config |
|
73
|
|
|
*/ |
|
74
|
|
|
private static $memory_limit = 268435456; |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Optional time limit (in seconds) to run the service before restarting to release resources. |
|
78
|
|
|
* |
|
79
|
|
|
* Defaults to no limit. |
|
80
|
|
|
* |
|
81
|
|
|
* @var int |
|
82
|
|
|
* @config |
|
83
|
|
|
*/ |
|
84
|
|
|
private static $time_limit = 0; |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Timestamp (in seconds) when the queue was started |
|
88
|
|
|
* |
|
89
|
|
|
* @var int |
|
90
|
|
|
*/ |
|
91
|
|
|
protected $startedAt = 0; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Should "immediate" jobs be managed using the shutdown function? |
|
95
|
|
|
* |
|
96
|
|
|
* It is recommended you set up an inotify watch and use that for |
|
97
|
|
|
* triggering immediate jobs. See the wiki for more information |
|
98
|
|
|
* |
|
99
|
|
|
* @var boolean |
|
100
|
|
|
* @config |
|
101
|
|
|
*/ |
|
102
|
|
|
private static $use_shutdown_function = true; |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* The location for immediate jobs to be stored in |
|
106
|
|
|
* |
|
107
|
|
|
* @var string |
|
108
|
|
|
* @config |
|
109
|
|
|
*/ |
|
110
|
|
|
private static $cache_dir = 'queuedjobs'; |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @var DefaultQueueHandler |
|
114
|
|
|
*/ |
|
115
|
|
|
public $queueHandler; |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* |
|
119
|
|
|
* @var TaskRunnerEngine |
|
120
|
|
|
*/ |
|
121
|
|
|
public $queueRunner; |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Config controlled list of default/required jobs |
|
125
|
|
|
* @var array |
|
126
|
|
|
*/ |
|
127
|
|
|
public $defaultJobs = []; |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Register our shutdown handler |
|
131
|
|
|
*/ |
|
132
|
|
|
public function __construct() |
|
133
|
|
|
{ |
|
134
|
|
|
// bind a shutdown function to process all 'immediate' queued jobs if needed, but only in CLI mode |
|
135
|
|
|
if (static::config()->get('use_shutdown_function') && Director::is_cli()) { |
|
136
|
|
|
register_shutdown_function(array($this, 'onShutdown')); |
|
137
|
|
|
} |
|
138
|
|
|
if (Config::inst()->get(Email::class, 'queued_job_admin_email') == '') { |
|
139
|
|
|
Config::modify()->set( |
|
140
|
|
|
Email::class, |
|
141
|
|
|
'queued_job_admin_email', |
|
142
|
|
|
Config::inst()->get(Email::class, 'admin_email') |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Adds a job to the queue to be started |
|
149
|
|
|
* |
|
150
|
|
|
* Relevant data about the job will be persisted using a QueuedJobDescriptor |
|
151
|
|
|
* |
|
152
|
|
|
* @param QueuedJob $job |
|
153
|
|
|
* The job to start. |
|
154
|
|
|
* @param $startAfter |
|
155
|
|
|
* The date (in Y-m-d H:i:s format) to start execution after |
|
156
|
|
|
* @param int $userId |
|
157
|
|
|
* The ID of a user to execute the job as. Defaults to the current user |
|
158
|
|
|
* @return int |
|
159
|
|
|
*/ |
|
160
|
|
|
public function queueJob(QueuedJob $job, $startAfter = null, $userId = null, $queueName = null) |
|
161
|
|
|
{ |
|
162
|
|
|
$signature = $job->getSignature(); |
|
163
|
|
|
|
|
164
|
|
|
// see if we already have this job in a queue |
|
165
|
|
|
$filter = array( |
|
166
|
|
|
'Signature' => $signature, |
|
167
|
|
|
'JobStatus' => array( |
|
168
|
|
|
QueuedJob::STATUS_NEW, |
|
169
|
|
|
QueuedJob::STATUS_INIT, |
|
170
|
|
|
) |
|
171
|
|
|
); |
|
172
|
|
|
|
|
173
|
|
|
$existing = DataList::create(QueuedJobDescriptor::class) |
|
174
|
|
|
->filter($filter) |
|
175
|
|
|
->first(); |
|
176
|
|
|
|
|
177
|
|
|
if ($existing && $existing->ID) { |
|
178
|
|
|
return $existing->ID; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$jobDescriptor = new QueuedJobDescriptor(); |
|
182
|
|
|
$jobDescriptor->JobTitle = $job->getTitle(); |
|
183
|
|
|
$jobDescriptor->JobType = $queueName ? $queueName : $job->getJobType(); |
|
184
|
|
|
$jobDescriptor->Signature = $signature; |
|
185
|
|
|
$jobDescriptor->Implementation = get_class($job); |
|
186
|
|
|
$jobDescriptor->StartAfter = $startAfter; |
|
187
|
|
|
|
|
188
|
|
|
if ($userId === null) { |
|
189
|
|
|
$userId = (Security::getCurrentUser() ? Security::getCurrentUser()->ID : null); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$jobDescriptor->RunAsID = $userId; |
|
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
// copy data |
|
195
|
|
|
$this->copyJobToDescriptor($job, $jobDescriptor); |
|
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
$jobDescriptor->write(); |
|
198
|
|
|
|
|
199
|
|
|
$this->startJob($jobDescriptor, $startAfter); |
|
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
return $jobDescriptor->ID; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Start a job (or however the queue handler determines it should be started) |
|
206
|
|
|
* |
|
207
|
|
|
* @param JobDescriptor $jobDescriptor |
|
208
|
|
|
* @param date $startAfter |
|
209
|
|
|
*/ |
|
210
|
|
|
public function startJob($jobDescriptor, $startAfter = null) |
|
211
|
|
|
{ |
|
212
|
|
|
if ($startAfter && strtotime($startAfter) > time()) { |
|
213
|
|
|
$this->queueHandler->scheduleJob($jobDescriptor, $startAfter); |
|
214
|
|
|
} else { |
|
215
|
|
|
// immediately start it on the queue, however that works |
|
216
|
|
|
$this->queueHandler->startJobOnQueue($jobDescriptor); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Copies data from a job into a descriptor for persisting |
|
222
|
|
|
* |
|
223
|
|
|
* @param QueuedJob $job |
|
224
|
|
|
* @param JobDescriptor $jobDescriptor |
|
225
|
|
|
*/ |
|
226
|
|
|
protected function copyJobToDescriptor($job, $jobDescriptor) |
|
227
|
|
|
{ |
|
228
|
|
|
$data = $job->getJobData(); |
|
229
|
|
|
|
|
230
|
|
|
$jobDescriptor->TotalSteps = $data->totalSteps; |
|
231
|
|
|
$jobDescriptor->StepsProcessed = $data->currentStep; |
|
232
|
|
|
if ($data->isComplete) { |
|
233
|
|
|
$jobDescriptor->JobStatus = QueuedJob::STATUS_COMPLETE; |
|
234
|
|
|
$jobDescriptor->JobFinished = date('Y-m-d H:i:s'); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
$jobDescriptor->SavedJobData = serialize($data->jobData); |
|
238
|
|
|
$jobDescriptor->SavedJobMessages = serialize($data->messages); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @param QueuedJobDescriptor $jobDescriptor |
|
243
|
|
|
* @param QueuedJob $job |
|
244
|
|
|
*/ |
|
245
|
|
|
protected function copyDescriptorToJob($jobDescriptor, $job) |
|
246
|
|
|
{ |
|
247
|
|
|
$jobData = null; |
|
|
|
|
|
|
248
|
|
|
$messages = null; |
|
|
|
|
|
|
249
|
|
|
|
|
250
|
|
|
// switching to php's serialize methods... not sure why this wasn't done from the start! |
|
251
|
|
|
$jobData = @unserialize($jobDescriptor->SavedJobData); |
|
252
|
|
|
$messages = @unserialize($jobDescriptor->SavedJobMessages); |
|
253
|
|
|
|
|
254
|
|
|
if (!$jobData) { |
|
255
|
|
|
// SS's convert:: function doesn't do this detection for us!! |
|
256
|
|
|
if (function_exists('json_decode')) { |
|
257
|
|
|
$jobData = json_decode($jobDescriptor->SavedJobData); |
|
258
|
|
|
$messages = json_decode($jobDescriptor->SavedJobMessages); |
|
259
|
|
|
} else { |
|
260
|
|
|
$jobData = Convert::json2obj($jobDescriptor->SavedJobData); |
|
261
|
|
|
$messages = Convert::json2obj($jobDescriptor->SavedJobMessages); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
$job->setJobData( |
|
266
|
|
|
$jobDescriptor->TotalSteps, |
|
267
|
|
|
$jobDescriptor->StepsProcessed, |
|
268
|
|
|
$jobDescriptor->JobStatus == QueuedJob::STATUS_COMPLETE, |
|
269
|
|
|
$jobData, |
|
270
|
|
|
$messages |
|
271
|
|
|
); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Check the current job queues and see if any of the jobs currently in there should be started. If so, |
|
276
|
|
|
* return the next job that should be executed |
|
277
|
|
|
* |
|
278
|
|
|
* @param string $type Job type |
|
279
|
|
|
* @return QueuedJobDescriptor |
|
280
|
|
|
*/ |
|
281
|
|
|
public function getNextPendingJob($type = null) |
|
282
|
|
|
{ |
|
283
|
|
|
// Filter jobs by type |
|
284
|
|
|
$type = $type ?: QueuedJob::QUEUED; |
|
285
|
|
|
$list = QueuedJobDescriptor::get() |
|
286
|
|
|
->filter('JobType', $type) |
|
287
|
|
|
->sort('ID', 'ASC'); |
|
288
|
|
|
|
|
289
|
|
|
// see if there's any blocked jobs that need to be resumed |
|
290
|
|
|
$waitingJob = $list |
|
291
|
|
|
->filter('JobStatus', QueuedJob::STATUS_WAIT) |
|
292
|
|
|
->first(); |
|
293
|
|
|
if ($waitingJob) { |
|
294
|
|
|
return $waitingJob; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
// If there's an existing job either running or pending, the lets just return false to indicate |
|
298
|
|
|
// that we're still executing |
|
299
|
|
|
$runningJob = $list |
|
|
|
|
|
|
300
|
|
|
->filter('JobStatus', array(QueuedJob::STATUS_INIT, QueuedJob::STATUS_RUN)) |
|
301
|
|
|
->first(); |
|
302
|
|
|
if ($runningJob) { |
|
303
|
|
|
return false; |
|
|
|
|
|
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
// Otherwise, lets find any 'new' jobs that are waiting to execute |
|
307
|
|
|
$newJob = $list |
|
308
|
|
|
->filter('JobStatus', QueuedJob::STATUS_NEW) |
|
309
|
|
|
->where(sprintf( |
|
310
|
|
|
'"StartAfter" < \'%s\' OR "StartAfter" IS NULL', |
|
311
|
|
|
DBDatetime::now()->getValue() |
|
312
|
|
|
)) |
|
313
|
|
|
->first(); |
|
314
|
|
|
|
|
315
|
|
|
return $newJob; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Runs an explicit check on all currently running jobs to make sure their "processed" count is incrementing |
|
320
|
|
|
* between each run. If it's not, then we need to flag it as paused due to an error. |
|
321
|
|
|
* |
|
322
|
|
|
* This typically happens when a PHP fatal error is thrown, which can't be picked up by the error |
|
323
|
|
|
* handler or exception checker; in this case, we detect these stalled jobs later and fix (try) to |
|
324
|
|
|
* fix them |
|
325
|
|
|
* |
|
326
|
|
|
* @param int $queue The queue to check against |
|
327
|
|
|
*/ |
|
328
|
|
|
public function checkJobHealth($queue = null) |
|
329
|
|
|
{ |
|
330
|
|
|
$queue = $queue ?: QueuedJob::QUEUED; |
|
331
|
|
|
// Select all jobs currently marked as running |
|
332
|
|
|
$runningJobs = QueuedJobDescriptor::get() |
|
333
|
|
|
->filter(array( |
|
334
|
|
|
'JobStatus' => array( |
|
335
|
|
|
QueuedJob::STATUS_RUN, |
|
336
|
|
|
QueuedJob::STATUS_INIT, |
|
337
|
|
|
), |
|
338
|
|
|
'JobType' => $queue, |
|
339
|
|
|
)); |
|
340
|
|
|
|
|
341
|
|
|
// If no steps have been processed since the last run, consider it a broken job |
|
342
|
|
|
// Only check jobs that have been viewed before. LastProcessedCount defaults to -1 on new jobs. |
|
343
|
|
|
$stalledJobs = $runningJobs |
|
344
|
|
|
->filter('LastProcessedCount:GreaterThanOrEqual', 0) |
|
345
|
|
|
->where('"StepsProcessed" = "LastProcessedCount"'); |
|
346
|
|
|
foreach ($stalledJobs as $stalledJob) { |
|
347
|
|
|
$this->restartStalledJob($stalledJob); |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
// now, find those that need to be marked before the next check |
|
351
|
|
|
// foreach job, mark it as having been incremented |
|
352
|
|
|
foreach ($runningJobs as $job) { |
|
353
|
|
|
$job->LastProcessedCount = $job->StepsProcessed; |
|
354
|
|
|
$job->write(); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
// finally, find the list of broken jobs and send an email if there's some found |
|
358
|
|
|
$brokenJobs = QueuedJobDescriptor::get()->filter('JobStatus', QueuedJob::STATUS_BROKEN); |
|
359
|
|
View Code Duplication |
if ($brokenJobs && $brokenJobs->count()) { |
|
|
|
|
|
|
360
|
|
|
$this->getLogger()->error( |
|
361
|
|
|
print_r( |
|
362
|
|
|
array( |
|
363
|
|
|
'errno' => 0, |
|
364
|
|
|
'errstr' => 'Broken jobs were found in the job queue', |
|
365
|
|
|
'errfile' => __FILE__, |
|
366
|
|
|
'errline' => __LINE__, |
|
367
|
|
|
'errcontext' => array() |
|
368
|
|
|
), |
|
369
|
|
|
true |
|
370
|
|
|
) |
|
371
|
|
|
); |
|
372
|
|
|
} |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* Checks through ll the scheduled jobs that are expected to exist |
|
377
|
|
|
*/ |
|
378
|
|
|
public function checkDefaultJobs($queue = null) |
|
379
|
|
|
{ |
|
380
|
|
|
$queue = $queue ?: QueuedJob::QUEUED; |
|
|
|
|
|
|
381
|
|
|
if (count($this->defaultJobs)) { |
|
382
|
|
|
$activeJobs = QueuedJobDescriptor::get()->filter( |
|
383
|
|
|
'JobStatus', |
|
384
|
|
|
array( |
|
385
|
|
|
QueuedJob::STATUS_NEW, |
|
386
|
|
|
QueuedJob::STATUS_INIT, |
|
387
|
|
|
QueuedJob::STATUS_RUN, |
|
388
|
|
|
QueuedJob::STATUS_WAIT, |
|
389
|
|
|
QueuedJob::STATUS_PAUSED, |
|
390
|
|
|
) |
|
391
|
|
|
); |
|
392
|
|
|
foreach ($this->defaultJobs as $title => $jobConfig) { |
|
393
|
|
|
if (!isset($jobConfig['filter']) || !isset($jobConfig['type'])) { |
|
394
|
|
|
$this->getLogger()->error("Default Job config: $title incorrectly set up. Please check the readme for examples"); |
|
395
|
|
|
continue; |
|
396
|
|
|
} |
|
397
|
|
|
$job = $activeJobs->filter(array_merge( |
|
398
|
|
|
array('Implementation' => $jobConfig['type']), |
|
399
|
|
|
$jobConfig['filter'] |
|
400
|
|
|
)); |
|
401
|
|
|
if (!$job->count()) { |
|
402
|
|
|
$this->getLogger()->error("Default Job config: $title was missing from Queue"); |
|
403
|
|
|
Email::create() |
|
404
|
|
|
->setTo(isset($jobConfig['email']) ? $jobConfig['email'] : Config::inst()->get('Email', 'queued_job_admin_email')) |
|
405
|
|
|
->setFrom(Config::inst()->get('Email', 'queued_job_admin_email')) |
|
406
|
|
|
->setSubject('Default Job "' . $title . '" missing') |
|
407
|
|
|
->setData($jobConfig) |
|
408
|
|
|
->addData('Title', $title) |
|
409
|
|
|
->addData('Site', Director::absoluteBaseURL()) |
|
|
|
|
|
|
410
|
|
|
->setHTMLTemplate('QueuedJobsDefaultJob') |
|
411
|
|
|
->send(); |
|
412
|
|
|
if (isset($jobConfig['recreate']) && $jobConfig['recreate']) { |
|
413
|
|
|
if (!array_key_exists('construct', $jobConfig) || !isset($jobConfig['startDateFormat']) || !isset($jobConfig['startTimeString'])) { |
|
414
|
|
|
$this->getLogger()->error("Default Job config: $title incorrectly set up. Please check the readme for examples"); |
|
415
|
|
|
continue; |
|
416
|
|
|
} |
|
417
|
|
|
singleton('Symbiote\\QueuedJobs\\Services\\QueuedJobService')->queueJob( |
|
418
|
|
|
Injector::inst()->createWithArgs($jobConfig['type'], $jobConfig['construct']), |
|
419
|
|
|
date($jobConfig['startDateFormat'], strtotime($jobConfig['startTimeString'])) |
|
420
|
|
|
); |
|
421
|
|
|
$this->getLogger()->error("Default Job config: $title has been re-added to the Queue"); |
|
422
|
|
|
} |
|
423
|
|
|
} |
|
424
|
|
|
} |
|
425
|
|
|
} |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
/** |
|
429
|
|
|
* Attempt to restart a stalled job |
|
430
|
|
|
* |
|
431
|
|
|
* @param QueuedJobDescriptor $stalledJob |
|
432
|
|
|
* @return bool True if the job was successfully restarted |
|
433
|
|
|
*/ |
|
434
|
|
|
protected function restartStalledJob($stalledJob) |
|
435
|
|
|
{ |
|
436
|
|
|
if ($stalledJob->ResumeCounts < static::config()->get('stall_threshold')) { |
|
437
|
|
|
$stalledJob->restart(); |
|
438
|
|
|
$message = _t( |
|
439
|
|
|
__CLASS__ . '.STALLED_JOB_RESTART_MSG', |
|
440
|
|
|
'A job named {name} appears to have stalled. It will be stopped and restarted, please login to make sure it has continued', |
|
441
|
|
|
['name' => $stalledJob->JobTitle] |
|
442
|
|
|
); |
|
443
|
|
|
} else { |
|
444
|
|
|
$stalledJob->pause(); |
|
445
|
|
|
$message = _t( |
|
446
|
|
|
__CLASS__ . '.STALLED_JOB_MSG', |
|
447
|
|
|
'A job named {name} appears to have stalled. It has been paused, please login to check it', |
|
448
|
|
|
['name' => $stalledJob->JobTitle] |
|
449
|
|
|
); |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
$this->getLogger()->error($message); |
|
453
|
|
|
$from = Config::inst()->get(Email::class, 'admin_email'); |
|
454
|
|
|
$to = Config::inst()->get(Email::class, 'queued_job_admin_email'); |
|
455
|
|
|
$subject = _t(__CLASS__ . '.STALLED_JOB', 'Stalled job'); |
|
456
|
|
|
$mail = new Email($from, $to, $subject, $message); |
|
457
|
|
|
$mail->send(); |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
/** |
|
461
|
|
|
* Prepares the given jobDescriptor for execution. Returns the job that |
|
462
|
|
|
* will actually be run in a state ready for executing. |
|
463
|
|
|
* |
|
464
|
|
|
* Note that this is called each time a job is picked up to be executed from the cron |
|
465
|
|
|
* job - meaning that jobs that are paused and restarted will have 'setup()' called on them again, |
|
466
|
|
|
* so your job MUST detect that and act accordingly. |
|
467
|
|
|
* |
|
468
|
|
|
* @param QueuedJobDescriptor $jobDescriptor |
|
469
|
|
|
* The Job descriptor of a job to prepare for execution |
|
470
|
|
|
* |
|
471
|
|
|
* @return QueuedJob|boolean |
|
472
|
|
|
*/ |
|
473
|
|
|
protected function initialiseJob(QueuedJobDescriptor $jobDescriptor) |
|
474
|
|
|
{ |
|
475
|
|
|
// create the job class |
|
476
|
|
|
$impl = $jobDescriptor->Implementation; |
|
477
|
|
|
$job = Injector::inst()->create($impl); |
|
478
|
|
|
/* @var $job QueuedJob */ |
|
479
|
|
|
if (!$job) { |
|
480
|
|
|
throw new Exception("Implementation $impl no longer exists"); |
|
481
|
|
|
} |
|
482
|
|
|
|
|
483
|
|
|
$jobDescriptor->JobStatus = QueuedJob::STATUS_INIT; |
|
484
|
|
|
$jobDescriptor->write(); |
|
485
|
|
|
|
|
486
|
|
|
// make sure the data is there |
|
487
|
|
|
$this->copyDescriptorToJob($jobDescriptor, $job); |
|
488
|
|
|
|
|
489
|
|
|
// see if it needs 'setup' or 'restart' called |
|
490
|
|
|
if ($jobDescriptor->StepsProcessed <= 0) { |
|
491
|
|
|
$job->setup(); |
|
492
|
|
|
} else { |
|
493
|
|
|
$job->prepareForRestart(); |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
|
|
// make sure the descriptor is up to date with anything changed |
|
497
|
|
|
$this->copyJobToDescriptor($job, $jobDescriptor); |
|
|
|
|
|
|
498
|
|
|
$jobDescriptor->write(); |
|
499
|
|
|
|
|
500
|
|
|
return $job; |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
/** |
|
504
|
|
|
* Given a {@link QueuedJobDescriptor} mark the job as initialised. Works sort of like a mutex. |
|
505
|
|
|
* Currently a database lock isn't entirely achievable, due to database adapters not supporting locks. |
|
506
|
|
|
* This may still have a race condition, but this should minimise the possibility. |
|
507
|
|
|
* Side effect is the job status will be changed to "Initialised". |
|
508
|
|
|
* |
|
509
|
|
|
* Assumption is the job has a status of "Queued" or "Wait". |
|
510
|
|
|
* |
|
511
|
|
|
* @param QueuedJobDescriptor $jobDescriptor |
|
512
|
|
|
* @return boolean |
|
513
|
|
|
*/ |
|
514
|
|
|
protected function grabMutex(QueuedJobDescriptor $jobDescriptor) |
|
515
|
|
|
{ |
|
516
|
|
|
// write the status and determine if any rows were affected, for protection against a |
|
517
|
|
|
// potential race condition where two or more processes init the same job at once. |
|
518
|
|
|
// This deliberately does not use write() as that would always update LastEdited |
|
519
|
|
|
// and thus the row would always be affected. |
|
520
|
|
|
try { |
|
521
|
|
|
DB::query(sprintf( |
|
522
|
|
|
'UPDATE "QueuedJobDescriptor" SET "JobStatus" = \'%s\' WHERE "ID" = %s', |
|
523
|
|
|
QueuedJob::STATUS_INIT, |
|
524
|
|
|
$jobDescriptor->ID |
|
525
|
|
|
)); |
|
526
|
|
|
} catch (Exception $e) { |
|
527
|
|
|
return false; |
|
528
|
|
|
} |
|
529
|
|
|
|
|
530
|
|
|
if (DB::getConn()->affectedRows() === 0 && $jobDescriptor->JobStatus !== QueuedJob::STATUS_INIT) { |
|
|
|
|
|
|
531
|
|
|
return false; |
|
532
|
|
|
} |
|
533
|
|
|
|
|
534
|
|
|
return true; |
|
535
|
|
|
} |
|
536
|
|
|
|
|
537
|
|
|
/** |
|
538
|
|
|
* Start the actual execution of a job. |
|
539
|
|
|
* The assumption is the jobID refers to a {@link QueuedJobDescriptor} that is status set as "Queued". |
|
540
|
|
|
* |
|
541
|
|
|
* This method will continue executing until the job says it's completed |
|
542
|
|
|
* |
|
543
|
|
|
* @param int $jobId |
|
544
|
|
|
* The ID of the job to start executing |
|
545
|
|
|
* @return boolean |
|
546
|
|
|
*/ |
|
547
|
|
|
public function runJob($jobId) |
|
|
|
|
|
|
548
|
|
|
{ |
|
549
|
|
|
// first retrieve the descriptor |
|
550
|
|
|
$jobDescriptor = DataObject::get_by_id( |
|
551
|
|
|
QueuedJobDescriptor::class, |
|
552
|
|
|
(int) $jobId |
|
553
|
|
|
); |
|
554
|
|
|
if (!$jobDescriptor) { |
|
555
|
|
|
throw new Exception("$jobId is invalid"); |
|
556
|
|
|
} |
|
557
|
|
|
|
|
558
|
|
|
// now lets see whether we have a current user to run as. Typically, if the job is executing via the CLI, |
|
559
|
|
|
// we want it to actually execute as the RunAs user - however, if running via the web (which is rare...), we |
|
560
|
|
|
// want to ensure that the current user has admin privileges before switching. Otherwise, we just run it |
|
561
|
|
|
// as the currently logged in user and hope for the best |
|
562
|
|
|
|
|
563
|
|
|
// We need to use $_SESSION directly because SS ties the session to a controller that no longer exists at |
|
564
|
|
|
// this point of execution in some circumstances |
|
565
|
|
|
$originalUserID = isset($_SESSION['loggedInAs']) ? $_SESSION['loggedInAs'] : 0; |
|
566
|
|
|
$originalUser = $originalUserID |
|
567
|
|
|
? DataObject::get_by_id(Member::class, $originalUserID) |
|
568
|
|
|
: null; |
|
569
|
|
|
$runAsUser = null; |
|
570
|
|
|
|
|
571
|
|
|
// If the Job has requested that we run it as a particular user, then we should try and do that. |
|
572
|
|
|
if ($jobDescriptor->RunAs() !== null) { |
|
573
|
|
|
$runAsUser = $this->setRunAsUser($jobDescriptor->RunAs(), $originalUser); |
|
|
|
|
|
|
574
|
|
|
} |
|
575
|
|
|
|
|
576
|
|
|
// set up a custom error handler for this processing |
|
577
|
|
|
$errorHandler = new JobErrorHandler(); |
|
578
|
|
|
|
|
579
|
|
|
$job = null; |
|
580
|
|
|
|
|
581
|
|
|
$broken = false; |
|
582
|
|
|
|
|
583
|
|
|
// Push a config context onto the stack for the duration of this job run. |
|
584
|
|
|
Config::nest(); |
|
585
|
|
|
|
|
586
|
|
|
if ($this->grabMutex($jobDescriptor)) { |
|
|
|
|
|
|
587
|
|
|
try { |
|
588
|
|
|
$job = $this->initialiseJob($jobDescriptor); |
|
|
|
|
|
|
589
|
|
|
|
|
590
|
|
|
// get the job ready to begin. |
|
591
|
|
|
if (!$jobDescriptor->JobStarted) { |
|
592
|
|
|
$jobDescriptor->JobStarted = date('Y-m-d H:i:s'); |
|
593
|
|
|
} else { |
|
594
|
|
|
$jobDescriptor->JobRestarted = date('Y-m-d H:i:s'); |
|
595
|
|
|
} |
|
596
|
|
|
|
|
597
|
|
|
// Only write to job as "Running" if 'isComplete' was NOT set to true |
|
598
|
|
|
// during setup() or prepareForRestart() |
|
|
|
|
|
|
599
|
|
|
if (!$job->jobFinished()) { |
|
600
|
|
|
$jobDescriptor->JobStatus = QueuedJob::STATUS_RUN; |
|
601
|
|
|
$jobDescriptor->write(); |
|
602
|
|
|
} |
|
603
|
|
|
|
|
604
|
|
|
$lastStepProcessed = 0; |
|
605
|
|
|
// have we stalled at all? |
|
606
|
|
|
$stallCount = 0; |
|
607
|
|
|
|
|
608
|
|
|
if ($job->SubsiteID && class_exists(Subsite::class)) { |
|
609
|
|
|
Subsite::changeSubsite($job->SubsiteID); |
|
610
|
|
|
|
|
611
|
|
|
// lets set the base URL as far as Director is concerned so that our URLs are correct |
|
612
|
|
|
$subsite = DataObject::get_by_id(Subsite::class, $job->SubsiteID); |
|
613
|
|
|
if ($subsite && $subsite->exists()) { |
|
614
|
|
|
$domain = $subsite->domain(); |
|
615
|
|
|
$base = rtrim(Director::protocol() . $domain, '/') . '/'; |
|
616
|
|
|
|
|
617
|
|
|
Config::modify()->set(Director::class, 'alternate_base_url', $base); |
|
618
|
|
|
} |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
// while not finished |
|
622
|
|
|
while (!$job->jobFinished() && !$broken) { |
|
623
|
|
|
// see that we haven't been set to 'paused' or otherwise by another process |
|
624
|
|
|
$jobDescriptor = DataObject::get_by_id( |
|
625
|
|
|
QueuedJobDescriptor::class, |
|
626
|
|
|
(int) $jobId |
|
627
|
|
|
); |
|
628
|
|
|
if (!$jobDescriptor || !$jobDescriptor->exists()) { |
|
629
|
|
|
$broken = true; |
|
630
|
|
|
$this->getLogger()->error( |
|
631
|
|
|
print_r( |
|
632
|
|
|
array( |
|
633
|
|
|
'errno' => 0, |
|
634
|
|
|
'errstr' => 'Job descriptor ' . $jobId . ' could not be found', |
|
635
|
|
|
'errfile' => __FILE__, |
|
636
|
|
|
'errline' => __LINE__, |
|
637
|
|
|
'errcontext' => array() |
|
638
|
|
|
), |
|
639
|
|
|
true |
|
640
|
|
|
) |
|
641
|
|
|
); |
|
642
|
|
|
break; |
|
643
|
|
|
} |
|
644
|
|
|
if ($jobDescriptor->JobStatus != QueuedJob::STATUS_RUN) { |
|
645
|
|
|
// we've been paused by something, so we'll just exit |
|
646
|
|
|
$job->addMessage( |
|
647
|
|
|
_t(__CLASS__ . '.JOB_PAUSED', 'Job paused at {time}', ['time' => date('Y-m-d H:i:s')]) |
|
648
|
|
|
); |
|
649
|
|
|
$broken = true; |
|
650
|
|
|
} |
|
651
|
|
|
|
|
652
|
|
|
if (!$broken) { |
|
653
|
|
|
try { |
|
654
|
|
|
$job->process(); |
|
655
|
|
|
} catch (Exception $e) { |
|
656
|
|
|
// okay, we'll just catch this exception for now |
|
657
|
|
|
$job->addMessage( |
|
658
|
|
|
_t( |
|
659
|
|
|
__CLASS__ . '.JOB_EXCEPT', |
|
660
|
|
|
'Job caused exception {message} in {file} at line {line}', |
|
661
|
|
|
[ |
|
662
|
|
|
'message' => $e->getMessage(), |
|
663
|
|
|
'file' => $e->getFile(), |
|
664
|
|
|
'line' => $e->getLine(), |
|
665
|
|
|
] |
|
666
|
|
|
), |
|
667
|
|
|
'ERROR' |
|
668
|
|
|
); |
|
669
|
|
|
$this->getLogger()->error($e->getMessage()); |
|
670
|
|
|
$jobDescriptor->JobStatus = QueuedJob::STATUS_BROKEN; |
|
671
|
|
|
$this->extend('updateJobDescriptorAndJobOnException', $jobDescriptor, $job, $e); |
|
672
|
|
|
} |
|
673
|
|
|
|
|
674
|
|
|
// now check the job state |
|
675
|
|
|
$data = $job->getJobData(); |
|
676
|
|
|
if ($data->currentStep == $lastStepProcessed) { |
|
677
|
|
|
$stallCount++; |
|
678
|
|
|
} |
|
679
|
|
|
|
|
680
|
|
|
if ($stallCount > static::config()->get('stall_threshold')) { |
|
681
|
|
|
$broken = true; |
|
682
|
|
|
$job->addMessage( |
|
683
|
|
|
_t( |
|
684
|
|
|
__CLASS__ . '.JOB_STALLED', |
|
685
|
|
|
'Job stalled after {attempts} attempts - please check', |
|
686
|
|
|
['attempts' => $stallCount] |
|
687
|
|
|
), |
|
688
|
|
|
'ERROR' |
|
689
|
|
|
); |
|
690
|
|
|
$jobDescriptor->JobStatus = QueuedJob::STATUS_BROKEN; |
|
691
|
|
|
} |
|
692
|
|
|
|
|
693
|
|
|
// now we'll be good and check our memory usage. If it is too high, we'll set the job to |
|
694
|
|
|
// a 'Waiting' state, and let the next processing run pick up the job. |
|
695
|
|
|
if ($this->isMemoryTooHigh()) { |
|
696
|
|
|
$job->addMessage( |
|
697
|
|
|
_t( |
|
698
|
|
|
__CLASS__ . '.MEMORY_RELEASE', |
|
699
|
|
|
'Job releasing memory and waiting ({used} used)', |
|
700
|
|
|
['used' => $this->humanReadable($this->getMemoryUsage())] |
|
701
|
|
|
) |
|
702
|
|
|
); |
|
703
|
|
|
if ($jobDescriptor->JobStatus != QueuedJob::STATUS_BROKEN) { |
|
704
|
|
|
$jobDescriptor->JobStatus = QueuedJob::STATUS_WAIT; |
|
705
|
|
|
} |
|
706
|
|
|
$broken = true; |
|
707
|
|
|
} |
|
708
|
|
|
|
|
709
|
|
|
// Also check if we are running too long |
|
710
|
|
|
if ($this->hasPassedTimeLimit()) { |
|
711
|
|
|
$job->addMessage(_t( |
|
712
|
|
|
__CLASS__ . '.TIME_LIMIT', |
|
713
|
|
|
'Queue has passed time limit and will restart before continuing' |
|
714
|
|
|
)); |
|
715
|
|
|
if ($jobDescriptor->JobStatus != QueuedJob::STATUS_BROKEN) { |
|
716
|
|
|
$jobDescriptor->JobStatus = QueuedJob::STATUS_WAIT; |
|
717
|
|
|
} |
|
718
|
|
|
$broken = true; |
|
719
|
|
|
} |
|
720
|
|
|
} |
|
721
|
|
|
|
|
722
|
|
|
if ($jobDescriptor) { |
|
723
|
|
|
$this->copyJobToDescriptor($job, $jobDescriptor); |
|
724
|
|
|
$jobDescriptor->write(); |
|
725
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
726
|
|
|
$this->getLogger()->error( |
|
727
|
|
|
print_r( |
|
728
|
|
|
array( |
|
729
|
|
|
'errno' => 0, |
|
730
|
|
|
'errstr' => 'Job descriptor has been set to null', |
|
731
|
|
|
'errfile' => __FILE__, |
|
732
|
|
|
'errline' => __LINE__, |
|
733
|
|
|
'errcontext' => array() |
|
734
|
|
|
), |
|
735
|
|
|
true |
|
736
|
|
|
) |
|
737
|
|
|
); |
|
738
|
|
|
$broken = true; |
|
739
|
|
|
} |
|
740
|
|
|
} |
|
741
|
|
|
|
|
742
|
|
|
// a last final save. The job is complete by now |
|
743
|
|
|
if ($jobDescriptor) { |
|
744
|
|
|
$jobDescriptor->write(); |
|
745
|
|
|
} |
|
746
|
|
|
|
|
747
|
|
|
if ($job->jobFinished()) { |
|
748
|
|
|
$job->afterComplete(); |
|
749
|
|
|
$jobDescriptor->cleanupJob(); |
|
750
|
|
|
} |
|
751
|
|
|
} catch (Exception $e) { |
|
752
|
|
|
// okay, we'll just catch this exception for now |
|
753
|
|
|
$this->getLogger()->error($e->getMessage()); |
|
754
|
|
|
$jobDescriptor->JobStatus = QueuedJob::STATUS_BROKEN; |
|
755
|
|
|
$this->extend('updateJobDescriptorAndJobOnException', $jobDescriptor, $job, $e); |
|
756
|
|
|
$jobDescriptor->write(); |
|
757
|
|
|
$broken = true; |
|
758
|
|
|
} |
|
759
|
|
|
} |
|
760
|
|
|
|
|
761
|
|
|
$errorHandler->clear(); |
|
762
|
|
|
|
|
763
|
|
|
Config::unnest(); |
|
764
|
|
|
|
|
765
|
|
|
$this->unsetRunAsUser($runAsUser, $originalUser); |
|
|
|
|
|
|
766
|
|
|
|
|
767
|
|
|
return !$broken; |
|
768
|
|
|
} |
|
769
|
|
|
|
|
770
|
|
|
/** |
|
771
|
|
|
* @param Member $runAsUser |
|
772
|
|
|
* @param Member|null $originalUser |
|
773
|
|
|
* @return null|Member |
|
774
|
|
|
*/ |
|
775
|
|
|
protected function setRunAsUser(Member $runAsUser, Member $originalUser = null) |
|
|
|
|
|
|
776
|
|
|
{ |
|
777
|
|
|
// Sanity check. Can't set the user if they don't exist. |
|
778
|
|
|
if ($runAsUser === null || !$runAsUser->exists()) { |
|
779
|
|
|
return null; |
|
780
|
|
|
} |
|
781
|
|
|
|
|
782
|
|
|
// Don't need to set Security user if we're already logged in as that same user. |
|
783
|
|
|
if ($originalUser && $originalUser->ID === $runAsUser->ID) { |
|
784
|
|
|
return null; |
|
785
|
|
|
} |
|
786
|
|
|
|
|
787
|
|
|
// We are currently either not logged in at all, or we're logged in as a different user. We should switch users |
|
788
|
|
|
// so that the context within the Job is correct. |
|
789
|
|
|
if (Controller::has_curr()) { |
|
790
|
|
|
Security::setCurrentUser($runAsUser); |
|
791
|
|
|
} else { |
|
792
|
|
|
$_SESSION['loggedInAs'] = $runAsUser->ID; |
|
793
|
|
|
} |
|
794
|
|
|
|
|
795
|
|
|
// This is an explicit coupling brought about by SS not having a nice way of mocking a user, as it requires |
|
796
|
|
|
// session nastiness |
|
797
|
|
|
if (class_exists('SecurityContext')) { |
|
798
|
|
|
singleton('SecurityContext')->setMember($runAsUser); |
|
799
|
|
|
} |
|
800
|
|
|
|
|
801
|
|
|
return $runAsUser; |
|
802
|
|
|
} |
|
803
|
|
|
|
|
804
|
|
|
/** |
|
805
|
|
|
* @param Member|null $runAsUser |
|
806
|
|
|
* @param Member|null $originalUser |
|
807
|
|
|
*/ |
|
808
|
|
|
protected function unsetRunAsUser(Member $runAsUser = null, Member $originalUser = null) |
|
|
|
|
|
|
809
|
|
|
{ |
|
810
|
|
|
// No runAsUser was set, so we don't need to do anything. |
|
811
|
|
|
if ($runAsUser === null) { |
|
812
|
|
|
return; |
|
813
|
|
|
} |
|
814
|
|
|
|
|
815
|
|
|
// There was no originalUser, so we should make sure that we set the user back to null. |
|
816
|
|
|
if (!$originalUser) { |
|
817
|
|
|
if (Controller::has_curr()) { |
|
818
|
|
|
Security::setCurrentUser(null); |
|
819
|
|
|
} else { |
|
820
|
|
|
$_SESSION['loggedInAs'] = null; |
|
821
|
|
|
} |
|
822
|
|
|
} |
|
823
|
|
|
|
|
824
|
|
|
// Okay let's reset our user. |
|
825
|
|
|
if (Controller::has_curr()) { |
|
826
|
|
|
Security::setCurrentUser($originalUser); |
|
827
|
|
|
} else { |
|
828
|
|
|
$_SESSION['loggedInAs'] = $originalUser->ID; |
|
829
|
|
|
} |
|
830
|
|
|
} |
|
831
|
|
|
|
|
832
|
|
|
/** |
|
833
|
|
|
* Start timer |
|
834
|
|
|
*/ |
|
835
|
|
|
protected function markStarted() |
|
836
|
|
|
{ |
|
837
|
|
|
if ($this->startedAt) { |
|
838
|
|
|
$this->startedAt = DBDatetime::now()->Format('U'); |
|
|
|
|
|
|
839
|
|
|
} |
|
840
|
|
|
} |
|
841
|
|
|
|
|
842
|
|
|
/** |
|
843
|
|
|
* Is execution time too long? |
|
844
|
|
|
* |
|
845
|
|
|
* @return bool True if the script has passed the configured time_limit |
|
846
|
|
|
*/ |
|
847
|
|
|
protected function hasPassedTimeLimit() |
|
848
|
|
|
{ |
|
849
|
|
|
// Ensure a limit exists |
|
850
|
|
|
$limit = static::config()->get('time_limit'); |
|
851
|
|
|
if (!$limit) { |
|
852
|
|
|
return false; |
|
853
|
|
|
} |
|
854
|
|
|
|
|
855
|
|
|
// Ensure started date is set |
|
856
|
|
|
$this->markStarted(); |
|
857
|
|
|
|
|
858
|
|
|
// Check duration |
|
859
|
|
|
$now = DBDatetime::now()->Format('U'); |
|
860
|
|
|
return $now > $this->startedAt + $limit; |
|
861
|
|
|
} |
|
862
|
|
|
|
|
863
|
|
|
/** |
|
864
|
|
|
* Is memory usage too high? |
|
865
|
|
|
* |
|
866
|
|
|
* @return bool |
|
867
|
|
|
*/ |
|
868
|
|
|
protected function isMemoryTooHigh() |
|
869
|
|
|
{ |
|
870
|
|
|
$used = $this->getMemoryUsage(); |
|
871
|
|
|
$limit = $this->getMemoryLimit(); |
|
872
|
|
|
return $limit && ($used > $limit); |
|
873
|
|
|
} |
|
874
|
|
|
|
|
875
|
|
|
/** |
|
876
|
|
|
* Get peak memory usage of this application |
|
877
|
|
|
* |
|
878
|
|
|
* @return float |
|
879
|
|
|
*/ |
|
880
|
|
|
protected function getMemoryUsage() |
|
881
|
|
|
{ |
|
882
|
|
|
// Note we use real_usage = false |
|
883
|
|
|
// http://stackoverflow.com/questions/15745385/memory-get-peak-usage-with-real-usage |
|
884
|
|
|
// Also we use the safer peak memory usage |
|
885
|
|
|
return (float)memory_get_peak_usage(false); |
|
886
|
|
|
} |
|
887
|
|
|
|
|
888
|
|
|
/** |
|
889
|
|
|
* Determines the memory limit (in bytes) for this application |
|
890
|
|
|
* Limits to the smaller of memory_limit configured via php.ini or silverstripe config |
|
891
|
|
|
* |
|
892
|
|
|
* @return float Memory limit in bytes |
|
893
|
|
|
*/ |
|
894
|
|
|
protected function getMemoryLimit() |
|
895
|
|
|
{ |
|
896
|
|
|
// Limit to smaller of explicit limit or php memory limit |
|
897
|
|
|
$limit = $this->parseMemory(static::config()->get('memory_limit')); |
|
898
|
|
|
if ($limit) { |
|
899
|
|
|
return $limit; |
|
900
|
|
|
} |
|
901
|
|
|
|
|
902
|
|
|
// Fallback to php memory limit |
|
903
|
|
|
$phpLimit = $this->getPHPMemoryLimit(); |
|
904
|
|
|
if ($phpLimit) { |
|
905
|
|
|
return $phpLimit; |
|
906
|
|
|
} |
|
907
|
|
|
} |
|
908
|
|
|
|
|
909
|
|
|
/** |
|
910
|
|
|
* Calculate the current memory limit of the server |
|
911
|
|
|
* |
|
912
|
|
|
* @return float |
|
913
|
|
|
*/ |
|
914
|
|
|
protected function getPHPMemoryLimit() |
|
915
|
|
|
{ |
|
916
|
|
|
return $this->parseMemory(trim(ini_get("memory_limit"))); |
|
917
|
|
|
} |
|
918
|
|
|
|
|
919
|
|
|
/** |
|
920
|
|
|
* Convert memory limit string to bytes. |
|
921
|
|
|
* Based on implementation in install.php5 |
|
922
|
|
|
* |
|
923
|
|
|
* @param string $memString |
|
924
|
|
|
* @return float |
|
925
|
|
|
*/ |
|
926
|
|
|
protected function parseMemory($memString) |
|
927
|
|
|
{ |
|
928
|
|
|
switch (strtolower(substr($memString, -1))) { |
|
929
|
|
|
case "b": |
|
930
|
|
|
return round(substr($memString, 0, -1)); |
|
931
|
|
|
case "k": |
|
932
|
|
|
return round(substr($memString, 0, -1) * 1024); |
|
933
|
|
|
case "m": |
|
934
|
|
|
return round(substr($memString, 0, -1) * 1024 * 1024); |
|
935
|
|
|
case "g": |
|
936
|
|
|
return round(substr($memString, 0, -1) * 1024 * 1024 * 1024); |
|
937
|
|
|
default: |
|
938
|
|
|
return round($memString); |
|
939
|
|
|
} |
|
940
|
|
|
} |
|
941
|
|
|
|
|
942
|
|
|
protected function humanReadable($size) |
|
943
|
|
|
{ |
|
944
|
|
|
$filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"); |
|
945
|
|
|
return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i] : '0 Bytes'; |
|
946
|
|
|
} |
|
947
|
|
|
|
|
948
|
|
|
|
|
949
|
|
|
/** |
|
950
|
|
|
* Gets a list of all the current jobs (or jobs that have recently finished) |
|
951
|
|
|
* |
|
952
|
|
|
* @param string $type |
|
953
|
|
|
* if we're after a particular job list |
|
954
|
|
|
* @param int $includeUpUntil |
|
955
|
|
|
* The number of seconds to include jobs that have just finished, allowing a job list to be built that |
|
956
|
|
|
* includes recently finished jobs |
|
957
|
|
|
* @return QueuedJobDescriptor |
|
958
|
|
|
*/ |
|
959
|
|
|
public function getJobList($type = null, $includeUpUntil = 0) |
|
960
|
|
|
{ |
|
961
|
|
|
return DataObject::get( |
|
962
|
|
|
QueuedJobDescriptor::class, |
|
963
|
|
|
$this->getJobListFilter($type, $includeUpUntil) |
|
964
|
|
|
); |
|
965
|
|
|
} |
|
966
|
|
|
|
|
967
|
|
|
/** |
|
968
|
|
|
* Return the SQL filter used to get the job list - this is used by the UI for displaying the job list... |
|
969
|
|
|
* |
|
970
|
|
|
* @param string $type |
|
971
|
|
|
* if we're after a particular job list |
|
972
|
|
|
* @param int $includeUpUntil |
|
973
|
|
|
* The number of seconds to include jobs that have just finished, allowing a job list to be built that |
|
974
|
|
|
* includes recently finished jobs |
|
975
|
|
|
* @return string |
|
976
|
|
|
*/ |
|
977
|
|
|
public function getJobListFilter($type = null, $includeUpUntil = 0) |
|
978
|
|
|
{ |
|
979
|
|
|
$util = singleton(QJUtils::class); |
|
980
|
|
|
|
|
981
|
|
|
$filter = array('JobStatus <>' => QueuedJob::STATUS_COMPLETE); |
|
982
|
|
|
if ($includeUpUntil) { |
|
983
|
|
|
$filter['JobFinished > '] = date('Y-m-d H:i:s', time() - $includeUpUntil); |
|
984
|
|
|
} |
|
985
|
|
|
|
|
986
|
|
|
$filter = $util->dbQuote($filter, ' OR '); |
|
987
|
|
|
|
|
988
|
|
|
if ($type) { |
|
|
|
|
|
|
989
|
|
|
$filter = $util->dbQuote(array('JobType =' => (string) $type)). ' AND ('.$filter.')'; |
|
990
|
|
|
} |
|
991
|
|
|
|
|
992
|
|
|
return $filter; |
|
993
|
|
|
} |
|
994
|
|
|
|
|
995
|
|
|
/** |
|
996
|
|
|
* Process the job queue with the current queue runner |
|
997
|
|
|
* |
|
998
|
|
|
* @param string $queue |
|
999
|
|
|
*/ |
|
1000
|
|
|
public function runQueue($queue) |
|
1001
|
|
|
{ |
|
1002
|
|
|
$this->checkJobHealth($queue); |
|
1003
|
|
|
$this->checkdefaultJobs($queue); |
|
1004
|
|
|
$this->queueRunner->runQueue($queue); |
|
1005
|
|
|
} |
|
1006
|
|
|
|
|
1007
|
|
|
/** |
|
1008
|
|
|
* Process all jobs from a given queue |
|
1009
|
|
|
* |
|
1010
|
|
|
* @param string $name The job queue to completely process |
|
1011
|
|
|
*/ |
|
1012
|
|
|
public function processJobQueue($name) |
|
1013
|
|
|
{ |
|
1014
|
|
|
// Start timer to measure lifetime |
|
1015
|
|
|
$this->markStarted(); |
|
1016
|
|
|
|
|
1017
|
|
|
// Begin main loop |
|
1018
|
|
|
do { |
|
1019
|
|
|
if (class_exists('Subsite')) { |
|
1020
|
|
|
// clear subsite back to default to prevent any subsite changes from leaking to |
|
1021
|
|
|
// subsequent actions |
|
1022
|
|
|
/** |
|
1023
|
|
|
* @todo Check for 4.x compatibility with Subsites once namespacing is implemented |
|
1024
|
|
|
*/ |
|
1025
|
|
|
\Subsite::changeSubsite(0); |
|
1026
|
|
|
} |
|
1027
|
|
|
|
|
1028
|
|
|
$job = $this->getNextPendingJob($name); |
|
1029
|
|
|
if ($job) { |
|
1030
|
|
|
$success = $this->runJob($job->ID); |
|
1031
|
|
|
if (!$success) { |
|
1032
|
|
|
// make sure job is null so it doesn't continue the current |
|
1033
|
|
|
// processing loop. Next queue executor can pick up where |
|
1034
|
|
|
// things left off |
|
1035
|
|
|
$job = null; |
|
1036
|
|
|
} |
|
1037
|
|
|
} |
|
1038
|
|
|
} while ($job); |
|
1039
|
|
|
} |
|
1040
|
|
|
|
|
1041
|
|
|
/** |
|
1042
|
|
|
* When PHP shuts down, we want to process all of the immediate queue items |
|
1043
|
|
|
* |
|
1044
|
|
|
* We use the 'getNextPendingJob' method, instead of just iterating the queue, to ensure |
|
1045
|
|
|
* we ignore paused or stalled jobs. |
|
1046
|
|
|
*/ |
|
1047
|
|
|
public function onShutdown() |
|
1048
|
|
|
{ |
|
1049
|
|
|
$this->processJobQueue(QueuedJob::IMMEDIATE); |
|
1050
|
|
|
} |
|
1051
|
|
|
|
|
1052
|
|
|
/** |
|
1053
|
|
|
* Get a logger |
|
1054
|
|
|
* @return LoggerInterface |
|
1055
|
|
|
*/ |
|
1056
|
|
|
public function getLogger() |
|
1057
|
|
|
{ |
|
1058
|
|
|
return Injector::inst()->get(LoggerInterface::class); |
|
1059
|
|
|
} |
|
1060
|
|
|
} |
|
1061
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.