Completed
Push — master ( c5ed18...ed0fce )
by Nathan
07:36
created

JobWorker::jobqueueExecute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace Symbiote\QueuedJobs\Workers;
4
5
use SilverStripe\ORM\DataList;
6
7
/**
8
 * @author [email protected]
9
 * @license BSD License http://silverstripe.org/bsd-license/
10
 */
11
12
// GearmanHandler is an extension that could be not available.
13
/**
14
 * @todo Test and implement against it for SilverStripe 4.x compatibility
15
 */
16
if (interface_exists('GearmanHandler')) {
17
    class JobWorker implements \GearmanHandler
18
    {
19
        /**
20
         * @var QueuedJobService
21
         */
22
        public $queuedJobService;
23
24
        /**
25
         * @return string
26
         */
27
        public function getName()
28
        {
29
            return 'jobqueueExecute';
30
        }
31
32
        /**
33
         * @param int $jobId
34
         * @return void
35
         */
36
        public function jobqueueExecute($jobId)
37
        {
38
            $this->queuedJobService->checkJobHealth();
39
            $job = DataList::create('Symbiote\\QueuedJobs\\DataObjects\\QueuedJobDescriptor')->byID($jobId);
40
            if ($job) {
41
                // check that we're not trying to execute something tooo soon
42
                if (strtotime($job->StartAfter) > time()) {
43
                    return;
44
                }
45
46
                $this->queuedJobService->runJob($jobId);
47
            }
48
        }
49
    }
50
}
51