Files
kiri-core/System/Process/Process.php
T
as2252258@163.com 91de213fb0 modify
2021-04-11 19:08:36 +08:00

73 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Snowflake\Process;
use Exception;
use JetBrains\PhpStorm\Pure;
use Snowflake\Application;
use Snowflake\Event;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake;
use Swoole\Coroutine\System;
/**
* Class Process
* @package Snowflake\Snowflake\Service
*/
abstract class Process extends \Swoole\Process implements SProcess
{
/** @var Application $application */
protected Application $application;
/**
* Process constructor.
* @param $application
* @param $name
* @param bool $enable_coroutine
* @throws Exception
*/
public function __construct($application, $name, $enable_coroutine = true)
{
parent::__construct([$this, '_load'], false, 1, $enable_coroutine);
$this->application = $application;
Snowflake::setProcessId($this->pid);
$content = System::readFile(storage('runtime.php'));
$annotation = Snowflake::app()->getAnnotation();
$annotation->setLoader(unserialize($content));
$annotation->runtime(APP_PATH . 'app/Kafka');
}
/**
* @param Process $process
* @throws Exception
*/
public function _load(Process $process)
{
putenv('environmental=' . Snowflake::PROCESS);
fire(Event::SERVER_WORKER_START);
if (Snowflake::getPlatform()->isLinux()) {
name($process->pid, $this->getPrefix());
}
$this->onHandler($process);
}
/**
* @return string
*/
#[Pure] private function getPrefix(): string
{
return get_called_class();
}
}