/** * @return array The return value should include 'errno' and 'data' */ function dispatch() { $parse_url = $this->parse_url(); if ($parse_url['errno'] != self::ERRNO_OK) { return $parse_url; } $url_piece = $parse_url['data']; $router = call_user_func($this->_router, $url_piece); if (isset($router['errno']) && $router['errno'] === self::ERRNO_OK) { return ['errno' => self::ERRNO_OK, 'data' => $router]; } elseif (isset($router['errno'])) { switch ($router['errno']) { case self::ERRNO_FORBIDDEN: \Lib_Log::info('forbidden page! url:[%s] case:[%s]', [json_encode($parse_url), json_encode($router)]); return ['errno' => self::ERRNO_FORBIDDEN, 'data' => 'You cannot visit this page!']; case self::ERRNO_SERVER_ERR: \Lib_Log::error('page err! url:[%s] case:[%s]', [json_encode($parse_url), json_encode($router)]); return ['errno' => self::ERRNO_SERVER_ERR, 'data' => 'Something wrong!']; case self::ERRNO_NOT_FOUND: default: \Lib_Log::notice('page not found! url:[%s] case:[%s]', [json_encode($parse_url), json_encode($router)]); return ['errno' => self::ERRNO_NOT_FOUND, 'data' => 'PAGE NOT FOUND!']; } } else { \Lib_Log::notice('page not found! url:[%s] case:[%s]', [json_encode($parse_url), json_encode($router)]); return ['errno' => self::ERRNO_FORBIDDEN, 'data' => 'PAGE NOT FOUND!']; } }
/** * 通过curl post数据 * @param string $url * @param array $post_data * @param array $header * @param string $cookie * @param int $timeout * @param int $retry_times * @param array $curl_status * @return mixed */ public static function curlPost($url, $post_data = array(), $header = array(), $cookie = '', $timeout = 5, $retry_times = 0, &$curl_status = array()) { //print_r(array("url"=>$url, "post_data" => $post_data, "header" => $header,"cookie"=>$cookie)); $post_string = is_array($post_data) ? http_build_query($post_data) : $post_data; $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); // 模拟的header头 //设置连接结束后保存cookie信息的文件 curl_setopt($ch, CURLOPT_COOKIE, $cookie); $retry_times < 0 && ($retry_times = 0); for ($i = 0; $i <= $retry_times; $i++) { if ($i > 0) { Lib_Log::info("the request of {$url} time out after {$timeout} sec and retrying the {$i} times and post_data is " . Lib_Array::varExport($post_data)); } $result = curl_exec($ch); if ($result !== false) { break; } } $curl_status = curl_getinfo($ch); curl_close($ch); return $result; }
/** * @param int $task_id * @param int $mid * @param string $class * @return string */ private function _loop_callback_hook($task_id, $mid, $class) { if (!isset($this->flow[Module_FlowManager_Main::FLOW_TYPE_MAIN]) || !isset($this->flow[Module_FlowManager_Main::FLOW_TYPE_STUFF])) { Lib_Log::info("empty task flow main or stuff, will not run callback hook, id: %d", $this->id); return null; } // 安全检查,只有主模块才能注册回调 $main_mids = $this->flow[Module_FlowManager_Main::FLOW_TYPE_MAIN]; if (!in_array($mid, $main_mids)) { Lib_Log::notice("mid [%d] not in flow, flow id: [%d]", [$mid, $this->id]); return null; } // 没有回调方案 $callback_relation_mids = $this->flow[Module_FlowManager_Main::FLOW_TYPE_STUFF]; if (!isset($callback_relation_mids[$mid])) { return null; } Lib_Log::debug(function () use($mid, $callback_relation_mids) { return vsprintf("FLOW_CALL: prepare to gen callback for mid [%d], mids [%s]", [$mid, json_encode($callback_relation_mids[$mid])]); }); // 生成回调 $callback = $this->gen_module_callback_in_loop($task_id, $mid); // 注册 if ($callback['errno'] == Const_Err_Base::ERR_OK) { $callback = $callback['data']; Lib_Log::debug(function () use($task_id, $mid, $callback, $class) { return vsprintf("FLOW_CALL: register callback to class [%s], mid: %d", [get_class($class), $mid]); }); $class->register_callback($callback); } else { Lib_Log::notice("FLOW_CALL: gen callback failed. return %s", Lib_Helper::format_err_struct($callback)); } }
/** * 信号处理函数 * @param number $signo * @param number $pid * @param number $status * @return boolean */ public function childSignalHandler($signo, $pid = null, $status = null) { //If no pid is provided, that means we're getting the signal from the system. Let's figure out //which child process ended if (!$pid) { $pid = pcntl_waitpid(-1, $status, WNOHANG); } //Make sure we get all of the exited children while ($pid > 0) { if ($pid && isset($this->currentJobs[$pid])) { $exitCode = pcntl_wexitstatus($status); if ($exitCode != 0) { Lib_Log::info("{$pid} exited with status " . $exitCode); } unset($this->currentJobs[$pid]); } else { if ($pid) { //Oh no, our job has finished before this parent process could even note that it had been launched! //Let's make note of it and handle it when the parent process is ready for it Lib_Log::info("..... Adding {$pid} to the signal queue ....."); $this->signalQueue[$pid] = $status; } } $pid = pcntl_waitpid(-1, $status, WNOHANG); } return true; }
/** * @param int $ap_id * @param int $mode * @param array $options * @param string $php_script * @return array */ static function exec_task($ap_id, $mode = Module_FlowManager_Main::RUN_MODE_CALLBACK, $options = ['meta' => true], $php_script = 'tools/access_point.php') { if (empty($ap_id)) { return Lib_Helper::get_err_struct(Const_Err_DataAccess::ERR_ID_NOT_SET, '没有获取到接入点ID'); } if (php_sapi_name() != 'cli') { Lib_Log::info("Run task, user [%s], Ap id:[%d]", [Module_Account_User::get_current_user(), $ap_id]); } if (Da\Sys_App::run_mode() == DA_RUN_MODE_PRO && $mode == Module_FlowManager_Main::RUN_MODE_CALLBACK && !isset($options['without_ct'])) { return Module_ScheduledTask_Main::run_ap_task($ap_id); } else { self::exec_task_by_cli($ap_id, $mode, $options, $php_script); } return Lib_Helper::get_return_struct(['msg' => '运行成功']); }
public function childSignalHandler($signo, $pid = null, $status = null) { $pid = pcntl_waitpid(-1, $status, WNOHANG); while ($pid > 0) { if ($pid && isset($this->currentJobs[$pid])) { $exitCode = pcntl_wexitstatus($status); //子进程失败 if ($exitCode != 0) { $this->pushFaildData($pid); } unset($this->currentJobs[$pid]); } else { if ($pid) { Lib_Log::info("..... Adding {$pid} to the signal queue ....."); $this->signalQueue[$pid] = $status; } } $pid = pcntl_waitpid(-1, $status, WNOHANG); } return true; }