在Event.php里的onWorkerStart调用定时类
public static function onWorkerStart($worker)
{
//进程启动开启定时任务
$task = new Task;
$task->start();
}
新建Task.php
<?php
namespace Applications;
use Events;
use GatewayWorker\Lib\Gateway;
use Workerman\Lib\Timer;
use \Workerman\Worker;
class Task
{
public function start()
{
$task = new Worker();
// 开启多少个进程运行定时任务,注意多进程并发问题
$task->count = 1;
$this->timeTask();
// 运行worker
Worker::runAll();
}
public function timeTask(){
//定时任务
//参数1 循环时间(秒)一次
//参数2 命名空间到类
//参数三 任务方法
//参数四 方法传入参数
//参数五 是否循环一次就停止此定时器 true 一直循环 false 循环一次就停止
Timer::add(参数1, array(参数2,参数3),参数4,参数5);
Timer::add(3600, array('\Applications\controller\ChatContent', 'moveChatContent'), [], false);
}
}