SyncTask 使用说明 new 时,传入需要处理的模块名,对应到表 sync_task_config 方法:run 方法 传入 callback done 方法 结束程序运行,并打印log日志 属性:config 包括本次项目(guideline) 的配置信息,通过此数组,可以获取上一次的更新时间 sync_script.php ```php run(function($task) { if (xx) { return $task->done(); } echo $task->config . "\n"; // return 'world'; return array('10', '20'); }); ?> ``` SyncTask.php ```php config = array('enabled'=>'Y', 'is_run'=>'N'); } public function run($fn) { $this->start_log(); $this->startedAt = microtime(true); if($this->config['enabled'] == 'N') { Log::info("同步已关闭..."); return $this->done(); } if($this->config['is_run'] == 'Y') { Log::info("正在运行..."); return $this->done(); } try { list($this->updated, $this->created) = $fn($this); } catch (Exception $e) { Log::error($e); } $this->done(); } public function done() { $this->end_log(); } private function start_log() { Log::info($_SERVER['SCRIPT_NAME'] . ' start...'); } private function end_log() { $this->run_time = $this->costTime(); Log::info('updated records:' . sg($this->updated,0)); Log::info('created records:' . sg($this->created,0)); Log::info('run time:' . $this->run_time . 'ms'); Log::info($_SERVER['SCRIPT_NAME'] . ' end.'); } private function costTime($str_format = false) { $this->finishedAt = microtime(TRUE); $cost = $this->finishedAt - $this->startedAt; $cost = $cost * 1000; if($str_format) { return number_format($cost, 3) . 'ms'; } else { return number_format($cost, 3, '.', ''); } } } ?> ``` BaseScript.php ```php 2 * 60 * 1000) { $msg = '【致命错误】程序运行时间超过2分钟!(' . $cost . ")\n" . $_SERVER['SCRIPT_NAME']; self::error($msg); } else if ($cost > 30 * 1000) { $msg = '【严重警告】程序运行时间超过30秒!(' . $cost . ")\n" . $_SERVER['SCRIPT_NAME']; self::error($msg); } else if ($cost > 10 * 1000) { $msg = '【警告】程序运行时间超过10秒!(' . $cost . ")\n" . $_SERVER['SCRIPT_NAME']; self::error($msg); } self::end(); } public static function end() { self::log('run time:' . self::costTime(self::$start_time)); self::log($_SERVER['SCRIPT_NAME'] . ' end.'); } public static function costTime($start, $str_format = true) { $cost = microtime(TRUE) - $start; $cost = $cost * 1000; if($str_format) { return number_format($cost, 3) . 'ms'; } else { return number_format($cost, 3, '.', ''); } } public static function log($msg) { Log::info($msg); } public static function error($msg) { Log::error($msg); } } ?> ```