swool 安装IDE自动提示工具 ide helper(含使用方法和注意事项)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lw545034502/article/details/93646546

安装swoole扩展  请查看本人另一篇博客地址:

https://blog.csdn.net/lw545034502/article/details/91379001

官方文档:

https://wiki.swoole.com/

IDE自动提示工具(自动生成版) :

git clone https://github.com/swoole/ide-helper.git

使用:

 //必须在协程中运行:
 go(function() use ($class){
    //这里写代码逻辑,注意不能用exit或者die打断点  只能抛出错误,有抛错也要有接收错误,如:使用try catch

 }));


 //在外面用下面代码,等待swoole全部执行完再退出
 \swoole_event::wait();

下面贴出我实际工作用到的代码 (执行定时任务):

public function startAction(){
        /*if($this->existDoWorkLock('AutoTask')){
            return;
        }*/
        $repos   = Repositories::getRepository('SysTask');
        $tasks   = $repos->getActiveTask();
        $endTime = time() + 59;
        $this->debug('开始autotask');
        do{
            try{
                //$this->debug('开始任务循环');
                foreach($tasks as $task){
                    if(!$task->class_name){
                        continue;
                    }
                    $className  = $this->getClassName($task->class_name);
                    $expireTime = $task->repeat_time > 1 ? $task->repeat_time - 1 : 59;
                    if($this->existDoWorkLock($className, $expireTime)){
                        /*if($task->class_name == 'OperatorReportTask'){
                            $this->debug('任务:' . $task->class_name . '在' . $expireTime . '秒内已经运行了。');
                        }*/
                        continue;
                    }
                    list($state, $class) = $this->loadClass($task->class_name);
                    if(!$state || !$class){
                        $info = '任务:' . $task->name . ',类:' . $className . '不存在';
                        $this->debug($info);
                        $this->addLog($task->id, $task->name, $info, SysTaskLog::TYPE_ERROR);
                        $repos->setTaskState($task->id, SysTask::STATE_STOP);
                        continue;
                    }
                    if(!($class instanceof TaskBase)){
                        $info = '任务:' . $task->name . ',类:' . $className . '非自动任务';
                        $this->debug($info);
                        $this->addLog($task->id, $task->name, $info, SysTaskLog::TYPE_ERROR);
                        $repos->setTaskState($task->id, SysTask::STATE_STOP);
                        continue;
                    }
                    $class->task       = $task;
                    $class->expireTime = $expireTime;
                    if(!$class->checkRunTime()){
                        //$this->debug('任务:' . $task->name . '不到执行的时间');
                        continue;
                    }
                    if($task->type == SysTask::TYPE_TIMING){
                        $info = '开始运行任务:' . $class->task->name . '。';
                        $this->addCacheInfo($class->task->id, $info, SysTaskLog::TYPE_INFO);
                    }
                    $this->debug('开始运行任务:' . $class->task->name . '。');
                    go(function() use ($class){
                        try{
                            list($state, $msg) = $class->start();
                            if(!$state){
                                $info = '任务:' . $class->task->name . ',类:' . $class->task->class_name . '运行失败。错误信息:' . $msg;
                                $this->debug($info);
                                $class->addLog($class->task->id, $class->task->name, $info, SysTaskLog::TYPE_ERROR);
                                return;
                            }
                            $info = '任务:' . $class->task->name . '运行完成。';
                            if($class->task->type == SysTask::TYPE_TIMING){
                                $class->addLog($class->task->id, $class->task->name, $info, SysTaskLog::TYPE_INFO);
                                $this->debug($info);
                                //writeLog(RUNTIME_PATH . '/logs/autotask_' . date('Y-m-d') . '.log', $info);
                            }else{
                                //writeLog(RUNTIME_PATH . '/logs/autotask_' . date('Y-m-d') . '.log', $info);
                                $this->debug($info);
                                $this->addCacheInfo($class->task->id, $info, SysTaskLog::TYPE_INFO);
                            }
                        }catch(\Exception $e){
                            $info = '任务:' . $class->task->name . '运行错误。错误信息:' . $e->getMessage();
                            $this->debug($info);
                            writeLog(RUNTIME_PATH . '/logs/autotask_' . date('Y-m-d') . '.log', $info);
                            $class->addLog($class->task->id, $class->task->name, $info, SysTaskLog::TYPE_ERROR);
                        }
                    });
                }
            }catch(\Exception $e){
                $info = '任务:运行错误。错误信息:' . $e->getMessage();
                $this->debug($info);
                writeLog(RUNTIME_PATH . '/logs/autotask_' . date('Y-m-d') . '.log', $info);
            }
            if(time() > $endTime){
                $this->debug('time:' . time() . ',endTime:' . $endTime);
                break;
            }
            usleep(50000);
        }while(true);
        $this->debug('重新发布系统事件');
        try{
            $this->republish();
        }catch(\Exception $e){
            $this->debug('重新发布事件错误,错误信息' . json_encode($e));
        }

        \swoole_event::wait();
        $this->debug('autotask 退出');
    }

 

展开阅读全文

没有更多推荐了,返回首页