/
ScheduledExecutionExtension.php
149 lines (133 loc) · 4.83 KB
/
ScheduledExecutionExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace Symbiote\QueuedJobs\Extensions;
use SilverStripe\Forms\DatetimeField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\NumericField;
use SilverStripe\Forms\ReadonlyField;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBDatetime;
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
use Symbiote\QueuedJobs\Jobs\ScheduledExecutionJob;
use Symbiote\QueuedJobs\Services\QueuedJobService;
/**
* An extension that can be added to objects that automatically
* adds scheduled execution capabilities to data objects.
*
* Developers who want to use these capabilities can set up
*
* @author marcus@symbiote.com.au
* @license BSD License http://silverstripe.org/bsd-license/
*
* @method QueuedJobDescriptor ScheduledJob()
*
* @extends DataExtension<DataObject&static>
*/
class ScheduledExecutionExtension extends DataExtension
{
/**
* @var array
*/
private static $db = array(
'FirstExecution' => 'DBDatetime',
'ExecuteInterval' => 'Int',
'ExecuteEvery' => "Enum(',Minute,Hour,Day,Week,Fortnight,Month,Year')",
'ExecuteFree' => 'Varchar',
);
/**
* @var array
*/
private static $defaults = array(
'ExecuteInterval' => 1,
);
/**
* @var array
*/
private static $has_one = array(
'ScheduledJob' => QueuedJobDescriptor::class,
);
/**
* @param FieldList $fields
*/
public function updateCMSFields(FieldList $fields)
{
$fields->removeByName([
'ExecuteInterval',
'ExecuteEvery',
'ExecuteFree',
'FirstExecution'
]);
$fields->findOrMakeTab(
'Root.Schedule',
_t(__CLASS__ . '.ScheduleTabTitle', 'Schedule')
);
$fields->addFieldsToTab('Root.Schedule', array(
$dt = DatetimeField::create('FirstExecution', _t(__CLASS__ . '.FIRST_EXECUTION', 'First Execution')),
FieldGroup::create(
NumericField::create('ExecuteInterval', ''),
DropdownField::create(
'ExecuteEvery',
'',
array(
'' => '',
'Minute' => _t(__CLASS__ . '.ExecuteEveryMinute', 'Minute'),
'Hour' => _t(__CLASS__ . '.ExecuteEveryHour', 'Hour'),
'Day' => _t(__CLASS__ . '.ExecuteEveryDay', 'Day'),
'Week' => _t(__CLASS__ . '.ExecuteEveryWeek', 'Week'),
'Fortnight' => _t(__CLASS__ . '.ExecuteEveryFortnight', 'Fortnight'),
'Month' => _t(__CLASS__ . '.ExecuteEveryMonth', 'Month'),
'Year' => _t(__CLASS__ . '.ExecuteEveryYear', 'Year'),
)
)
)->setTitle(_t(__CLASS__ . '.EXECUTE_EVERY', 'Execute every')),
TextField::create(
'ExecuteFree',
_t(__CLASS__ . '.EXECUTE_FREE', 'Scheduled (in strtotime format from first execution)')
)
));
if ($this->owner->ScheduledJobID) {
$jobTime = $this->owner->ScheduledJob()->StartAfter;
$fields->addFieldsToTab('Root.Schedule', array(
ReadonlyField::create('NextRunDate', _t(__CLASS__ . '.NEXT_RUN_DATE', 'Next run date'), $jobTime)
));
}
}
public function onBeforeWrite()
{
parent::onBeforeWrite();
if ($this->owner->FirstExecution) {
$changed = $this->owner->getChangedFields();
$changed = (
isset($changed['FirstExecution'])
|| isset($changed['ExecuteInterval'])
|| isset($changed['ExecuteEvery'])
|| isset($changed['ExecuteFree'])
);
if ($changed && $this->owner->ScheduledJobID) {
if ($this->owner->ScheduledJob()->exists()) {
$this->owner->ScheduledJob()->delete();
}
$this->owner->ScheduledJobID = 0;
}
if (!$this->owner->ScheduledJobID) {
$job = new ScheduledExecutionJob($this->owner);
$time = DBDatetime::now()->Rfc2822();
if ($this->owner->FirstExecution) {
$time = DBDatetime::create()->setValue($this->owner->FirstExecution)->Rfc2822();
}
$this->owner->ScheduledJobID = QueuedJobService::singleton()
->queueJob($job, $time);
}
}
}
/**
* Define your own version of this method in your data objects to be executed EVERY time
* the scheduled job triggers.
*/
public function onScheduledExecution()
{
}
}