|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yii2mod\scheduling; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
|
|
|
|
|
6
|
|
|
use Cron\CronExpression; |
|
7
|
|
|
use League\CLImate\CLImate; |
|
8
|
|
|
use Yii; |
|
9
|
|
|
use yii\base\InvalidConfigException; |
|
10
|
|
|
use yii\console\Controller; |
|
11
|
|
|
use yii\di\Instance; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class ScheduleController |
|
15
|
|
|
* |
|
16
|
|
|
* @package yii2mod\scheduling |
|
17
|
|
|
*/ |
|
18
|
|
|
class ScheduleController extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Schedule |
|
22
|
|
|
*/ |
|
23
|
|
|
public $schedule = 'schedule'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string Schedule file that will be used to run schedule |
|
27
|
|
|
*/ |
|
28
|
|
|
public $scheduleFile = '@app/config/schedule.php'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @inheritdoc |
|
32
|
|
|
*/ |
|
33
|
|
|
public function options($actionID) |
|
34
|
|
|
{ |
|
35
|
|
|
return array_merge(parent::options($actionID), |
|
36
|
|
|
$actionID == 'run' ? ['scheduleFile'] : [] |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritdoc |
|
42
|
|
|
*/ |
|
43
|
|
|
public function init() |
|
44
|
|
|
{ |
|
45
|
|
|
parent::init(); |
|
46
|
|
|
|
|
47
|
|
|
if (Yii::$app->has($this->schedule)) { |
|
|
|
|
|
|
48
|
|
|
$this->schedule = Instance::ensure($this->schedule, Schedule::class); |
|
49
|
|
|
} else { |
|
50
|
|
|
$this->schedule = Yii::createObject(Schedule::class); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Run scheduled commands |
|
56
|
|
|
*/ |
|
57
|
|
|
public function actionRun() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->importScheduleFile(); |
|
60
|
|
|
|
|
61
|
|
|
$events = $this->schedule->dueEvents(Yii::$app); |
|
62
|
|
|
|
|
63
|
|
|
foreach ($events as $event) { |
|
64
|
|
|
$this->stdout('Running scheduled command: ' . $event->getSummaryForDisplay() . "\n"); |
|
65
|
|
|
$event->run(Yii::$app); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (count($events) === 0) { |
|
69
|
|
|
$this->stdout("No scheduled commands are ready to run.\n"); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Render list of registered tasks |
|
75
|
|
|
*/ |
|
76
|
|
|
public function actionList() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->importScheduleFile(); |
|
79
|
|
|
|
|
80
|
|
|
$climate = new CLImate(); |
|
81
|
|
|
$data = []; |
|
82
|
|
|
$row = 0; |
|
83
|
|
|
|
|
84
|
|
|
foreach ($this->schedule->getEvents() as $event) { |
|
85
|
|
|
$data[] = [ |
|
86
|
|
|
'#' => ++$row, |
|
87
|
|
|
'Task' => $event->getSummaryForDisplay(), |
|
88
|
|
|
'Expression' => $event->getExpression(), |
|
89
|
|
|
'Command to Run' => is_a($event, CallbackEvent::class) |
|
90
|
|
|
? $event->getSummaryForDisplay() |
|
91
|
|
|
: $event->command, |
|
92
|
|
|
'Next run at' => $this->getNextRunDate($event), |
|
93
|
|
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$climate->table($data); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Import schedule file |
|
101
|
|
|
* |
|
102
|
|
|
* @throws InvalidConfigException |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function importScheduleFile() |
|
105
|
|
|
{ |
|
106
|
|
|
$scheduleFile = Yii::getAlias($this->scheduleFile); |
|
107
|
|
|
|
|
108
|
|
|
if (!file_exists($scheduleFile)) { |
|
109
|
|
|
throw new InvalidConfigException("Can not load schedule file {$this->scheduleFile}"); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$schedule = $this->schedule; |
|
113
|
|
|
call_user_func(function () use ($schedule, $scheduleFile) { |
|
114
|
|
|
include $scheduleFile; |
|
115
|
|
|
}); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get the next scheduled run date for this event |
|
120
|
|
|
* |
|
121
|
|
|
* @param Event $event |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function getNextRunDate(Event $event) |
|
126
|
|
|
{ |
|
127
|
|
|
$cron = CronExpression::factory($event->getExpression()); |
|
128
|
|
|
$date = Carbon::now(); |
|
129
|
|
|
|
|
130
|
|
|
if ($event->hasTimezone()) { |
|
131
|
|
|
$date->setTimezone($event->getTimezone()); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $cron->getNextRunDate()->format('Y-m-d H:i:s'); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths