时间:2021-07-01 10:21:17 帮助过:46人阅读
function onWorkerStart(swoole_server $serv, $worker_id)
{
$this->loger->write_log('info',"Work_id " . $worker_id . " start \n");
//如果当前运行的进程是task进程
if($worker_id < $serv->setting['worker_num'])
{
//开始执行任务
$serv->task($worker_id);
}
//添加定时器 只在第一个worker启动时创建一个定时器 来定时检索有没有要推送的消息
if($worker_id == 0)
{
//创建定时器
$timer = $serv->tick(3000,function ($timer_id){
$db = new DBO(config_item('mall_db_config'));
//获取所有未发送的推送消息
$query_param = array('limit'=>100,'order'=>'createon asc','where'=>'msg_status = 0');
$msgs = $db->select('t_msg_push',
array('id','mall_id','member_id','vpl','in_time','out_time','park_code',
'pay_points','park_duration','residual_points','msg_type'),
$query_param
);
if(!empty($msgs))
{
push_msg($msgs,$db);
}
else
{
simple_log('No message to send.');
}
//关闭数据库连接
$db->close();
});
}
}
1.使用swoole的tick创建了定时器,每3s去数据库查询需要发送的信息并进行发送,但是运行几天后定时器就不跑了,定时器是在启动worker进程时
function onWorkerStart(swoole_server $serv, $worker_id)
{
$this->loger->write_log('info',"Work_id " . $worker_id . " start \n");
//如果当前运行的进程是task进程
if($worker_id < $serv->setting['worker_num'])
{
//开始执行任务
$serv->task($worker_id);
}
//添加定时器 只在第一个worker启动时创建一个定时器 来定时检索有没有要推送的消息
if($worker_id == 0)
{
//创建定时器
$timer = $serv->tick(3000,function ($timer_id){
$db = new DBO(config_item('mall_db_config'));
//获取所有未发送的推送消息
$query_param = array('limit'=>100,'order'=>'createon asc','where'=>'msg_status = 0');
$msgs = $db->select('t_msg_push',
array('id','mall_id','member_id','vpl','in_time','out_time','park_code',
'pay_points','park_duration','residual_points','msg_type'),
$query_param
);
if(!empty($msgs))
{
push_msg($msgs,$db);
}
else
{
simple_log('No message to send.');
}
//关闭数据库连接
$db->close();
});
}
}