* * * * *
- - - - -
| | | | |
| | | | |
| | | | +----- day of week (0 - 7) (Sunday=0 or 7)
| | | +---------- month (1 - 12)
| | +--------------- day of month (1 - 31)
| +-------------------- hour (0 - 23)
+------------------------- min (0 - 59)
@yearly 每年一次 等同于(0 0 1 1 *)
@annually 每年一次 等同于(0 0 1 1 *)
@monthly 每月一次 等同于(0 0 1 * *)
@weekly 每周一次 等同于(0 0 * * 0)
@daily 每日一次 等同于(0 0 * * *)
@hourly 每小时一次 等同于(0 * * * *)
<?php
namespace App\Crontab;
use EasySwoole\EasySwoole\Crontab\AbstractCronTask;
class OddNumber extends AbstractCronTask
{
public static function getRule(): string
{
// TODO: Implement getRule() method.
//定时器表达式
return '1-59/2 * * * *';
}
public static function getTaskName(): string
{
// TODO: Implement getTaskName() method.
//定时任务名称
return '奇数时间运行';
}
function run(int $taskId, int $workerIndex)
{
// TODO: Implement run() method.
//定时任务处理逻辑
var_dump('奇数运行 '.date('Y-m-d H:i'));
}
function onException(\Throwable $throwable, int $taskId, int $workerIndex)
{
// TODO: Implement onException() method.
echo $throwable->getMessage();
}
}
<?php
namespace App\Crontab;
use EasySwoole\EasySwoole\Crontab\AbstractCronTask;
class EvenNumber extends AbstractCronTask
{
public static function getRule(): string
{
// TODO: Implement getRule() method.
//定时器表达式
return '0-58/2 * * * *';
}
public static function getTaskName(): string
{
// TODO: Implement getTaskName() method.
//定时任务名称
return '偶数时间运行';
}
function run(int $taskId, int $workerIndex)
{
// TODO: Implement run() method.
//定时任务处理逻辑
var_dump('偶数运行 '.date('Y-m-d H:i'));
}
function onException(\Throwable $throwable, int $taskId, int $workerIndex)
{
// TODO: Implement onException() method.
echo $throwable->getMessage();
}
}
public static function mainServerCreate(EventRegister $register)
{
// TODO: Implement mainServerCreate() method.
//奇数时间定时任务
Crontab::getInstance()->addTask(OddNumber::class);
//偶数时间定时任务
Crontab::getInstance()->addTask(EvenNumber::class);
}
php easyswoole start
即可,效果图如下本文为北溟有鱼QAQ原创文章,转载无需和我联系,但请注明来自北溟有鱼QAQ https://www.amdzz.cn
最新评论