GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 68dece...87f38d )
by Robbie
9s
created

QueuedJobService::setRunAsUser()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 12
nc 6
nop 2
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;
0 ignored issues
show
Unused Code introduced by
The property $stall_threshold is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
The property $memory_limit is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
The property $time_limit is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
The property $use_shutdown_function is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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';
0 ignored issues
show
Unused Code introduced by
The property $cache_dir is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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;
0 ignored issues
show
Documentation introduced by
The property RunAsID does not exist on object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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.

Loading history...
193
194
        // copy data
195
        $this->copyJobToDescriptor($job, $jobDescriptor);
0 ignored issues
show
Documentation introduced by
$jobDescriptor is of type object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>, but the function expects a object<Symbiote\QueuedJo...Services\JobDescriptor>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
196
197
        $jobDescriptor->write();
198
199
        $this->startJob($jobDescriptor, $startAfter);
0 ignored issues
show
Documentation introduced by
$jobDescriptor is of type object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>, but the function expects a object<Symbiote\QueuedJo...Services\JobDescriptor>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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;
0 ignored issues
show
Unused Code introduced by
$jobData is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
248
        $messages = null;
0 ignored issues
show
Unused Code introduced by
$messages is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $runningJob is correct as $list->filter('JobStatus...::STATUS_RUN))->first() (which targets SilverStripe\ORM\DataList::first()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
300
            ->filter('JobStatus', array(QueuedJob::STATUS_INIT, QueuedJob::STATUS_RUN))
301
            ->first();
302
        if ($runningJob) {
303
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Symbiote\QueuedJobs\Serv...vice::getNextPendingJob of type Symbiote\QueuedJobs\Data...ueuedJobDescriptor|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
$queue is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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())
0 ignored issues
show
Security Bug introduced by
It seems like \SilverStripe\Control\Director::absoluteBaseURL() targeting SilverStripe\Control\Director::absoluteBaseURL() can also be of type false; however, SilverStripe\Control\Email\Email::addData() does only seem to accept string|null, did you maybe forget to handle an error condition?
Loading history...
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);
0 ignored issues
show
Documentation introduced by
$jobDescriptor is of type object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>, but the function expects a object<Symbiote\QueuedJo...Services\JobDescriptor>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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) {
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\ORM\DB::getConn() has been deprecated with message: since version 4.0 Use DB::get_conn instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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)
0 ignored issues
show
Coding Style introduced by
runJob uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $originalUser defined by $originalUserID ? \Silve...$originalUserID) : null on line 566 can also be of type object<SilverStripe\ORM\DataObject>; however, Symbiote\QueuedJobs\Serv...Service::setRunAsUser() does only seem to accept null|object<SilverStripe\Security\Member>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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)) {
0 ignored issues
show
Compatibility introduced by
$jobDescriptor of type object<SilverStripe\ORM\DataObject> is not a sub-type of object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>. It seems like you assume a child class of the class SilverStripe\ORM\DataObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
587
            try {
588
                $job = $this->initialiseJob($jobDescriptor);
0 ignored issues
show
Compatibility introduced by
$jobDescriptor of type object<SilverStripe\ORM\DataObject> is not a sub-type of object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>. It seems like you assume a child class of the class SilverStripe\ORM\DataObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
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()
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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 {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $originalUser defined by $originalUserID ? \Silve...$originalUserID) : null on line 566 can also be of type object<SilverStripe\ORM\DataObject>; however, Symbiote\QueuedJobs\Serv...rvice::unsetRunAsUser() does only seem to accept null|object<SilverStripe\Security\Member>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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)
0 ignored issues
show
Coding Style introduced by
setRunAsUser uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
unsetRunAsUser uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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');
0 ignored issues
show
Documentation Bug introduced by
It seems like \SilverStripe\ORM\FieldT...ime::now()->Format('U') can also be of type string. However, the property $startedAt is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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