mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-03 12:17:48 +08:00
Rewrite process name with app_name.
This commit is contained in:
parent
770f0dd16d
commit
3234bfba8a
1
src/server/.gitattributes
vendored
Normal file
1
src/server/.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
||||
/tests export-ignore
|
@ -12,14 +12,26 @@ declare(strict_types=1);
|
||||
|
||||
namespace Hyperf\Server\Listener;
|
||||
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
use Hyperf\Framework\Event\AfterWorkerStart;
|
||||
use Hyperf\Framework\Event\OnManagerStart;
|
||||
use Hyperf\Framework\Event\OnStart;
|
||||
use Hyperf\Process\Event\BeforeProcessHandle;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class InitProcessTitleListener implements ListenerInterface
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
public function listen(): array
|
||||
{
|
||||
return [
|
||||
@ -32,18 +44,31 @@ class InitProcessTitleListener implements ListenerInterface
|
||||
|
||||
public function process(object $event)
|
||||
{
|
||||
$prefix = 'Hyperf.';
|
||||
if ($this->container->has(ConfigInterface::class)) {
|
||||
$name = $this->container->get(ConfigInterface::class)->get('app_name');
|
||||
if ($name) {
|
||||
$prefix = $name . '.';
|
||||
}
|
||||
}
|
||||
|
||||
if ($event instanceof OnStart) {
|
||||
@cli_set_process_title('Master');
|
||||
$this->setTitle($prefix . 'Master');
|
||||
} elseif ($event instanceof OnManagerStart) {
|
||||
@cli_set_process_title('Manager');
|
||||
$this->setTitle($prefix . 'Manager');
|
||||
} elseif ($event instanceof AfterWorkerStart) {
|
||||
if ($event->server->taskworker) {
|
||||
@cli_set_process_title('TaskWorker.' . $event->workerId);
|
||||
$this->setTitle($prefix . 'TaskWorker.' . $event->workerId);
|
||||
} else {
|
||||
@cli_set_process_title('Worker.' . $event->workerId);
|
||||
$this->setTitle($prefix . 'Worker.' . $event->workerId);
|
||||
}
|
||||
} elseif ($event instanceof BeforeProcessHandle) {
|
||||
@cli_set_process_title($event->process->name . '.' . $event->index);
|
||||
$this->setTitle($prefix . $event->process->name . '.' . $event->index);
|
||||
}
|
||||
}
|
||||
|
||||
protected function setTitle($title)
|
||||
{
|
||||
@cli_set_process_title($title);
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,12 @@ use Hyperf\Framework\Event\OnManagerStart;
|
||||
use Hyperf\Framework\Event\OnStart;
|
||||
use Hyperf\Process\Event\BeforeProcessHandle;
|
||||
use Hyperf\Server\Listener\InitProcessTitleListener;
|
||||
use Hyperf\Utils\Context;
|
||||
use HyperfTest\Server\Stub\DemoProcess;
|
||||
use HyperfTest\Server\Stub\InitProcessTitleListenerStub;
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
@ -25,9 +30,15 @@ use PHPUnit\Framework\TestCase;
|
||||
*/
|
||||
class InitProcessTitleListenerTest extends TestCase
|
||||
{
|
||||
protected function tearDown()
|
||||
{
|
||||
Mockery::close();
|
||||
}
|
||||
|
||||
public function testInitProcessTitleListenerListen()
|
||||
{
|
||||
$listener = new InitProcessTitleListener();
|
||||
$container = Mockery::mock(ContainerInterface::class);
|
||||
$listener = new InitProcessTitleListener($container);
|
||||
|
||||
$this->assertSame([
|
||||
OnStart::class,
|
||||
@ -36,4 +47,30 @@ class InitProcessTitleListenerTest extends TestCase
|
||||
BeforeProcessHandle::class,
|
||||
], $listener->listen());
|
||||
}
|
||||
|
||||
public function testProcessDefaultName()
|
||||
{
|
||||
$container = Mockery::mock(ContainerInterface::class);
|
||||
$container->shouldReceive('has')->with(Mockery::any())->andReturn(false);
|
||||
|
||||
$listener = new InitProcessTitleListenerStub($container);
|
||||
$process = new DemoProcess($container);
|
||||
|
||||
$listener->process(new BeforeProcessHandle($process, 1));
|
||||
|
||||
$this->assertSame('Hyperf.test.demo.1', Context::get('test.server.process.title'));
|
||||
}
|
||||
|
||||
public function testProcessName()
|
||||
{
|
||||
$container = Mockery::mock(ContainerInterface::class);
|
||||
$container->shouldReceive('has')->with(Mockery::any())->andReturn(false);
|
||||
|
||||
$listener = new InitProcessTitleListenerStub($container);
|
||||
$process = new DemoProcess($container);
|
||||
|
||||
$listener->process(new BeforeProcessHandle($process, 1));
|
||||
|
||||
$this->assertSame('Hyperf.test.demo.1', Context::get('test.server.process.title'));
|
||||
}
|
||||
}
|
||||
|
24
src/server/tests/Stub/DemoProcess.php
Normal file
24
src/server/tests/Stub/DemoProcess.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace HyperfTest\Server\Stub;
|
||||
|
||||
use Hyperf\Process\AbstractProcess;
|
||||
|
||||
class DemoProcess extends AbstractProcess
|
||||
{
|
||||
public $name = 'test.demo';
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
}
|
||||
}
|
24
src/server/tests/Stub/InitProcessTitleListenerStub.php
Normal file
24
src/server/tests/Stub/InitProcessTitleListenerStub.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace HyperfTest\Server\Stub;
|
||||
|
||||
use Hyperf\Server\Listener\InitProcessTitleListener;
|
||||
use Hyperf\Utils\Context;
|
||||
|
||||
class InitProcessTitleListenerStub extends InitProcessTitleListener
|
||||
{
|
||||
public function setTitle($title)
|
||||
{
|
||||
Context::set('test.server.process.title', $title);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user