-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConsoleController.php
157 lines (142 loc) · 4.94 KB
/
ConsoleController.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
<?php
/**
* @package yii2-scheduler
* @author Miro Hudak <mhudak@dev.enscope.com>
* @copyright Copyright © Miro Hudak, enscope.com, 2016
* @version 1.0
*/
namespace enscope\Scheduler
{
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\console\Controller;
class ConsoleController
extends Controller
{
public $defaultAction = 'run-tasks';
/**
* @var bool Suppress stdout() messages
*/
public $quiet = false;
/**
* @var bool Allow verbose stdout(string, true) messages
*/
public $verbose = false;
/**
* Definition of scheduled tasks, which must be an array
* or configurations or instances of ScheduledTaskInterface.
*
* @var ScheduledTaskInterface[]
*/
public $tasks = [];
/**
* @var bool If set, all tasks are forced to execute regardless of schedule
*/
public $force = false;
/**
* Returns the names of valid options for the action (id)
* An option requires the existence of a public member variable whose
* name is the option name.
* Child classes may override this method to specify possible options.
*
* Note that the values setting via options are not available
* until [[beforeAction()]] is being called.
*
* @param string $actionID the action id of the current request
*
* @return array the names of the options valid for the action
*/
public function options($actionID)
{
switch ($actionID)
{
case 'run-tasks':
return ['force', 'verbose', 'quiet'];
}
return [];
}
/**
* Initializes the object.
* This method is invoked at the end of the constructor after the object is initialized with the
* given configuration.
*/
public function init()
{
parent::init();
$this->initializeTasks();
}
/**
* Executes scheduled tasks which are scheduled for current date and time.
*/
public function actionRunTasks()
{
// this time is passed to scheduled tasks
// and all tasks should base schedule satisfaction
// on this time
$taskTime = date_create();
$this->stdout("Executing scheduled tasks...\n", true, true);
foreach ($this->tasks as $scheduledTask)
{
try
{
if ($scheduledTask->execute($taskTime, $this->force))
{
$this->stdout("{$scheduledTask}: Task executed.\n", true, true);
}
}
catch (TerminateScheduleException $tse)
{
// this is the only schedule exception, that will terminate the scheduler
$this->stderr("SCHEDULER TERMINATED: {$scheduledTask}: {$tse->getMessage()}\n");
break;
}
catch (Exception $ex)
{
$this->stderr("ERROR: {$scheduledTask}: {$ex->getMessage()}\n");
}
}
$this->stdout("Scheduled tasks completed.\n", true, true);
}
protected function initializeTasks()
{
foreach ($this->tasks as $index => $config)
{
if ($config instanceof ScheduledTaskInterface)
{
// skip already initialized tasks
continue;
}
if (!(($this->tasks[$index] = \Yii::createObject($config)) instanceof ScheduledTaskInterface))
{
throw new InvalidConfigException('Scheduled task must implement ScheduledTaskInterface.');
}
}
}
/**
* Overridden method honors $quiet option to disable
* all non-error output.
*
* @param string $string
* @param bool $verbose If TRUE, message is considered as verbose
* @param string|bool $timestamp If set, messages are timestamped by specified format or default format
*
* @return bool|int|string
*/
public function stdout($string, $verbose = false, $timestamp = false)
{
if (!$verbose
|| $this->verbose
)
{
if ($timestamp)
{
$timestamp = is_string($timestamp) ? $timestamp : 'd-m-Y H:i:s';
$currentTime = date_create();
$string = "[{$currentTime->format($timestamp)}] {$string}";
}
return !$this->quiet ? parent::stdout($string) : '';
}
return ('');
}
}
}