/** * @brief 子进程进入控制调度 */ protected function _start() { //进程刚拉起来的时候先做任务的状态复位(重启、升级、进程异常退出在拉起的情况) $objModel = new \Daemon\Models\Demo($this->_objAha); $ret = (yield $objModel->dbTrans()); //重置中间状态失败 退出事件循环 等待master重新拉起进程 if (false === $ret) { Log::monitor()->error(array(Monitor::KEY => Monitor::RESET_PROCESSING_ERR)); \swoole_event_exit(); } else { //重置中间状态成功 初始化定时器 $this->_initTimer(); } }
<?php function timeout($tm) { echo time() . " Timeout #{$tm}\n"; if ($tm == 3) { global $timer4; swoole_timer_clear($timer4); } } $timer1 = swoole_timer_after(1000, function ($id) { echo "hello world"; global $timer1; swoole_timer_clear($timer1); }, 1); $timer2 = swoole_timer_after(2000, 'timeout', 2); $timer3 = swoole_timer_after(4000, 'timeout', 3); $timer4 = swoole_timer_after(8000, 'timeout', 4); $timer5 = swoole_timer_after(10000, 'timeout', 5); swoole_process::signal(SIGTERM, function () { swoole_event_exit(); }); var_dump($timer1, $timer2, $timer3, $timer4, $timer5);
public static function exit() { if (self::$_mode == "cli") { //swoole_server模式下不进程event_exit swoole_event_exit(); } }
public function signalChildCallback($signalNo) { switch ($signalNo) { case SIGUSR1: swoole_event_exit(); break; default: } }
/** * @param int $worker_num * @param bool $daemon * @throws Exception\NotFound */ function runWorker($worker_num = 1, $daemon = false) { if ($worker_num > 1 or $daemon) { if (!class_exists('\\swoole\\process')) { throw new Exception\NotFound("require swoole extension"); } if ($worker_num < 0 or $worker_num > 1000) { $worker_num = 200; } } else { $this->_atomic = new \swoole_atomic(1); $this->_worker(); return; } if ($daemon) { \swoole_process::daemon(); } $this->_atomic = new \swoole_atomic(1); for ($i = 0; $i < $worker_num; $i++) { $process = new \swoole\process(array($this, '_worker'), false, false); $process->start(); $this->_workers[] = $process; } \swoole_process::signal(SIGCHLD, function () { while (true) { $exitProcess = \swoole_process::wait(false); if ($exitProcess) { foreach ($this->_workers as $k => $p) { if ($p->pid == $exitProcess['pid']) { if ($this->_atomic->get() == 1) { $p->start(); } else { unset($this->_workers[$k]); if (count($this->_workers) == 0) { swoole_event_exit(); } } } } } else { break; } } }); \swoole_process::signal(SIGTERM, function () { //停止运行 $this->_atomic->set(0); }); }