Home >Backend Development >PHP Tutorial >The swoole timer is automatically closed for no apparent reason, but all processes are running normally?

The swoole timer is automatically closed for no apparent reason, but all processes are running normally?

WBOY
WBOYOriginal
2016-08-04 09:21:482021browse

1. Use swoole's tick to create a timer. Every 3 seconds, it goes to the database to query the information that needs to be sent and sends it. However, the timer stops running after running for a few days. The timer is started when the worker process is started

<code>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();
            });
        }
    }</code>

Reply content:

1. Use swoole's tick to create a timer. Every 3 seconds, it goes to the database to query the information that needs to be sent and sends it. However, the timer stops running after running for a few days. The timer is started when the worker process is started

<code>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();
            });
        }
    }</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:ecshop smarty templateNext article:ecshop smarty template