public function run($task) { $cmd = $task["cmd"]; $status = 0; exec($cmd, $output, $status); Main::log_write($cmd . ",已执行.status:" . $status); exit($status); }
public function __construct($file) { if (empty($file) || !empty($file) && !file_exists($file)) { Main::log_write("指定配置文件不存在,file:" . $file); exit; } $this->filePath = $file; }
protected function getDbConfig() { $config = (include ROOT_PATH . "config/config.php"); if (empty($config) || !isset($config["mysql"])) { Main::log_write("mysql config not found"); exit; } return $config["mysql"]; }
/** * 子进程 自动载入需要运行的工作类 * @param $class */ public function autoload($class) { include ROOT_PATH . "plugin" . DS . "PluginBase.class.php"; $file = ROOT_PATH . "plugin" . DS . $class . ".class.php"; if (file_exists($file)) { include $file; } else { Main::log_write("处理类不存在"); } }
/** * 子进程 自动载入需要运行的工作类 * @param $class */ public function autoload($class) { include ROOT_PATH . "worker/WorkerBase.class.php"; $file = ROOT_PATH . "worker/" . $class . "Worker" . ".class.php"; if (file_exists($file)) { include $file; } else { Main::log_write("处理类不存在"); } }
public function run($task) { $client = new GearmanClient(); $client->addServers($task["server"]); $client->doBackground($task["cmd"], $task["ext"]); if (($code = $client->returnCode()) != GEARMAN_SUCCESS) { Main::log_write("Gearman:" . $task["cmd"] . " to " . $task["server"] . " error,code=" . $code); exit; } Main::log_write("Gearman:" . $task["cmd"] . " to " . $task["server"] . " success,code=" . $code); exit; }
public function content($config) { if (!isset($config["host"]) || !isset($config["port"]) || !isset($config["timeout"]) || !isset($config["queue"])) { Main::log_write(vsprintf(" host=%s,port=%s,timeout=%s,queue=%s", $config)); exit; } $this->Redis = new Redis(); if (!$this->Redis->pconnect($config["host"], $config["port"], isset($config["timeout"]))) { Main::log_write(vsprintf("redis can't connect.host=%s,port=%s,timeout=%s", $config)); exit; } if (isset($config["db"]) && is_numeric($config["db"])) { $this->Redis->select($config["db"]); } $this->queue = $config["queue"]; }
/** * 解析启动模式参数 * @param $opt */ public static function params_s($opt) { //判断传入了s参数但是值,则提示错误 if (isset($opt["s"]) && !$opt["s"] || isset($opt["s"]) && !in_array($opt["s"], array("start", "stop", "restart"))) { Main::log_write("Please run: path/to/php main.php -s [start|stop|restart]"); } if (isset($opt["s"]) && in_array($opt["s"], array("start", "stop", "restart"))) { switch ($opt["s"]) { case "start": Crontab::start(); break; case "stop": Crontab::stop(); break; case "restart": Crontab::restart(); break; } } }
/** * 注册信号 */ private static function register_signal() { swoole_process::signal(SIGTERM, function ($signo) { self::exit2p("收到退出信号,退出主进程"); }); swoole_process::signal(SIGCHLD, function ($signo) { while ($ret = swoole_process::wait(false)) { $pid = $ret['pid']; if (isset(self::$task_list[$pid])) { $task = self::$task_list[$pid]; if ($task["type"] == "crontab") { $end = microtime(true); $start = $task["start"]; $id = $task["id"]; Main::log_write("{$id} [Runtime:" . sprintf("%0.6f", $end - $start) . "]"); $task["process"]->close(); //关闭进程 unset(self::$task_list[$pid]); if (isset(self::$unique_list[$id]) && self::$unique_list[$id] > 0) { self::$unique_list[$id]--; } } if ($task["type"] == "worker") { $end = microtime(true); $start = $task["start"]; $classname = $task["classname"]; Main::log_write("{$classname}_{$task["number"]} [Runtime:" . sprintf("%0.6f", $end - $start) . "]"); $task["process"]->close(); //关闭进程 (new Worker())->create_process($classname, $task["number"], $task["redis"]); } } } }); swoole_process::signal(SIGUSR1, function ($signo) { //TODO something }); }
/** * 注册信号 */ private static function register_signal() { swoole_process::signal(SIGTERM, function ($signo) { if (!empty(Main::$http_server)) { swoole_process::kill(Main::$http_server->pid, SIGKILL); } self::exit2p("收到退出信号,退出主进程"); }); swoole_process::signal(SIGCHLD, function ($signo) { while (($pid = pcntl_wait($status, WNOHANG)) > 0) { $task = self::$task_list[$pid]; $end = microtime(true); $start = $task["start"]; $id = $task["id"]; Main::log_write("{$id} [Runtime:" . sprintf("%0.6f", $end - $start) . "]"); unset(self::$task_list[$pid]); if (isset(self::$unique_list[$id]) && self::$unique_list[$id] > 0) { self::$unique_list[$id]--; } } }); swoole_process::signal(SIGUSR1, function ($signo) { LoadConfig::reload_config(); }); }
/** * 运行httpserver * @param $worker */ public function http_run($worker) { Main::log_write("HTTP Server 已启动"); $binpath = $_SERVER["_"]; $worker->exec($binpath, array(ROOT_PATH . "/http.php", $worker->pipe, Crontab::$config_file, self::$host, self::$port)); }
/** * 退出主进程 * @param $msg */ public static function exit2p($msg) { @unlink(self::$pid_file); Main::log_write($msg . "\n"); exit; }