|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\QueuedJobs\Jobs; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataObject; |
|
6
|
|
|
use SilverStripe\QueuedJobs\Services\AbstractQueuedJob; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A job that gets executed on a particular schedule. When it runs, |
|
10
|
|
|
* it will call the onScheduledExecution method on the owning |
|
11
|
|
|
* dataobject. |
|
12
|
|
|
* |
|
13
|
|
|
* @author [email protected] |
|
14
|
|
|
* @license BSD License http://silverstripe.org/bsd-license/ |
|
15
|
|
|
*/ |
|
16
|
|
|
class ScheduledExecutionJob extends AbstractQueuedJob |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param DataObject $dataObject |
|
20
|
|
|
* @param int $timesExecuted |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct($dataObject = null, $timesExecuted = 0) |
|
23
|
|
|
{ |
|
24
|
|
|
if ($dataObject) { |
|
25
|
|
|
$this->objectID = $dataObject->ID; |
|
|
|
|
|
|
26
|
|
|
$this->objectType = $dataObject->ClassName; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
// captured so we have a unique hash generated for this job |
|
29
|
|
|
$this->timesExecuted = $timesExecuted; |
|
|
|
|
|
|
30
|
|
|
$this->totalSteps = 1; |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return DataObject |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getDataObject() |
|
38
|
|
|
{ |
|
39
|
|
|
return DataObject::get_by_id($this->objectType, $this->objectID); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getTitle() |
|
46
|
|
|
{ |
|
47
|
|
|
return _t( |
|
48
|
|
|
'ScheduledExecutionJob.Title', |
|
49
|
|
|
'Scheduled execution for {title}', |
|
50
|
|
|
array('title' => $this->getDataObject()->getTitle()) |
|
|
|
|
|
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
public function setup() |
|
56
|
|
|
{ |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function process() |
|
60
|
|
|
{ |
|
61
|
|
|
$object = $this->getDataObject(); |
|
62
|
|
|
if ($object) { |
|
63
|
|
|
$object->onScheduledExecution(); |
|
64
|
|
|
|
|
65
|
|
|
// figure out what our rescheduled date should be |
|
66
|
|
|
$timeStr = $object->ExecuteFree; |
|
67
|
|
|
if ($object->ExecuteEvery) { |
|
68
|
|
|
$executeInterval = $object->ExecuteInterval; |
|
69
|
|
|
if (!$executeInterval || !is_numeric($executeInterval)) { |
|
70
|
|
|
$executeInterval = 1; |
|
71
|
|
|
} |
|
72
|
|
|
$timeStr = '+' . $executeInterval . ' ' . $object->ExecuteEvery; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$next = strtotime($timeStr); |
|
76
|
|
|
if ($next > time()) { |
|
77
|
|
|
// in the future |
|
78
|
|
|
$nextGen = date('Y-m-d H:i:s', $next); |
|
79
|
|
|
$nextId = singleton('SilverStripe\\QueuedJobs\\Services\\QueuedJobService')->queueJob( |
|
80
|
|
|
new ScheduledExecutionJob($object, $this->timesExecuted + 1), |
|
|
|
|
|
|
81
|
|
|
$nextGen |
|
82
|
|
|
); |
|
83
|
|
|
$object->ScheduledJobID = $nextId; |
|
84
|
|
|
$object->write(); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->currentStep++; |
|
89
|
|
|
$this->isComplete = true; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
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@propertyannotation to your class or interface to document the existence of this variable.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.