• 【代码块】定时任务<swoole> 100%无人值守


    namespace app	askcontroller;
    
    abstract class Basic{
        protected $processPrefix = 'lhj-';
        public function __construct(){
            if(!IS_CLI){
                error('ERROR_MASTER')->send();exit;
            }
        }
        // 打印日志并退出
        protected function logExit($msg = 'exit'){
    
            exit;
        }
        // 发送邮件提醒开发人员
        protected function remindDeveloper($error = ''){
    
         $mail = new xalibMailer();
            $mail->send('*****@qq.com', '***错误提醒', $error);
        }
        // 重启服务
        protected function checkBoot($processName){
            // 判断该服务是否已启动,如果已启动,则 直接跳出
            if($this->serviceIsRun($processName)){
                $this->logExit("service 【{$processName}】 is running!");
            }
            $processName = $this->processPrefix.$processName;   //  添加前缀
            swoole_set_process_name($processName);
        }
        // 判断该名称服务是否已启动
        protected function serviceIsRun($processName){
            $processName = $this->processPrefix.$processName;   //  添加前缀
            $cmd = "ps -eaf |grep "{$processName}" | grep -v "grep"| awk '{print $2}'";
            $result = exec($cmd);
            return (bool)$result;
        }
    }

    业务类

    <?php
    namespace app	askcontroller;
    use thinkDb;
    use thinkView;
    class Sms extends Base{
        //  发送短信
        public function send(){
            $processName = 'sms-task';
            $this->checkBoot($processName);
            while (true){
                $task = model('Sms', 'cache')->pop();
                if($task){
                    sms($task['phone'], $task['code'], $task['param'], $task['sign']);
                }
                usleep(200);
            }
        }
    
    
    }

    使用

    crontab 业务方法定时每分钟调用一次

     亦可使用supervisor实现,不需使用crontab + 代码块实现

  • 相关阅读:
    366. Find Leaves of Binary Tree
    369. Plus One Linked List
    370. Range Addition
    411. Minimum Unique Word Abbreviation
    379. Design Phone Directory
    Permutation
    leetcode216-Combination Sum III
    百度star编程赛-练习1
    腾讯暑期实习生面试题
    素数筛选
  • 原文地址:https://www.cnblogs.com/lixingbaophp/p/9143710.html
Copyright © 2020-2023  润新知