1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TCronTask class file. |
5
|
|
|
* |
6
|
|
|
* @author Brad Anderson <[email protected]> |
7
|
|
|
* @link https://github.com/pradosoft/prado |
8
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Prado\Util\Cron; |
12
|
|
|
|
13
|
|
|
use Prado\Exceptions\TConfigurationException; |
14
|
|
|
use Prado\Prado; |
15
|
|
|
use Prado\TApplicationComponent; |
16
|
|
|
use Prado\TPropertyValue; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* TCronTask class. |
20
|
|
|
* |
21
|
|
|
* TCronTask is the base class for all cron tasks. Subclasses need to implement the |
22
|
|
|
* abstract method {@link execute} to run tasks. |
23
|
|
|
* If a task is not run at the schedule time, it is run at the next available task sweep. |
24
|
|
|
* |
25
|
|
|
* @author Brad Anderson <[email protected]> |
26
|
|
|
* @since 4.2.0 |
27
|
|
|
*/ |
28
|
|
|
abstract class TCronTask extends TApplicationComponent |
29
|
|
|
{ |
30
|
|
|
/** @var string The name of the task */ |
31
|
|
|
private $_name; |
32
|
|
|
|
33
|
|
|
/** @var string The schedule */ |
34
|
|
|
private $_schedule; |
35
|
|
|
|
36
|
|
|
/** @var string The user Id of the task */ |
37
|
|
|
private $_userName; |
38
|
|
|
|
39
|
|
|
/** @var string The module Id */ |
40
|
|
|
private $_moduleId; |
41
|
|
|
|
42
|
|
|
/** @var TTimeScheduler The time scheduler */ |
43
|
|
|
private $_scheduler; |
44
|
|
|
|
45
|
|
|
/** @var int The number of times which the cron task has run since the counter has been cleared */ |
46
|
|
|
private $_processCount = 0; |
47
|
|
|
|
48
|
|
|
/** @var int the last time this task was run */ |
49
|
|
|
private $_lastexectime; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* This is the abstract method for running a task. |
53
|
|
|
* @param TCronModule $cronModule the module calling the task |
54
|
|
|
*/ |
55
|
|
|
abstract public function execute($cronModule); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string the unique name of the Task |
59
|
|
|
*/ |
60
|
|
|
public function getName() |
61
|
|
|
{ |
62
|
|
|
return $this->_name; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $name the unique name of the Task |
67
|
|
|
*/ |
68
|
|
|
public function setName($name) |
69
|
|
|
{ |
70
|
|
|
$this->_name = TPropertyValue::ensureString($name); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string the cron style schedule of the task |
75
|
|
|
*/ |
76
|
|
|
public function getSchedule() |
77
|
|
|
{ |
78
|
|
|
return $this->_schedule; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* |
83
|
|
|
* @param string $schedule the cron style schedule of the task |
84
|
|
|
*/ |
85
|
|
|
public function setSchedule($schedule) |
86
|
|
|
{ |
87
|
|
|
$this->_schedule = TPropertyValue::ensureString($schedule); |
88
|
|
|
if ($this->_scheduler) { |
89
|
|
|
$this->_scheduler->setSchedule($this->_schedule); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string the task being executed |
95
|
|
|
*/ |
96
|
|
|
public function getTask() |
97
|
|
|
{ |
98
|
|
|
return get_class($this); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string the user id executing the Task |
103
|
|
|
*/ |
104
|
|
|
public function getUserName() |
105
|
|
|
{ |
106
|
|
|
return $this->_userName; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $username the user id executing the Task |
111
|
|
|
*/ |
112
|
|
|
public function setUserName($username) |
113
|
|
|
{ |
114
|
|
|
$this->_userName = TPropertyValue::ensureString($username); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return null|string the utility module for the task |
119
|
|
|
*/ |
120
|
|
|
public function getModuleId() |
121
|
|
|
{ |
122
|
|
|
return $this->_moduleId; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* |
127
|
|
|
* @param string $moduleId the utility module for the task. |
128
|
|
|
*/ |
129
|
|
|
public function setModuleId($moduleId) |
130
|
|
|
{ |
131
|
|
|
$this->_moduleId = ($moduleId !== null) ? TPropertyValue::ensureString($moduleId) : null; |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return null|\Prado\IModule returns the module from ModuleId |
136
|
|
|
*/ |
137
|
|
|
public function getModule() |
138
|
|
|
{ |
139
|
|
|
$app = $this->getApplication(); |
140
|
|
|
$module = $app->getModule($this->_moduleId); |
141
|
|
|
if ($module === null && $this->_moduleId !== null) { |
142
|
|
|
throw new TConfigurationException('crontask_no_module', $this->_moduleId); |
143
|
|
|
} |
144
|
|
|
return $module; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return int the number of times the task has run |
149
|
|
|
*/ |
150
|
|
|
public function getProcessCount() |
151
|
|
|
{ |
152
|
|
|
return $this->_processCount; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* |
157
|
|
|
* @param int $count the number of times the task has run |
158
|
|
|
*/ |
159
|
|
|
public function setProcessCount($count) |
160
|
|
|
{ |
161
|
|
|
return $this->_processCount = $count; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return numeric the time of running this cron task |
|
|
|
|
166
|
|
|
*/ |
167
|
|
|
public function getLastExecTime() |
168
|
|
|
{ |
169
|
|
|
return $this->_lastexectime; |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* sometimes floats don't output correctly to 6 significant figures (microtime). |
174
|
|
|
* @param numeric $v the time of running this cron task |
175
|
|
|
*/ |
176
|
|
|
public function setLastExecTime($v) |
177
|
|
|
{ |
178
|
|
|
if ($v !== null) { |
179
|
|
|
$this->_lastexectime = TPropertyValue::ensureInteger($v); |
180
|
|
|
} else { |
181
|
|
|
$this->_lastexectime = null; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Resets the lastExecTime to either null or, if there is a next trigger |
187
|
|
|
* time from time(), sets lastExecTime to now. This prevents erroneously |
188
|
|
|
* triggering repeating tasks on first cron from existing prior triggers |
189
|
|
|
* from time=0. |
190
|
|
|
*/ |
191
|
|
|
public function resetTaskLastExecTime() |
192
|
|
|
{ |
193
|
|
|
$now = time(); |
194
|
|
|
$schedule = $this->getScheduler(); |
195
|
|
|
$nextTriggerTime = $schedule->getNextTriggerTime($now); |
196
|
|
|
$this->_lastexectime = ($nextTriggerTime === null) ? null : |
197
|
|
|
(($schedule->getNextTriggerTime(0) == $nextTriggerTime) ? null : $now); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return null|numeric time of the next trigger after the lastExecTime, null if none. |
202
|
|
|
*/ |
203
|
|
|
public function getNextTriggerTime() |
204
|
|
|
{ |
205
|
|
|
$s = $this->getScheduler(); |
206
|
|
|
return $s->getNextTriggerTime($this->_lastexectime ?? 0); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return bool is the current time after the NextTriggerTime |
211
|
|
|
*/ |
212
|
|
|
public function getIsPending() |
213
|
|
|
{ |
214
|
|
|
$trigger = $this->getNextTriggerTime(); |
215
|
|
|
return $trigger !== null && time() >= $trigger; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return \Prado\Util\Cron\TTimeScheduler the time scheduler for processing the schedule |
220
|
|
|
*/ |
221
|
|
|
public function getScheduler() |
222
|
|
|
{ |
223
|
|
|
if ($this->_scheduler === null) { |
224
|
|
|
$this->_scheduler = Prado::createComponent('Prado\\Util\\Cron\\TTimeScheduler'); |
225
|
|
|
$this->_scheduler->setSchedule($this->_schedule); |
226
|
|
|
} |
227
|
|
|
return $this->_scheduler; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Returns an array with the names of all variables of this object that should NOT be serialized |
232
|
|
|
* because their value is the default one or useless to be cached for the next load. |
233
|
|
|
* Reimplement in derived classes to add new variables, but remember to also to call the parent |
234
|
|
|
* implementation first. |
235
|
|
|
* @param array $exprops by reference |
236
|
|
|
*/ |
237
|
|
|
protected function _getZappableSleepProps(&$exprops) |
238
|
|
|
{ |
239
|
|
|
parent::_getZappableSleepProps($exprops); |
240
|
|
|
|
241
|
|
|
$exprops[] = "\0Prado\Util\Cron\TCronTask\0_scheduler"; |
242
|
|
|
if ($this->_userName === null) { |
243
|
|
|
$exprops[] = "\0Prado\Util\Cron\TCronTask\0_userName"; |
244
|
|
|
} |
245
|
|
|
if ($this->_moduleId === null) { |
246
|
|
|
$exprops[] = "\0Prado\Util\Cron\TCronTask\0_moduleId"; |
247
|
|
|
} |
248
|
|
|
if ($this->_processCount == 0) { |
249
|
|
|
$exprops[] = "\0Prado\Util\Cron\TCronTask\0_processCount"; |
250
|
|
|
} |
251
|
|
|
if ($this->_lastexectime == null) { |
252
|
|
|
$exprops[] = "\0Prado\Util\Cron\TCronTask\0_lastexectime"; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|