CrontabJob.php
4.99 KB
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
<?php
namespace common\components\crontabs;
use InvalidArgumentException;
use function preg_match;
use function sprintf;
/**
* 一个任务(crontab)的实例本身
* Class CrontabJob
* $commond = '* * * * * sleep 48; cd /var/www/html/mk-hyk; php yii crontab/notification >/dev/null 2>&1 & #notification-48';
* 调用createFromCrontabLine($commond)之后的结果:
* [
* [enabled] => 1
* [minutes] => *
* [hours] => *
* [dayOfMonth] => *
* [months] => *
* dayOfWeek] => *
* [taskCommandLine] => sleep 48; cd /var/www/html/qiaodangjia; php yii crontab/notification >/dev/null 2>&1 &
* [comments] => notification-48
* [shortCut] =>
* ]
* @package common\components\crontabs
*/
class CrontabJob
{
/**
* 任务是否启用
* 禁用的状态会在命令前面加 # 符号
* @var boolean
*/
public $enabled = true;
/**
* 分钟 (0 - 59)
* @var string/int
*/
public $minutes;
/**
* 小时 (0 - 23)
* @var string/int
*/
public $hours;
/**
* 天 (1 - 31)
* @var string/int
*/
public $dayOfMonth;
/**
* 月 (1 - 12)
* @var string/int
*/
public $months;
/**
* 周一到周日,0-6,或者使用名字
* @var tring/int
*/
public $dayOfWeek;
/**
* 要被执行的任务命令行
* @var string
*/
public $taskCommandLine;
/**
* 可选注释,会被放在任务行的最后#符号后面
* @var string
*/
public $comments;
/**
* @var string
*/
public $shortCut;
/**
* 通过任务命令行创建CrontabJob对象的工厂方法
*
* @param string $crontabLine
* @throws InvalidArgumentException
* @return CrontabJob
*/
public static function createFromCrontabLine($crontabLine)
{
// Check crontab line format validity
$crontabLineRegex = '/^[\s\t]*(#)?[\s\t]*(([*0-9,-\/]+)[\s\t]+([*0-9,-\/]+)'
. '[\s\t]+([*0-9,-\/]+)[\s\t]+([*a-z0-9,-\/]+)[\s\t]+([*a-z0-9,-\/]+)|'
. '(@(reboot|yearly|annually|monthly|weekly|daily|midnight|hourly)))'
. '[\s\t]+([^#]+)([\s\t]+#(.+))?$/'
;
if (!preg_match($crontabLineRegex, $crontabLine, $matches)) {
throw new InvalidArgumentException(
'无效的Crontab命令行, 解析失败!'
);
}
// Create the job from parsed crontab line values
$crontabJob = new self();
if (!empty($matches[1])) {
$crontabJob->enabled = false;
} else {
$crontabJob->enabled = true;
}
if (!empty($matches)) {
$crontabJob->minutes = $matches[3];
$crontabJob->hours = $matches[4];
$crontabJob->dayOfMonth = $matches[5];
$crontabJob->months = $matches[6];
$crontabJob->dayOfWeek = $matches[7];
}
if (!empty($matches[8])) {
$crontabJob->shortCut = $matches[9];
}
$crontabJob->taskCommandLine = $matches[10];
if (!empty($matches[12])) {
$crontabJob->comments = $matches[12];
}
return $crontabJob;
}
/**
* 把一个CrontabJob对象实例格式化为一个可供Linux系统的任务命令行
*
* @throws InvalidArgumentException
* @return String
*/
public function formatCrontabLine()
{
// Check if job has a task command line
if (!isset($this->taskCommandLine) || empty($this->taskCommandLine)) {
throw new InvalidArgumentException(
'CrontabJob contain\'s no task command line'
);
}
$taskPlanningNotation = (isset($this->shortCut) && !empty($this->shortCut))
? sprintf('@%s', $this->shortCut)
: sprintf(
'%s %s %s %s %s',
(isset($this->minutes) ? $this->minutes : '*'),
(isset($this->hours) ? $this->hours : '*'),
(isset($this->dayOfMonth) ? $this->dayOfMonth : '*'),
(isset($this->months) ? $this->months : '*'),
(isset($this->dayOfWeek) ? $this->dayOfWeek : '*')
)
;
return sprintf(
'%s%s %s%s',
($this->enabled ? '' : '#'),
$taskPlanningNotation,
$this->taskCommandLine,
(isset($this->comments) ? (' #' . $this->comments) : '')
);
}
/**
* Format the CrontabJob time format string to: * * * * *
*
* @throws InvalidArgumentException
* @return String
*/
public function formatCrontabTime()
{
return sprintf(
'%s %s %s %s %s',
(isset($this->minutes) ? $this->minutes : '*'),
(isset($this->hours) ? $this->hours : '*'),
(isset($this->dayOfMonth) ? $this->dayOfMonth : '*'),
(isset($this->months) ? $this->months : '*'),
(isset($this->dayOfWeek) ? $this->dayOfWeek : '*')
);
}
}