This commit is contained in:
2021-03-01 16:46:10 +08:00
parent b1eadf279e
commit 13ce5d980b
+37 -35
View File
@@ -6,8 +6,10 @@ namespace Snowflake\Process;
use Exception; use Exception;
use JetBrains\PhpStorm\Pure;
use Snowflake\Application; use Snowflake\Application;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
/** /**
@@ -17,47 +19,47 @@ use Snowflake\Snowflake;
abstract class Process extends \Swoole\Process implements SProcess abstract class Process extends \Swoole\Process implements SProcess
{ {
/** @var Application $application */ /** @var Application $application */
protected Application $application; protected Application $application;
/** /**
* Process constructor. * Process constructor.
* @param $application * @param $application
* @param $name * @param $name
* @param bool $enable_coroutine * @param bool $enable_coroutine
* @throws Exception * @throws Exception
*/ */
public function __construct($application, $name, $enable_coroutine = true) public function __construct($application, $name, $enable_coroutine = true)
{ {
parent::__construct([$this, '_load'], false, 1, $enable_coroutine); parent::__construct([$this, '_load'], false, 1, $enable_coroutine);
$this->application = $application; $this->application = $application;
Snowflake::setWorkerId($this->pid); Snowflake::setWorkerId($this->pid);
} }
/** /**
* @param Process $process * @param Process $process
* @throws \Snowflake\Exception\ComponentException * @throws ComponentException
*/ */
public function _load(Process $process) public function _load(Process $process)
{ {
putenv('environmental=' . Snowflake::PROCESS); putenv('environmental=' . Snowflake::PROCESS);
fire(Event::SERVER_WORKER_START); fire(Event::SERVER_WORKER_START);
if (Snowflake::isLinux()) { if (Snowflake::isLinux()) {
$this->name($this->getPrefix()); $this->name($this->getPrefix());
} }
$this->onHandler($process); $this->onHandler($process);
} }
/** /**
* @return string * @return string
*/ */
private function getPrefix(): string #[Pure] private function getPrefix(): string
{ {
return ucfirst(rtrim(Snowflake::app()->id, ':') . ': ' . get_called_class()); return Snowflake::app()->id . get_called_class();
} }
} }