CrontabsGroupComponent.php
8.07 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
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
<?php
namespace common\components\crontabs;
use yii\base\Component;
use yii\base\InvalidValueException;
use function dirname;
use function count;
use function md5;
use function popen;
use function fread;
use function pclose;
/**
* $repository = new CrontabRepository(new CrontabAdapter);
* $crontabs = file(__DIR__.DIRECTORY_SEPARATOR.'crontabs.php');
* foreach ($crontabs as $crontab){
* $repository->addJob(CrontabJob::createFromCrontabLine($crontab));
* }
* $jobs = $repository->findJobByRegex('/(.s)*\#close-order/');
* print_r($jobs);
*
* Class CrontabsGroupComponent
* @package common\components
*/
class CrontabsGroupComponent extends Component
{
private $root = '/var/www/html';
private $output = '/dev/null';//
private $groupNameRegex = '(.s)*\#';
private $phpYiiCommand = 'php yii';
private $cdCommand = 'cd';
private $crontabRepository;
public function init()
{
$this->root = dirname(dirname(dirname(__DIR__))).DIRECTORY_SEPARATOR;
$this->crontabRepository = new CrontabRepository(new CrontabAdapter);
}
/**
* 通过任务命令行添加CrontabJob任务
*/
public function addJobByCrontabLine($crontabLine)
{
$crontabJob = CrontabJob::createFromCrontabLine($crontabLine);
$this->crontabRepository->addJob($crontabJob);
}
/**
* 根据分组添加任务
* @param $groupName
* @param $route
* @param int $groupJobsCount 一分钟内轮询多少条任务,最大60条
* @return mixed
*/
public function addJobByGroupName($crontabRootPath, $timeFormat, $groupName, $route, $groupJobsCount = 5)
{
$this->root = $crontabRootPath;
$groupJobsCount = (int)$groupJobsCount;
if($groupJobsCount > 60){
throw new InvalidValueException('Max jobs count is 60,now is '.$groupJobsCount);
}
$times = explode(' ', $timeFormat);
$sleepInterval = 60 / $groupJobsCount;
for ($i = 0; $i < $groupJobsCount; $i++) {
$crontabJob = new CrontabJob();
if (!empty($times)) {
$crontabJob->minutes = $times[0];
$crontabJob->hours = $times[1];
$crontabJob->dayOfMonth = $times[2];
$crontabJob->months = $times[3];
$crontabJob->dayOfWeek = $times[4];
}
if (0 == $i) {
$crontabJob->taskCommandLine = $this->getJobCommandLine($groupName, $route);
} else {
$sleep = $sleepInterval * $i;
$crontabJob->taskCommandLine = $this->getJobCroupCommandLine($sleep, $groupName, $route);
}
$crontabJob->comments = self::jobName($groupName, $i);
$this->crontabRepository->addJob($crontabJob);
}
return $this->crontabRepository->persist();
}
/**
* 保存所有活动任务的选项,并启动
* @return mixed
*/
public function persist()
{
return $this->crontabRepository->persist();
}
/**
* @param $groupName
* @param $route
* @param int $groupJobsCount 一分钟内轮询多少条任务,最大60条
* @return mixed
*/
public function persistByGroupName($groupName, $route, $groupJobsCount = 5)
{
$groupJobsCount = (int)$groupJobsCount;
if($groupJobsCount > 60){
throw new InvalidValueException('Max jobs count is 60,now is '.$groupJobsCount);
}
// 清除该分组的任务
$this->clearJobsByGroupName($groupName);
$sleepInterval = 60 / $groupJobsCount;
for ($i = 0; $i < $groupJobsCount; $i++) {
$crontabJob = new CrontabJob();
if (0 == $i) {
$crontabJob->taskCommandLine = $this->getJobCommandLine($groupName, $route);
} else {
$sleep = $sleepInterval * $i;
$crontabJob->taskCommandLine = $this->getJobCroupCommandLine($sleep, $groupName, $route);
}
$crontabJob->comments = self::jobName($groupName, $i);
$this->crontabRepository->addJob($crontabJob);
}
return $this->crontabRepository->persist();
}
/**
* @param $groupName
*/
public function clearJobsByGroupName($groupName)
{
$jobs = $this->findJobsByGroupName($groupName);
foreach ($jobs as $job){
$this->crontabRepository->removeJob($job);
}
return $this->crontabRepository->persist();
}
/**
* 清空定时任务
* @return mixed
*/
public function clearJobs()
{
$this->crontabRepository->clearJob();
return $this->crontabRepository->persist();
}
/**
* @param $groupName 按分组名称来查找任务
* @return array 某分组的CrontabJob数组
*/
public function findJobsByGroupName($groupName)
{
return (array)$this->crontabRepository->findJobByRegex("/{$this->groupNameRegex}{$groupName}/");
}
/**
* @param $groupName 按分组名称来查找任务
* @return array 某分组的CrontabJob数组
*/
public function countJobsByGroupName($groupName)
{
return count($this->findJobsByGroupName($groupName));
}
/**
* @param $groupName
* @param $i
* @return string
*/
private static function jobName($groupName, $i)
{
return $groupName . '-' . $i;
}
/**
* 启动进程的时候带上一个进程识别符
* 返回类似格式:
* cd /var/www/html/mk-hyk; php yii crontab/notification /var/www/html/mk-libs/wechat-notification >/dev/null 2>&1 &
* @param $sleep
*/
private function getJobCommandLine($groupName, $route)
{
// 2>&1 是把错误输出导入(合并)到标准输出流中
$processToken = $this->getProcessToken($groupName);
return "{$this->cdCommand} {$this->root}; {$this->phpYiiCommand} {$route} {$processToken} >{$this->output} 2>&1 &";
}
/**
* 进程识别符
* @param $groupName
* @return string
*/
private function getProcessToken($groupName)
{
return md5($this->root . $groupName);
}
/**
* 返回类似格式:sleep 48; cd /var/www/html/mk-hyk/; php yii crontab/notification >/dev/null 2>&1 &
* @param $sleep
* @param $route
* @return string
*/
private function getJobCroupCommandLine($sleep, $groupNamem, $route)
{
return "sleep {$sleep}; " . $this->getJobCommandLine($groupNamem, $route);
}
/**
* 返回已经启动的PHP进程,通过进程唯一识别符(process token)来获知定时任务启动的监听进程
* 注意:如果定时任务启动的不是监听程序,那么该PHP进程执行完之后就会自动销毁。
* @param $groupName 任务组名,用来识别业务
* @param $route console控制台对应的应用程序(application route)路由
* @return int 进程数量
*/
public function getJobsProcessCount($groupName, $route)
{
$jobProcessCommand = $this->getJobProcessCommand($groupName, $route);
$cmd = popen("ps -ef | {$jobProcessCommand} | wc -l", 'r');
$num = fread($cmd, 512);
pclose($cmd);
return (int)$num;
}
/**
* ps -ef | grep 'php yii worker/listen 3a34facf9ddaad970db9f13910161a4d ' | grep -v grep | grep -v '#wechat-notification' | pgrep php
* 移除(kill)掉所有对应($groupName, $route)定时任务开启的进程
* @param $groupName
* @param $route
*/
public function removeJobsProcessByGroupName($groupName, $route)
{
$jobProcessCommand = $this->getJobProcessCommand($groupName, $route);
$cmd = popen("ps -ef | {$jobProcessCommand} | pgrep php | xargs kill -s 9", 'w');
pclose($cmd);
}
/**
* 进程命令:根据($groupName, $route)匹配出对应的进程
* @param $groupName
* @param $route
* @return string
*/
private function getJobProcessCommand($groupName, $route)
{
$processToken = $this->getProcessToken($groupName);
return "grep '{$this->phpYiiCommand} {$route} {$processToken}'";
}
}