forked from omnilight/yii2-scheduling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduleController.php
75 lines (61 loc) · 1.75 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
<?php
namespace omnilight\scheduling;
use yii\console\Controller;
use yii\di\Instance;
/**
* Run the scheduled commands
*/
class ScheduleController extends Controller
{
/**
* @var Schedule
*/
public $schedule = 'schedule';
/**
* @var string Schedule file that will be used to run schedule
*/
public $scheduleFile;
public function options($actionID)
{
return array_merge(parent::options($actionID),
$actionID == 'run' ? ['scheduleFile'] : []
);
}
public function init()
{
if (\Yii::$app->has($this->schedule)) {
$this->schedule = Instance::ensure($this->schedule, Schedule::className());
} else {
$this->schedule = \Yii::createObject(Schedule::className());
}
parent::init();
}
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");
}
}
protected function importScheduleFile()
{
if ($this->scheduleFile === null) {
return;
}
$scheduleFile = \Yii::getAlias($this->scheduleFile);
if (file_exists($scheduleFile) == false) {
$this->stderr('Can not load schedule file '.$this->scheduleFile."\n");
return;
}
$schedule = $this->schedule;
call_user_func(function() use ($schedule, $scheduleFile) {
include $scheduleFile;
});
}
}