yii2mod /
yii2-scheduling
| 1 | <?php |
||
| 2 | |||
| 3 | namespace yii2mod\scheduling; |
||
| 4 | |||
| 5 | use League\CLImate\CLImate; |
||
| 6 | use Yii; |
||
| 7 | use yii\base\InvalidConfigException; |
||
| 8 | use yii\console\Controller; |
||
| 9 | use yii\di\Instance; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Class ScheduleController |
||
| 13 | * |
||
| 14 | * @package yii2mod\scheduling |
||
| 15 | */ |
||
| 16 | class ScheduleController extends Controller |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var Schedule |
||
| 20 | */ |
||
| 21 | public $schedule = 'schedule'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string Schedule file that will be used to run schedule |
||
| 25 | */ |
||
| 26 | public $scheduleFile = '@app/config/schedule.php'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @inheritdoc |
||
| 30 | */ |
||
| 31 | public function options($actionID) |
||
| 32 | { |
||
| 33 | return array_merge(parent::options($actionID), |
||
| 34 | $actionID == 'run' ? ['scheduleFile'] : [] |
||
| 35 | ); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @inheritdoc |
||
| 40 | */ |
||
| 41 | public function init() |
||
| 42 | { |
||
| 43 | parent::init(); |
||
| 44 | |||
| 45 | if (Yii::$app->has($this->schedule)) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 46 | $this->schedule = Instance::ensure($this->schedule, Schedule::class); |
||
| 47 | } else { |
||
| 48 | $this->schedule = Yii::createObject(Schedule::class); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Run scheduled commands |
||
| 54 | */ |
||
| 55 | public function actionRun() |
||
| 56 | { |
||
| 57 | $this->importScheduleFile(); |
||
| 58 | |||
| 59 | $events = $this->schedule->dueEvents(Yii::$app); |
||
| 60 | |||
| 61 | foreach ($events as $event) { |
||
| 62 | $this->stdout('Running scheduled command: ' . $event->getSummaryForDisplay() . "\n"); |
||
| 63 | $event->run(Yii::$app); |
||
| 64 | } |
||
| 65 | |||
| 66 | if (count($events) === 0) { |
||
| 67 | $this->stdout("No scheduled commands are ready to run.\n"); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Render list of registered tasks |
||
| 73 | */ |
||
| 74 | public function actionList() |
||
| 75 | { |
||
| 76 | $this->importScheduleFile(); |
||
| 77 | |||
| 78 | $climate = new CLImate(); |
||
| 79 | $data = []; |
||
| 80 | $row = 0; |
||
| 81 | |||
| 82 | foreach ($this->schedule->getEvents() as $event) { |
||
| 83 | $data[] = [ |
||
| 84 | '#' => ++$row, |
||
| 85 | 'Task' => $event->getDescription(), |
||
| 86 | 'Expression' => $event->getExpression(), |
||
| 87 | 'Command to Run' => $event->command, |
||
| 88 | ]; |
||
| 89 | } |
||
| 90 | |||
| 91 | $climate->table($data); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Import schedule file |
||
| 96 | * |
||
| 97 | * @throws InvalidConfigException |
||
| 98 | */ |
||
| 99 | protected function importScheduleFile() |
||
| 100 | { |
||
| 101 | $scheduleFile = Yii::getAlias($this->scheduleFile); |
||
| 102 | |||
| 103 | if (!file_exists($scheduleFile)) { |
||
| 104 | throw new InvalidConfigException("Can not load schedule file {$this->scheduleFile}"); |
||
| 105 | } |
||
| 106 | |||
| 107 | $schedule = $this->schedule; |
||
| 108 | call_user_func(function () use ($schedule, $scheduleFile) { |
||
| 109 | include $scheduleFile; |
||
| 110 | }); |
||
| 111 | } |
||
| 112 | } |
||
| 113 |