read(APP_PATH . 'app', 'App'); $this->debug(sprintf('scan app dir use time %s', microtime(true) - $start)); if ($worker_id >= $server->setting['worker_num']) { $this->onTask($server, $worker_id); } else { $this->onWorker($server, $worker_id); } $this->debug(sprintf('%s #%d Pid:%d start.', ucfirst(env('environmental')), $worker_id, $server->worker_pid)); } /** * @param Server $server * @param int $worker_id * @throws ComponentException|ConfigException */ public function onTask(Server $server, int $worker_id) { putenv('environmental=' . Snowflake::TASK); $start = microtime(true); fire(Event::SERVER_TASK_START); $this->debug(sprintf('Event::SERVER_WORKER_START use time %s', microtime(true) - $start)); $this->set_process_name($server, $worker_id); } /** * @param Server $server * @param int $worker_id * @throws Exception * onWorker */ public function onWorker(Server $server, int $worker_id) { Snowflake::setWorkerId($server->worker_pid); putenv('environmental=' . Snowflake::WORKER); try { $start = microtime(true); fire(Event::SERVER_WORKER_START, [$worker_id]); $this->debug(sprintf('Event::SERVER_WORKER_START use time %s', microtime(true) - $start)); } catch (\Throwable $exception) { $this->addError($exception); write($exception->getMessage(), 'worker'); } $this->set_process_name($server, $worker_id); } /** * @param $socket * @param $worker_id * @return string * @throws ConfigException */ private function set_process_name($socket, $worker_id): mixed { $prefix = Config::get('id', false, 'system'); if ($worker_id >= $socket->setting['worker_num']) { $name = $prefix . ' Task: No.' . $worker_id; } else { $name = $prefix . ' worker: No.' . $worker_id; } if (Snowflake::isMac()) { return 1; } return swoole_set_process_name($name); } }