zoukankan      html  css  js  c++  java
  • laravel自定义任务队列

    > php artisan make:command Dir/Example

    <?php
    
    namespace AppConsoleCommandsDir;
    
    use IlluminateConsoleCommand;
    use IlluminateSupportFacadesDB;
    use IlluminateSupportFacadesRedis;
    
    class Example extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'example';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = '任务描述';
    
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
            $this->rdb = Redis::connection();
            $this->db = DB::connection();
        }
    
        /**
         * Execute the console command.
         *
         * @return mixed
         */
        public function handle()
        {
            //启动log
            $this->setLog('beginning!~');//启动日志
            //持续运行
            while (true) {
                if (!$data = $this->getData()) {
                    eval(self::BLOCK);
                }
                try {
                    //核心函数
                    $this->action($data);
                } catch (Exception $exception) {
                    $err_msg = json_encode(['line' => $exception->getLine(), 'msg' => $exception->getMessage(), 'file' => $exception->getFile()], JSON_UNESCAPED_UNICODE);
                    $this->setLog($err_msg);//录错
                    eval(self::BLOCK);
                }
            }
        }
    
        /**
         * User:cyq
         * @param $data
         * Comment:核心函数
         */
        private function action($data){
            //逻辑操作处理
        }
    
        /**
         * User:cyq
         * Comment:可以根据mysql or redis 获取待处理操作的数据
         */
        private function getData()
        {
            //redis
    //        return $this->rdb->rpop();
            //mysql
    //        return $this->db->table()->where()->get()->toArray();
        }
    
        /**
         * User:cyq
         * Comment:记录操作日志
         */
        private function setLog($msg)
        {
    //        LogUtil::LogDiyInfo($msg, 'Dir/exampleLog');
            //写入此类的日志文件
        }
    
        /** 阻塞 */
        const BLOCK = "sleep(self::SLEEP_WAIT);continue;";
        private $rdb;//redis
        private $db;//mysql
        const SLEEP_WAIT = 5;//任务列表休眠时间
    }
  • 相关阅读:
    自执行匿名函数
    jQuery Ajax 实例 ($.ajax、$.post、$.get)
    iframe的滚动条问题:显示/隐藏滚动条
    选择器的整理
    html标记
    Axure RP
    苹果官方人机交互指南中明确定义了应用中需要包括的图标和启动画面图片
    tableview_nav 动画效果
    WebView加载HTML图片大小自适应与文章自动换行
    iOS App创建桌面快捷方式
  • 原文地址:https://www.cnblogs.com/cyq632694540/p/14663333.html
Copyright © 2011-2022 走看看