Skip to content

Commit

Permalink
crontab支持
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Apr 12, 2018
1 parent 69b528c commit 5cd8295
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"php": ">=7.1.0",
"nikic/fast-route": "^1.2",
"nikic/php-parser": "^3.1",
"jeremeamia/SuperClosure": "^2.3"
"jeremeamia/SuperClosure": "^2.3",
"dragonmantank/cron-expression": "^2.1"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 4 additions & 3 deletions src/Core/Component/Crontab/CronTab.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ class CronTab

private $task = [];

public function addRule($rule,$task):CronTab
public function addRule(string $taskName,$rule,$task):CronTab
{
$this->task[] = [
$this->task[$taskName] = [
'rule'=>$rule,
'task'=>$task
'task'=>$task,
'isStart'=>true
];
return $this;
}
Expand Down
36 changes: 28 additions & 8 deletions src/Core/Component/Crontab/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,43 @@
namespace EasySwoole\Core\Component\Crontab;


use Cron\CronExpression;
use EasySwoole\Core\Component\Trigger;
use EasySwoole\Core\Swoole\Process\AbstractProcess;
use EasySwoole\Core\Swoole\Task\TaskManager;
use Swoole\Process;

class Runner extends AbstractProcess
{

protected $allTask = [];
public function run(Process $process)
{
// TODO: Implement run() method.
$allTask = $this->getArgs();
$this->addTick(1000,function ()use($allTask){
foreach ($allTask as $task){
//解析每个规则,判断该任务的最近执行时间,如果需要执行则投递到异步任务执行并标记已经执行。
$rule = $task['rule'];
$call = $task['task'];
TaskManager::processAsync($call);
$this->allTask = $this->getArgs();
//crontab最小执行单位是1min,因此29s检测一次,以半分钟为周期
$this->addTick(30000,function (){
$allToDo = [];
$current = time();
foreach ($this->allTask as $task){
//若相差小于30s 则投递执行
try{
$rule = $task['rule'];
$time = CronExpression::factory($rule)->getNextRunDate()->getTimestamp();
//过期任务不执行
if($time > $current && ($time - $current <= 30)){
$allToDo[] = $task['task'];

}
}catch (\Throwable $throwable){
Trigger::throwable($throwable);
}
}
if(!empty($allToDo)){
$this->delay(30000,function ()use($allToDo){
foreach ($allToDo as $task){
TaskManager::processAsync($task);
}
});
}
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Swoole/ServerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use EasySwoole\Config;
use EasySwoole\Core\Component\Cache\Cache;
use EasySwoole\Core\Component\Cluster\Cluster;
use EasySwoole\Core\Component\Crontab\CronTab;
use EasySwoole\Core\Component\Event;
use EasySwoole\Core\Component\Invoker;
use EasySwoole\Core\Component\Trigger;
Expand Down Expand Up @@ -61,6 +62,7 @@ public function start():void
$this->attachListener();
Cache::getInstance();
Cluster::getInstance()->run();
CronTab::getInstance()->run();
$this->isStart = true;
$this->getServer()->start();
}
Expand Down

0 comments on commit 5cd8295

Please sign in to comment.