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

安装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 退出');
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值