/** * @param array $url_piece * @return array * @throws Exception */ function route($url_piece) { if (isset($url_piece[0]) && strpos($url_piece[0], '.php') !== false) { array_shift($url_piece); } $module = array_shift($url_piece); $path = implode('/', array_filter($url_piece)); if ($module == self::DEFAULT_MODULE && $path == self::DEFAULT_PATH) { self::redirect_to('/'); } empty($module) && ($module = self::DEFAULT_MODULE); empty($path) && ($path = self::DEFAULT_PATH); $this->_current_url_info['module'] = $module; Lib_Log::debug('Try to route to module: [%s], path: [%s]', [$module, $path]); if (isset($this->_router[$module])) { return $this->_dispatch_to_method($this->_router[$module], $path); } $class = Module_ModuleManager_Register::get_module_entry_class($module); if ($class && is_callable([$class, self::ROUTER_REG_METHOD])) { $router = call_user_func([$class, self::ROUTER_REG_METHOD]); $this->_router[$module] = $router; return $this->_dispatch_to_method($router, $path); } else { return ['errno' => Da\Sys_Router::ERRNO_NOT_FOUND, 'data' => 'class ' . var_export($class, true) . ' can not route.' . ' module: [' . $module . ']' . ' path: [' . $path . ']']; } }
/** * @param array */ function init_db_with_config($config) { $host = $config['host']; $port = $config['port']; Lib_Log::debug("Try to connect redis: %s", json_encode($config)); try { $this->_db = new Lib_Redis($host, $port); } catch (RedisException $ex) { $this->_db = null; Lib_Log::error("REDIS-ERROR: %s", json_encode($ex)); } }
/** * @param null $ip * @param null $port * @param null $unix_sock * @throws Exception */ function __construct($ip = null, $port = null, $unix_sock = null) { if (!empty($ip) && !empty($port)) { $this->_ip = $ip; $this->_port = $port; Lib_Log::debug("Lib redis start to connect redis %s:%s", [strval($ip), strval($port)]); } else { if (empty($unix_sock)) { throw new Exception("Lib redis, empty ip and port, without unixsock addr too."); } if (file_exists($unix_sock)) { $this->_unix_sock = $unix_sock; Lib_Log::debug("Lib redis connect to unix sock: %s", $unix_sock); } else { throw new Exception('Lib redis, no such sock file: ' . $unix_sock); } } $this->_create_connection(); }
/** * @param int $hook_type * @param array $args * @return bool */ function run_hooks($hook_type = self::HOOK_TYPE_BEFORE_RUN, $args = []) { if (!isset($this->_hooks[$hook_type])) { return false; } foreach ($this->_hooks[$hook_type] as $_name => $_hook) { if (!isset($_hook[1])) { unset($this->_hooks[$hook_type][$_name]); continue; } if (is_callable($_hook[0])) { Lib_Log::debug("TASK_HOOK: call hook type [%d], name [%s], once: [%s]", [$hook_type, $_name, var_export($_hook[1], true)]); call_user_func_array($_hook[0], $args); } else { Lib_Log::notice("TASK_HOOK: hook [%s] not callable", json_encode($_hook[0])); } // run once, remove if ($_hook[1] == true) { unset($this->_hooks[$hook_type][$_name]); } } return true; }
/** * @param int $task_id * @param int $mid * @param array $data * @return bool|int */ private function _write_meta_file($task_id, $mid, $data) { // 检查meta选项,如果有则生成meta文件 if (isset($this->options[Module_FlowManager_Main::EXPORT_META_FILE]) && $this->options[Module_FlowManager_Main::EXPORT_META_FILE] == Module_FlowManager_Main::META_FILE_CREATE) { Lib_Log::debug("%s use meta file, task: %d, mid %d", [__CLASS__, $task_id, $mid]); $meta_file = self::meta_file_path($task_id, $mid); if ($meta_file) { return file_put_contents($meta_file, json_encode($data)); } } return false; }
/** * @param string */ function init_with_conf_path($conf_path) { Lib_Log::debug("Init config with path: %s", json_encode($conf_path)); $this->config($conf_path); $this->init_db_with_config($this->get_config()); }
/** * @param int $id * @param string $value * @return array */ private static function _format_field_value($id, $value) { if (!is_array($value)) { Lib_Log::debug('filed values is not array, values: %s', json_encode($value)); return []; } foreach ($value as $_k => $_v) { if (is_array($_v) && isset($_v[Const_Interface::FIELD_ATTR_RELATION])) { $new_value = []; if (!is_array($_v[Const_Interface::FIELD_ATTR_RELATION])) { Lib_Log::error('Fields error, please check relaction fields :' . json_encode($_v) . ' in an array'); continue; } foreach ($_v[Const_Interface::FIELD_ATTR_RELATION] as $__value) { $new_value[] = [Const_Interface::FIELD_ATTR_KEY => $__value, Const_Interface::FIELD_ATTR_MODULE => $id]; } $value[$_k][Const_Interface::FIELD_ATTR_RELATION] = $new_value; } } return $value; }
/** * @param int $task_id * @param int $module_id * @param array $data * @param array $options * @return array */ function run_module($task_id, $module_id, $data, $options) { $class = $this->get_module_class($module_id); Lib_Log::debug("%s begin run module [%d]\ttask_id:[%d]\toptions:%s", [__METHOD__, $module_id, $task_id, json_encode($options)]); $class->set_options($options); $this->run_hooks(self::get_run_module_hook_type(self::HOOK_TYPE_BEFORE_RUN_MODULE_PREFIX, $module_id), [$task_id, $module_id, $class, $data]); $this->run_hooks(self::HOOK_TYPE_BEFORE_RUN, [$task_id, $module_id, $class, $data]); try { $result = $class->run($task_id, $data); } catch (Exception $ex) { $code = $ex->getCode(); $msg = $ex->getMessage(); Lib_Log::error("Got a Exception, code [%d], message [%s]", [$code, $msg]); $result = Lib_Helper::get_err_struct($code, $msg); } $this->run_hooks(self::get_run_module_hook_type(self::HOOK_TYPE_AFTER_RUN_MODULE_PREFIX, $module_id), [$task_id, $module_id, $class, $result]); $this->run_hooks(self::HOOK_TYPE_AFTER_RUN, [$task_id, $module_id, $class, $result]); return $result; }
/** * @param $callback * @return null|array * @throws Exception */ protected function _doAction($callback) { Lib_Log::debug(__METHOD__ . ': do action begin!'); if (empty($this->_currentDbName) || empty($this->_currentTableName)) { throw new Exception(__METHOD__ . 'empty param ! DB:' . var_export($this->_currentDbName, true) . ' Table: ' . var_export($this->_currentTableName, true), self::ERRNO_EMPTY_PARAMS); } Lib_Log::debug(__METHOD__ . ': data check finish!'); $doAct = function () use($callback) { $dbName = $this->_currentDbName; $tableName = $this->_currentTableName; if (empty($this->_conn->{$dbName})) { $this->ReConnect(); } $collection = $this->_conn->{$dbName}->{$tableName}; $ret = $callback($collection); return $ret; }; Lib_Log::debug(__METHOD__ . ': create function finish!'); $retryCount = self::RETRY_COUNT - 1; $ret = null; do { Lib_Log::debug(__METHOD__ . ': begin do action! retry count is: %d', $retryCount); try { $ret = $doAct(); $retryCount = -1; } catch (Exception $e) { if ($retryCount < 0) { throw new Exception($e->getMessage(), $e->getCode()); } else { Lib_Log::warn("MONGO Retry,retry time: %d, Exception code: %s, msg: %s", array(self::RETRY_COUNT - $retryCount, strval($e->getCode()), $e->getMessage())); } } } while (is_null($ret) && $retryCount-- >= 0); return $ret; }
/** * @param string * @param null|string * @return array|bool * @throws \Exception */ public function get_config($str, $path = null) { $path_array = explode('/', $str); if (!isset($path_array[0])) { return false; } $file = ''; $filepath = ''; $section = array_pop($path_array); empty($path_array) || ($file = array_pop($path_array)); empty($path_array) || ($filepath = implode($path_array)); empty($path) && ($path = $this->get_config_path()); if (!empty($filepath)) { $path .= '/' . $filepath; } if (empty($file)) { $file = $section; $section = null; } if (is_file($path . '/' . $file . '.ini')) { } elseif (!empty($file) && is_dir($path . '/' . $file)) { $path = $path . '/' . $file; $file = $section; $section = null; } else { return false; } $run_mode = Sys_App::app()->get_run_mode(); $run_mode == DA_RUN_MODE_PRO && $this->check_run_mode_suffix(false); $run_mode_abbr = self::run_mode_abbr($run_mode); $run_mode_conf_file = empty($run_mode_abbr) ? $file : $file . '.' . $run_mode_abbr; if ($this->_check_run_mode_suffix && is_file($path . '/' . $run_mode_conf_file . '.ini')) { $file = $run_mode_conf_file; \Lib_Log::debug("use run mode config file: [%s]", $file); } $file .= '.ini'; $config = $this->get_config_from_file($file, $section, $path); return $config; }
/** * @brief * 子进程处理函数 * 子进程返回非0值,则会重试 * @see * @note * @author duxin01 * @date 2014/04/17 16:19:59 **/ public function childJob($hotelInfo) { $pid = posix_getpid(); $loginfo = json_encode($hotelInfo); Lib_Log::debug("child job begin, pid:{$pid}, input data: {$logInfo}"); $ret = call_user_func($this->_childCb, $hotelInfo['data']); Lib_Log::debug("child job end, pid:{$pid}, "); if ($ret != 0) { Lib_Log::debug("child job failed!pid:{$pid} return {$ret}"); exit($ret); } }
/** * @param array $cond * @return array */ public function get_tables($cond = []) { switch ($this->_driver) { case self::DB_DRIVER_MONGO: default: Lib_Log::debug(__METHOD__ . ': Use mongo driver, cond: %s', [json_encode($cond)]); try { $result = $this->_db->GetTables($cond); if (is_null($result)) { $result = Lib_Helper::get_err_struct(Const_Err_Db::ERR_MONGO_COLLECTIONS_FAIL, '获取数据集失败', __FILE__, __LINE__); } else { $result = Lib_Helper::get_return_struct($result); } } catch (Exception $ex) { Lib_Log::error(__METHOD__ . " get faild! cond: %s, code: %d, messsage: %s", [json_encode($cond), $ex->getCode(), $ex->getMessage()]); $result = Lib_Helper::get_err_struct(Const_Err_Db::ERR_MONGO_COLLECTIONS_FAIL, '获取数据集失败', __FILE__, __LINE__); } } return $result; }
/** * @param int $mid * @param array $stuff_mids * @param array $id_map * @return callable */ static function after_run_module_record_hook($mid, $stuff_mids, $id_map) { if ($stuff_mids === null) { $stuff_mids = []; } if (!isset($id_map[$mid][1])) { Lib_Log::warn("%s gen record id and order failed!", __METHOD__); } return function ($task_id, $module_id, $class, $result) use($mid, $stuff_mids, $id_map) { $record_id = $id_map[$mid][0]; $model_module_record = new Model_TaskRunModuleRecord(); // log for record Lib_Log::debug('record after hook task_id: [%d], module_id: [%d], record_id: [%d]', [$task_id, $module_id, $record_id]); // end log $stat = []; $end_time = time(); $counts = Module_ControlCentre_Counter::get_all_counts(); if (!isset($result['errno']) || $result['errno'] !== Const_Err_Base::ERR_OK) { $record = ['status' => Module_ModuleManager_Main::RUN_MODULE_STATUS_FAILED, 'result' => $result, 'end_time' => $end_time]; if (isset($result[Const_DataAccess::MREK_STATUS])) { $record['status'] = $result[Const_DataAccess::MREK_STATUS]; } } else { $record = ['status' => Module_ModuleManager_Main::RUN_MODULE_STATUS_FINISH, 'end_time' => $end_time]; $data = $result['data']; if (method_exists($class, 'format_record_msg')) { $stat = call_user_func([$class, 'format_record_msg'], $data); } if (!empty($stat)) { $record['result'] = ['errno' => Const_Err_Base::ERR_OK, 'msg' => $stat]; $record['stat'] = $stat; } } if ($counts['errno'] != Const_Err_Base::ERR_OK) { $stuff_mids = []; } else { $counts = $counts['data']; $_tmp_stat = Module_ControlCentre_Counter::formated_counts($mid, $counts); if (is_array($_tmp_stat) && !empty($_tmp_stat)) { $record['stat'] = isset($record['stat']) ? array_merge($_tmp_stat, $record['stat']) : $_tmp_stat; } unset($_tmp_stat); } $model_module_record->update_record($record_id, $record); foreach ($stuff_mids as $_mid) { $stat = []; $record_id = $id_map[$_mid][0]; $_tmp_stat = Module_ControlCentre_Counter::formated_counts($_mid, $counts); is_array($_tmp_stat) && ($stat = array_merge($_tmp_stat, $stat)); unset($_tmp_stat); $record = ['status' => Module_ModuleManager_Main::RUN_MODULE_STATUS_CALLED, 'end_time' => $end_time]; if (!empty($stat)) { $record['stat'] = $stat; } $model_module_record->update_record($record_id, $record); } }; }