<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ~~~ <?php /** * Created by PhpStorm. * User: Mikkle * QQ:776329498 * Date: 2018/1/19 * Time: 10:10 */ namespace mikkle\tp_worker; use mikkle\tp_master\Db; use mikkle\tp_master\Exception; use mikkle\tp_master\Log; use mikkle\tp_tools\Time; abstract class WorkerHookBase { protected $listName; protected $redis; protected $workList; protected $workerName; public static $instance; protected $connect =[] ; protected $saveLog=false; protected $tableName = "mk_log_service_queue"; protected $error; /** * Base constructor. * @param array $options */ public function __construct($options=[]) { $this->redis = $this->redis(); $this->workList = "worker_list"; $this->workerName = get_called_class(); $this->listName = md5($this->workerName); $this->_initialize(); } public function _initialize(){ } public function run(&$prams,$options){ self::add( $prams,$options); } abstract protected function runHandle ($data); /** * @title redis * @description redis加載自定義Redis類 * User: Mikkle * QQ:776329498 * @param array $options * @return \mikkle\tp_redis\Redis */ protected static function redis(){ return WorkerRedis::instance(); } /** * @title runWorker * @description 標注命令行執行此任務 * User: Mikkle * QQ:776329498 * @param string $handleName */ public function runWorker($handleName="workRunFunction"){ $this->redis->hset($this->workList,$this->workerName,$handleName); } /** * 標注命令行清除此任務 * Power: Mikkle * Email:776329498@qq.com */ public function clearWorker(){ $this->redis->hdel($this->workList,$this->workerName); } /** * Power: Mikkle * Email:776329498@qq.com * @param array $options * @return static */ static public function instance($options=[]){ if (isset(static::$instance)){ return static::$instance; }else{ return new static($options); } } /** * * 快速添加模版消息任務 * * 當命令行未運行 直接執行 * description add * User: Mikkle * QQ:776329498 * @param $data * @param array $options * @param string $handleName * @return bool */ static public function add($data,$options=[],$handleName="workRunFunction"){ try{ $data=json_encode($data); $instance = static::instance($options); switch (true){ case (self::checkCommandRun()): $instance->redis->lpush($instance->listName,$data); $instance->runWorker($handleName); Log::notice("Command service start work!!"); break; default: Log::notice("Command service No away!!"); $instance->runHandle($data); } return true; }catch (Exception $e){ Log::error($e->getMessage()); return false; } } /** * 命令行執行的方法 * Power: Mikkle * Email:776329498@qq.com */ static public function workRunFunction(){ $instance = static::instance(); try{ $i = 0; while(true){ $redisData= $instance->redis->rpop($instance->listName); $data = json_decode($redisData,true); if ($data){ $result = $instance->runHandle($data); if ($instance->saveLog){ $instance->saveRunLog($result,$data); } }else{ $instance->clearWorker(); break; } $i++; sleep(1); } echo "執行了{$i}次任務".PHP_EOL; }catch (Exception $e){ Log::error($e->getMessage()); echo ($e->getMessage()); } } /** * 檢測命令行是否執行中 * Power: Mikkle * Email:776329498@qq.com * @return bool */ static public function checkCommandRun(){ return self::redis()->get("command") ? true :false; } public function getError(){ if (is_array($this->error )){ return json_encode( $this->error ); } return $this->error; } /* * 檢查是注重某些值是非為空 */ protected function checkArrayValueEmpty($array,$value,$error=true){ switch (true){ case (empty($array)||!is_array($array)): if ($error==true){ $this->addError("要檢測的數據不存在或者非數組"); } return false; break; case (is_array($value)): foreach ($value as $item){ if (!isset($array[$item]) || (empty($array[$item]) && $array[$item]!==0)){ if ($error==true) { $this->addError("要檢測的數組數據有不存在鍵值{$item}"); } return false; } } break; case (is_string($value)): if (!isset($array[$value]) || empty($array[$value] && $array[$value]!==0)){ if ($error==true) { $this->addError("要檢測的數組數據有不存在鍵值{$value}"); } return false; } break; default: } return true; } public function addError($error){ $this->error = is_string($error) ? $error : json_encode($error); } protected function saveRunLog($result,$data){ try{ $operateData = [ "class" => $this->workerName, "args" => json_encode($data), "result"=> $result ? "true":"false", "error" => $this->error ? $this->getError() : null, "time" => Time::getDefaultTimeString(), ]; Db::connect($this->connect)->table($this->tableName)->insert($operateData); }catch (Exception $e){ Log::error($e->getMessage()); } } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看