代码基本上是仿照文档写的。
$room_config=array();
$task = new Worker();
$task->count = 1;
$task->onWorkerStart = function($task)
{
global $room_config;
ReloadRoomConfig();
//定时检测房间设置
$check_room_interval = 30;
Timer::add($check_room_interval, function() use (&$room_config)
{
info('ReLoad Room Setting ...');
ReloadRoomConfig();
});
//聊天机器人
Timer::add(1,function() use (&$room_config)
{
//处理随机聊天业务
});
};
Worker::runAll();
调试模式很好,但是用-d模式日志一直在增长,不知道为什么。
#tail -f workman.log
2018-06-19 01:04:19 pid:30443 Worker process terminated
2018-06-19 01:04:19 pid:30444 Worker process terminated
2018-06-19 01:04:19 pid:30445 Worker process terminated
2018-06-19 01:04:19 pid:30446 Worker process terminated
2018-06-19 01:04:19 pid:30447 Worker process terminated
2018-06-19 01:04:19 pid:30448 Worker process terminated
2018-06-19 01:04:19 pid:30449 Worker process terminated
2018-06-19 01:04:19 pid:30450 Worker process terminated
2018-06-19 01:04:19 pid:30451 Worker process terminated
不断循环...
数据库 redis等资源在onWorkerStart里初始化,不要在启动脚本里直接初始化。
OK,果然放到onWorkerStart就没问题了,谢谢@walkor