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