forked from Yiivgeny/Yii-PHPDocCrontab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPHPDocCrontab.php
331 lines (305 loc) · 11.4 KB
/
PHPDocCrontab.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/**
* Yii Framework extension. Better installing console commands as cron jobs.
*
* @author Evgeny Blinov <[email protected]>
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
/**
* PHPDocCrontab is a CConsoleCommand to automaticly running marked actions.
*
* @author Evgeny Blinov <[email protected]>
* @package PHPDocCrontab
*/
class PHPDocCrontab extends CConsoleCommand {
/**
* @var string PHPDoc tag prefix for using by PHPDocCrontab extension.
*/
public $tagPrefix = 'cron';
/**
* @var string PHP interpriter path (if empty, path will be checked automaticly)
*/
public $interpreterPath = null;
/**
* @var string path to writing logs
*/
public $logsDir = null;
/**
* Placeholders:
* %L - logsDir path
* %C - name of command
* %A - name of action
* %P - pid of runner-script (current)
* %D(string formatted as arg of date() function) - formatted date
* @var string mask log file name
*/
public $logFileName = '%L/%C.%A.log';
/**
* @var string Bootstrap script path (if empty, current command runner will be used)
*/
public $bootstrapScript = null;
/**
* @var string Timestamp used as current datetime
* @see http://php.net/manual/en/function.strtotime.php
*/
public $timestamp = 'now';
/**
* @var string the name of the default action. Defaults to 'run'.
*/
public $defaultAction = 'run';
/**
* Initialize empty config parameters.
*/
public function init() {
parent::init();
//Checking PHP interpriter path
if ($this->interpreterPath === null){
if ($this->isWindowsOS()){
//Windows OS
$this->interpreterPath = 'php.exe';
}
else{
//nix based OS
$this->interpreterPath = '/usr/bin/env php';
}
}
//Checking logs directory
if ($this->logsDir === null){
$this->logsDir = Yii::app()->getRuntimePath();
}
$this->logsDir = Yii::getPathOfAlias($this->logsDir);
if ( !file_exists($this->logsDir)) {
Yii::log("Log dir doesn't exists, trying create", CLogger::LEVEL_WARNING, 'ext.'.__CLASS__);
if(!mkdir($this->logsDir))
{
Yii::log("Can't create logdir: {$this->logsDir}", CLogger::LEVEL_WARNING, 'ext.'.__CLASS__);
}
}
//Checking bootstrap script
if ($this->bootstrapScript === null){
$this->bootstrapScript = realpath($this->getCommandRunner()->getScriptName());
}
}
/**
* Provides the command description.
* @return string the command description.
*/
public function getHelp() {
$commandUsage = $this->getCommandRunner()->getScriptName().' '.$this->getName();
return <<<RAW
Usage: {$commandUsage} <action>
Actions:
view <tags> - Show active tasks, specified by tags.
run <options> <tags> - Run suitable tasks, specified by tags (default action).
help - Show this help.
Tags:
[tag1] [tag2] [...] [tagN] - List of tags
Options:
[--tagPrefix=value]
[--interpreterPath=value]
[--logsDir=value]
[--logFileName=value]
[--bootstrapScript=value]
[--timestamp=value]
RAW;
}
/**
* Transform string datetime expressions to array sets
*
* @param array $parameters
* @return array
*/
protected function transformDatePieces(array $parameters){
$dimensions = array(
array(0,59), //Minutes
array(0,23), //Hours
array(1,31), //Days
array(1,12), //Months
array(0,6), //Weekdays
array(0,59), // Seconds
);
foreach ($parameters AS $n => &$repeat) {
list($repeat, $every) = explode('\\', $repeat, 2) + array(false, 1);
if ($repeat === '*') $repeat = range($dimensions[$n][0], $dimensions[$n][1]);
else {
$repeatPiece = array();
foreach (explode(',', $repeat) as $piece) {
$piece = explode('-', $piece, 2);
if (count($piece) === 2) $repeatPiece = array_merge($repeatPiece, range($piece[0], $piece[1]));
else $repeatPiece[] = $piece[0];
}
$repeat = $repeatPiece;
}
if ($every > 1) foreach ($repeat AS $key => $piece){
if ($piece%$every !== 0) unset($repeat[$key]);
}
}
for($i = count($parameters); $i < count($dimensions); ++$i)
{
$parameters[$i] = range($dimensions[$i][0], $dimensions[$i][1]);
}
return $parameters;
}
/**
* Parsing and filtering PHPDoc comments.
*
* @param string $comment Raw PHPDoc comment
* @return array List of valid tags
*/
protected function parseDocComment($comment){
if (empty($comment)) return array();
//Forming pattern based on $this->tagPrefix
$pattern = '#^\s*\*\s+@('.$this->tagPrefix.'(-(\w+))?)\s*(.*?)\s*$#im';
//Miss tags:
//cron, cron-tags, cron-args, cron-strout, cron-stderr
if (preg_match_all($pattern, $comment, $matches, PREG_SET_ORDER)){
foreach ($matches AS $match)
$return[ $match[3]?$match[3]:0 ] = $match[4];
if (isset($return[0])){
$return['_raw'] = preg_split('#\s+#', $return[0], 6);
$return[0] = $this->transformDatePieces($return['_raw']);
//Getting tag list. If empty, string "default" will be used.
$return['tags'] = isset($return['tags'])?preg_split('#\W+#', $return['tags']):array('default');
return $return;
}
}
}
/**
* Getting tasklist.
*
* @return array List of command actions associated with {@link PHPDocCrontab} runner.
*/
protected function prepareActions(){
$actions = array();
$Runner = $this->getCommandRunner();
// Command loop
foreach ($Runner->commands AS $command => $file){
$CommandObject = $Runner->createCommand($command);
if ($CommandObject instanceof $this) continue;
$Reflection = new ReflectionObject($CommandObject);
// Methods loop
$Methods = $Reflection->getMethods(ReflectionMethod::IS_PUBLIC);
foreach ($Methods AS $Method){
$name = $Method->getName();
//Filetring methods. Valid only public actions.
if(
!strncasecmp($name,'action',6) &&
strlen($name) > 6 &&
($docComment = $this->parseDocComment($Method->getDocComment()))
){
$name=substr($name, 6);
$name[0]=strtolower($name[0]);
$actions[] = array(
'command' => $command,
'action' => $name,
'docs' => $docComment
);
}
}
}
return $actions;
}
/**
* OS-independent background command execution .
*
* @param string $command
* @param string $stdout path to file for writing stdout
* @param string $stderr path to file for writing stderr
*/
protected function runCommandBackground($command, $stdout, $stderr){
$command =
$this->interpreterPath.' '.
$command.
' >>'.escapeshellarg($stdout).
' 2>'.(($stdout === $stderr)?'&1': ('>' . escapeshellarg($stderr)));
if ($this->isWindowsOS()){
//Windows OS
pclose(popen('start /B "Yii run command" '.$command, 'r'));
}
else{
//nix based OS
system($command.' &');
}
}
/**
* Checking is windows family OS
*
* @return boolean return true if script running under windows OS
*/
protected function isWindowsOS(){
return strncmp(PHP_OS, 'WIN', 3) === 0;
}
/**
* Running actions associated with {@link PHPDocCrontab} runner and matched with timestamp.
*
* @param array $args List of run-tags to running actions (if empty, only "default" run-tag will be runned).
*/
public function actionRun($args = array()){
$tags = &$args;
if(count($tags) == 0)
{
$tags[] = 'default';
}
//Getting timestamp will be used as current
$time = strtotime($this->timestamp);
if ($time === false) throw new CException('Bad timestamp format');
$now = explode(' ', date('i G j n w s', $time));
$runned = 0;
foreach ($this->prepareActions() as $task) {
if (array_intersect($tags, $task['docs']['tags'])){
foreach ($now AS $key => $piece){
//Checking current datetime on timestamp piece array.
if (!in_array($piece, $task['docs'][0][$key])) continue 2;
}
//Forming command to run
$command = $this->bootstrapScript.' '.$task['command'].' '.$task['action'];
if (isset($task['docs']['args'])) $command .= ' '.escapeshellcmd($task['docs']['args']);
//Setting default stdout & stderr
if (isset($task['docs']['stdout'])) $stdout = $task['docs']['stdout'];
else $stdout = $this->logFileName;
$stdout = $this->formatFileName($stdout, $task);
$stderr = isset($task['docs']['stderr'])?$this->formatFileName($task['docs']['stderr'], $task):$stdout;
$this->runCommandBackground($command, $stdout, $stderr);
Yii::log('Running task ['.(++$runned).']: '.$task['command'].' '.$task['action'], CLogger::LEVEL_INFO, 'ext.'.__CLASS__);
}
}
if ($runned > 0){
Yii::log('Runned '.$runned.' task(s) at '.date('r', $time), CLogger::LEVEL_INFO, 'ext.'.__CLASS__);
}
else{
Yii::log('No task on '.date('r', $time), CLogger::LEVEL_INFO, 'ext.'.__CLASS__);
}
}
/**
* Show actions associated with {@link PHPDocCrontab} runner.
*
* @param $args array List of run-tags for filtering action list (if empty, show all).
*/
public function actionView($args = array()){
$tags = &$args;
foreach ($this->prepareActions() as $task) {
if (!$tags || array_intersect($tags, $task['docs']['tags'])){
//Forming to using with printf function
$times = $task['docs']['_raw'];
array_unshift($times, $task['command'].'.'.$task['action']);
array_unshift($times, "Action %-40s on %6s %6s %6s %6s %6s %s\n");
array_push($times, empty($task['docs']['tags'])?'':(' ('.implode(', ', $task['docs']['tags']).')'));
call_user_func_array('printf', $times);
}
}
}
protected function formatFileName($pattern, $task){
$pattern = str_replace(
array('%L', '%C', '%A', '%P'),
array($this->logsDir, $task['command'], $task['action'], getmypid()),
$pattern
);
return preg_replace_callback('#%D\((.+)\)#U', create_function('$str', 'return date($str[1]);'), $pattern);
}
/**
* Help command. Show command usage.
*/
public function actionHelp(){
echo $this->getHelp();
}
}