-
Notifications
You must be signed in to change notification settings - Fork 9
/
ScheduleController.php
136 lines (115 loc) · 3.18 KB
/
ScheduleController.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
<?php
namespace yii2mod\scheduling;
use Carbon\Carbon;
use Cron\CronExpression;
use League\CLImate\CLImate;
use Yii;
use yii\base\InvalidConfigException;
use yii\console\Controller;
use yii\di\Instance;
/**
* Class ScheduleController
*
* @package yii2mod\scheduling
*/
class ScheduleController extends Controller
{
/**
* @var Schedule
*/
public $schedule = 'schedule';
/**
* @var string Schedule file that will be used to run schedule
*/
public $scheduleFile = '@app/config/schedule.php';
/**
* @inheritdoc
*/
public function options($actionID)
{
return array_merge(parent::options($actionID),
$actionID == 'run' ? ['scheduleFile'] : []
);
}
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (Yii::$app->has($this->schedule)) {
$this->schedule = Instance::ensure($this->schedule, Schedule::class);
} else {
$this->schedule = Yii::createObject(Schedule::class);
}
}
/**
* Run scheduled commands
*/
public function actionRun()
{
$this->importScheduleFile();
$events = $this->schedule->dueEvents(Yii::$app);
foreach ($events as $event) {
$this->stdout('Running scheduled command: ' . $event->getSummaryForDisplay() . "\n");
$event->run(Yii::$app);
}
if (count($events) === 0) {
$this->stdout("No scheduled commands are ready to run.\n");
}
}
/**
* Render list of registered tasks
*/
public function actionList()
{
$this->importScheduleFile();
$climate = new CLImate();
$data = [];
$row = 0;
foreach ($this->schedule->getEvents() as $event) {
$data[] = [
'#' => ++$row,
'Task' => $event->getSummaryForDisplay(),
'Expression' => $event->getExpression(),
'Command to Run' => is_a($event, CallbackEvent::class)
? $event->getSummaryForDisplay()
: $event->command,
'Next run at' => $this->getNextRunDate($event),
];
}
$climate->table($data);
}
/**
* Import schedule file
*
* @throws InvalidConfigException
*/
protected function importScheduleFile()
{
$scheduleFile = Yii::getAlias($this->scheduleFile);
if (!file_exists($scheduleFile)) {
throw new InvalidConfigException("Can not load schedule file {$this->scheduleFile}");
}
$schedule = $this->schedule;
call_user_func(function () use ($schedule, $scheduleFile) {
include $scheduleFile;
});
}
/**
* Get the next scheduled run date for this event
*
* @param Event $event
*
* @return string
*/
protected function getNextRunDate(Event $event)
{
$cron = CronExpression::factory($event->getExpression());
$date = Carbon::now();
if ($event->hasTimezone()) {
$date->setTimezone($event->getTimezone());
}
return $cron->getNextRunDate()->format('Y-m-d H:i:s');
}
}