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