-
Notifications
You must be signed in to change notification settings - Fork 1
/
CronTask.php
603 lines (519 loc) · 16.4 KB
/
CronTask.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
<?php
/**
* CronTask is the class with details of the scheduled application console task.
*
* It has all necessary methods to set time schedule of the task. By default schedule is equal to original
* cron's: '* * * * *' that means to run specified console command every minute.
*
* Original cron syntax is supported for methods hour(), minute(), day(), month(), dayOfWeek(), cron().
*
* By calling `unique()` method you can set flag for task to be unique. It means that if task already running and
* it's time to run it again - new instance will not be executed (warning message would be added to logs).
*
* You can combine any methods to set desired schedule. For example, if you want to run command 'import' with
* action 'products' and params '--updateAll=1' every day at 18:00, you need to create next task instance:
*
* $task = new CronTask('import', 'products', array('updateAll' => 1));
* $task->name('Import products (daily@18:00)')->daily()->hour(18);
*
* How to add and run task see CronService class description.
*
* @author Vadym Stepanov <[email protected]>
* @date 26.07.2017
*/
class CronTask
{
const MONTH_JANUARY = 1;
const MONTH_FEBRUARY = 2;
const MONTH_MARCH = 3;
const MONTH_APRIL = 4;
const MONTH_MAY = 5;
const MONTH_JUNE = 6;
const MONTH_JULY = 7;
const MONTH_AUGUST = 8;
const MONTH_SEPTEMBER = 9;
const MONTH_OCTOBER = 10;
const MONTH_NOVEMBER = 11;
const MONTH_DECEMBER = 12;
const WEEK_MONDAY = 0;
const WEEK_TUESDAY = 1;
const WEEK_WEDNESDAY = 2;
const WEEK_THURSDAY = 3;
const WEEK_FRIDAY = 4;
const WEEK_SATURDAY = 5;
const WEEK_SUNDAY = 6;
/**
* @var string console controller of command to execute. Required
*/
private $command;
/**
* @var string action of console controller of command to execute. Can be omitted
*/
private $action;
/**
* @var array key-valued list of params for specified command
*/
private $params = array();
/**
* @var string optional name of the task (used in displaying of the task statuses and logs). Optional.
*/
private $name;
/**
* @var bool the flag to set uniqueness of the task (to disallow overlapping)
*/
private $unique = false;
/**
* @var string the name of the output file with full path to output task process
*/
private $outputFile;
/**
* @var string minute value for cron schedule. Valid range: 0-59. Default: '*', means 'every minute'.
*/
private $minuteValue = '*';
/**
* @var string hour value for cron schedule. Valid range: 0-23. Default: '*', means 'every hour'.
*/
private $hourValue = '*';
/**
* @var string day of month value for cron schedule. Valid range: 1-31. Default: '*', means 'every day'.
*/
private $dayValue = '*';
/**
* @var string month value for cron schedule. Valid range: 1-12. Default: '*', means 'every month'.
*/
private $monthValue = '*';
/**
* @var string day of week value for cron schedule. Valid range: 0-6, 0 for Sunday, 6 for Saturday. Default: '*',
* means 'any day of week'.
*/
private $dayOfWeekValue = '*';
/**
* @var CronProcess information about status and last start/stop time
*/
private $_process;
/**
* @var array calculated values of allowed minutes, hours, days, etc.
*/
private $_allowedValues = array();
/**
* Create default task, check command and params
* @param string $command
* @param string|null $action
* @param array $params
* @throws InvalidArgumentException
*/
public function __construct($command, $action = null, array $params = array())
{
if (empty($command)) {
throw new InvalidArgumentException(
'Command cannot be empty. You should specify name of the application console command'
);
}
if (!preg_match('/^[a-z0-9\-_]+$/i', $command)) {
throw new InvalidArgumentException(
'Specified command value is not valid. Valid examples: myConsole, import, product-feed'
);
}
if (!empty($action) && !preg_match('/^[a-z0-9\-_]+$/i', $action)) {
throw new InvalidArgumentException(
'Specified action value is not valid. Valid examples: run, index, force-start'
);
}
$this->command = $command;
$this->action = $action;
$clearedParams = array();
foreach ($params as $key => $value) {
if (!preg_match('/^[a-z]+\w+$/i', $key)) {
throw new InvalidArgumentException(
"Bad param name: '{$key}'. It must contain alphanumeric values and/or underscore."
);
}
$clearedParams[$key] = $value;
}
$this->params = $clearedParams;
}
/**
* Get console command
* @return string
*/
public function getCommand()
{
return $this->command;
}
/**
* Get console command action
* @return string
*/
public function getCommandAction()
{
return $this->action;
}
/**
* Get list of params (already cleared and escaped)
* @return array
*/
public function getParams()
{
return $this->params;
}
/**
* Get unique identifier for current task
* @param string $hashFunc the name of the PHP hash function
* @return string
*/
public function getId($hashFunc = 'sha1')
{
if (!function_exists($hashFunc)) {
throw new RuntimeException(
"Your server does not have hash function '{$hashFunc}'. Use another one: md5, sha1, crc32"
);
}
return $hashFunc(json_encode(array(
$this->command,
$this->action,
$this->params,
)));
}
/**
* Get name of the task. If not specified, identifier will be returned.
* @return string
*/
public function getName()
{
return $this->name ?: $this->getId();
}
/**
* Get output file
* @return string
*/
public function getOutputFile()
{
return $this->outputFile;
}
/**
* Get cron schedule
* @return string
*/
public function getCron()
{
return "{$this->minuteValue} {$this->hourValue} {$this->dayValue} {$this->monthValue} {$this->dayOfWeekValue}";
}
/**
* Check if task instances cannot be overlapped
* @return bool
*/
public function isUnique()
{
return $this->unique;
}
/**
* Set task to be unique to prevent overlapping
* @return $this
*/
public function unique()
{
$this->unique = true;
return $this;
}
/**
* Set name of the task
* @param string $value
* @return $this
*/
public function name($value)
{
$this->name = trim($value);
return $this;
}
/**
* Set hour schedule of the task.
* @param string $value
* @return $this
*/
public function hour($value)
{
$this->hourValue = $this->parseValue('hour', (string)$value);
return $this;
}
/**
* Set minute schedule of the task
* @param string $value
* @return $this
*/
public function minute($value)
{
$this->minuteValue = $this->parseValue('minute', (string)$value);
return $this;
}
/**
* Set day of month schedule of the task
* @param string $value
* @return $this
*/
public function day($value)
{
$this->dayValue = $this->parseValue('day', (string)$value);
return $this;
}
/**
* Set month schedule of the task
* @param string $value
* @return $this
*/
public function month($value)
{
$this->monthValue = $this->parseValue('month', (string)$value);
return $this;
}
/**
* Set day of week schedule of the task
* @param string $value
* @return $this
*/
public function dayOfWeek($value)
{
$this->dayOfWeekValue = $this->parseValue('dayOfWeek', (string)$value);
return $this;
}
/**
* Macro method to run task at the beginning of the each hour (at 10:00, 11:00, ...)
* @return $this
*/
public function hourly()
{
return $this->cron('0 * * * *');
}
/**
* Macro method to run task at the beginning of the each day
* @return $this
*/
public function daily()
{
return $this->cron('0 0 * * *');
}
/**
* Macro method to run task on the 1st day of each month
* @return $this
*/
public function monthly()
{
return $this->cron('0 0 1 * *');
}
/**
* Macro method to run task on the 1st day of each year
* @return $this
*/
public function yearly()
{
return $this->cron('0 0 1 1 *');
}
/**
* Macro method to run task on each Sunday at 00:00
* @return $this
*/
public function weekly()
{
return $this->cron('0 0 * * 0');
}
/**
* Set schedule by cron value
* @param string $value
* @return $this
* @throws InvalidArgumentException
*/
public function cron($value)
{
$parts = array('minute', 'hour', 'day', 'month', 'dayOfWeek');
$partPattern = '[\d\/\-\,\*]+';
foreach ($parts as &$part) {
$part = "(?P<{$part}>{$partPattern})";
}
unset($part);
$regexp = '/^' . implode('\s', $parts) . '$/';
preg_match($regexp, $value, $matches);
if (count($matches) === 0) {
throw new InvalidArgumentException("Bad cron expression: {$value}");
}
$this
->minute($matches['minute'])
->hour($matches['hour'])
->day($matches['day'])
->month($matches['month'])
->dayOfWeek($matches['dayOfWeek']);
return $this;
}
/**
* Set output file to store any output from task console command
* @param string $filePath
* @return $this
* @throws InvalidArgumentException
*/
public function setOutputFile($filePath)
{
if (is_dir($filePath)) {
throw new InvalidArgumentException('Wrong output path - this is the directory!');
}
$dirName = dirname($filePath);
if (!is_dir($dirName)) {
throw new InvalidArgumentException('Wrong output path - target directory for output file does not exist!');
}
if (!is_writable($dirName)) {
throw new InvalidArgumentException('Wrong output path - target directory for output file does not writable!');
}
$this->outputFile = $filePath;
return $this;
}
/**
* Check if task can be executed now.
* @return bool
*/
public function canRun()
{
$date = getdate();
$dateValues = array(
'minute' => $date['minutes'],
'hour' => $date['hours'],
'day' => $date['mday'],
'month' => $date['mon'],
'dayOfWeek' => $date['wday'],
);
$canRun = array();
foreach ($dateValues as $field => $value) {
if (!array_key_exists($field, $this->_allowedValues)) {
$fieldName = $field . 'Value';
$this->parseValue($field, $this->$fieldName);
}
$canRun[$field] = in_array($value, $this->_allowedValues[$field], true);
}
return $canRun['minute'] && $canRun['hour'] && $canRun['month'] && $canRun['day'] && $canRun['dayOfWeek'];
}
/**
* Create process instance with additional information
* @return CronProcess
*/
public function getProcessInfo()
{
if ($this->_process === null) {
$this->_process = CronProcess::createByTask($this, Yii::app()->cron);
}
return $this->_process;
}
/**
* Parse value of the specified field in cron schedule and get allowed date/time values (minutes, hours, ...)
* @param string $field name of the field of cron schedule: 'hour', 'minute', ...
* @param string $value value for field
* @return string
* @throws InvalidArgumentException
*/
private function parseValue($field, $value)
{
$regexp = '(\,|^)(?P<arg1>\*|%DIGIT%)(\-(?P<arg2>%DIGIT%))?(\/(?P<step>%DIGIT%))?';
$digitRegexp = '';
switch ($field) {
case 'hour':
$digitRegexp = '[0-2]?[0-9]+';
break;
case 'minute':
$digitRegexp = '[0-5]?[0-9]+';
break;
case 'day':
$digitRegexp = '[1-3]?[0-9]+';
break;
case 'month':
$digitRegexp = '1?[0-9]+';
break;
case 'dayOfWeek':
$digitRegexp = '[0-6]+';
break;
}
$regexp = str_replace('%DIGIT%', $digitRegexp, $regexp);
preg_match_all("/{$regexp}/", $value, $matches);
$num = count($matches['arg1']);
if ($num === 0) {
throw new InvalidArgumentException("Bad syntax for '{$field}' (value: '{$value}')");
}
$analyze = array();
for ($i = 0; $i < $num; $i++) {
$analyze[] = array(
'arg1' => $matches['arg1'][$i],
'arg2' => $matches['arg2'][$i],
'step' => $matches['step'][$i],
);
}
$this->getAllowedValues($field, $analyze);
return $value;
}
/**
* Get all allowed values for parsed field conditions
* @param string $field name of the field of cron schedule
* @param array $matches parsed range conditions from entered value
* @throws InvalidArgumentException
*/
private function getAllowedValues($field, $matches)
{
$sequence = array();
foreach ($matches as $match) {
if ($match['arg1'] === '*' && $match['arg2'] !== '') {
throw new InvalidArgumentException(
"Bad syntax for '{$field}' (part of value: '{$match['arg1']}-{$match['arg2']}')"
);
}
try {
$sequence = array_merge(
$sequence,
$this->generateSequence($field, $match['arg1'], $match['arg2'], $match['step'])
);
} catch (InvalidArgumentException $e) {
throw new InvalidArgumentException(
"Bad syntax for '{$field}' (part of value: '{$match['arg1']}-{$match['arg2']}'). Error: {$e->getMessage()}"
);
}
}
$this->_allowedValues[$field] = $sequence;
}
/**
* Generate sequence of the allowed values for specified range with some step
* @param string $field name of the field of cron schedule
* @param mixed $start start of the range or '*' (means, all available values)
* @param mixed $end end of the range (can be omitted)
* @param mixed $step step of value increasing in range
* @return array
* @throws InvalidArgumentException
*/
private function generateSequence($field, $start, $end, $step)
{
$fieldLimits = array(
'hour' => array('start' => 0, 'end' => 23),
'minute' => array('start' => 0, 'end' => 59),
'day' => array('start' => 1, 'end' => 31),
'month' => array('start' => 1, 'end' => 12),
'dayOfWeek' => array('start' => 0, 'end' => 6),
);
$step = $step ? (int)$step : 1;
if ($start === '*') {
$start = $fieldLimits[$field]['start'];
$end = $fieldLimits[$field]['end'];
} else {
$start = (int)$start;
if ($start < $fieldLimits[$field]['start']) {
throw new InvalidArgumentException("Wrong beginning of the range: '{$start}'");
}
if (empty($end)) {
$end = $start;
if ($step > 1) {
$end = $fieldLimits[$field]['end'];
}
} else {
$end = (int)$end;
}
if ($end > $fieldLimits[$field]['end']) {
throw new InvalidArgumentException("Wrong ending of the range: '{$end}'");
}
}
$values = array();
for ($i = $start; $i <= $end; $i += $step) {
$values[] = $i;
}
return $values;
}
}