swoft-cloud / swoft

🚀 PHP Microservice Full Coroutine Framework
https://swoft.org
Apache License 2.0
5.58k stars 788 forks source link

提议:增加user process命名,便于显性跟踪进程信息 #1326

Open Hetystars opened 4 years ago

Hetystars commented 4 years ago
Q A
Bug report? no
Feature request? no
Swoft version 2.1.0
Swoole version 4.4.16
PHP version 7.2
Runtime environment CentOS 7

Describe the bug 当前user process没有显性命名,不便于查看用户自定义进程的信息 Expected behavior 提议:增加user process命名,便于显性跟踪进程信息 Screenshots If applicable, add screenshots to help explain your problem.

Details 见代码

    /**
     * Add process
     *
     * @param Server $server
     *
     * @throws ServerException
     */
    private function addProcesses(Server $server): void
    {
        $process = $server->getProcess();
        file_put_contents('/tmp/record.log',json_encode($process));
        if (empty($process)) {
            return;
        }

        foreach ($process as $name => $userProcess) {
            if (!$userProcess instanceof UserProcessInterface) {
                throw new ServerException('Server add process must be instanceof UserProcessInterface!');
            }

            $callback  = [$userProcess, 'run'];
            $stdinOut  = $userProcess->isStdinOut();
            $pipeType  = $userProcess->getPipeType();
            $coroutine = $userProcess->isCoroutine();

            $function = function (SwooleProcess $swProcess) use ($callback, $server, $name) {
                $process = Process::new($swProcess);
                //增加此行
                Swoft\Stdlib\Helper\Sys::setProcessTitle(sprintf('swoft-process-%s', $name));
                // Before
                Swoft::trigger(ProcessEvent::BEFORE_USER_PROCESS, null, $server, $process, $name);

                try {// Run
                    PhpHelper::call($callback, $process);
                } catch (Throwable $e) {
                    Error::log('User process fail(%s %s %d)!', $e->getFile(), $e->getMessage(), $e->getLine());
                }

                // After
                Swoft::trigger(ProcessEvent::AFTER_USER_PROCESS);
            };

            $currentProcess = new SwooleProcess($function, $stdinOut, $pipeType, $coroutine);
            // save swoole process
            $userProcess->setSwooleProcess($currentProcess);
            $server->getSwooleServer()->addProcess($currentProcess);
        }
    }

Provide minimal script to reproduce the issue

// paste code
inhere commented 4 years ago

@Hetystars 你在自己的进程类里面也是可以设置的呀