mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 11:48:08 +08:00
Merge pull request #366 from daydaygo/feat-201908081359
feat: cli_set_process_title
This commit is contained in:
commit
ef905de796
@ -1,5 +1,9 @@
|
||||
# v1.0.11 - TBD
|
||||
|
||||
## Added
|
||||
|
||||
- [#366](https://github.com/hyperf-cloud/hyperf/pull/366) Added InitProcessTitleListener, and `onStart`, `onManagerStart` events.
|
||||
|
||||
## Fixed
|
||||
|
||||
# v1.0.10 - 2019-08-09
|
||||
|
@ -192,6 +192,7 @@
|
||||
"HyperfTest\\Process\\": "src/process/tests/",
|
||||
"HyperfTest\\Redis\\": "src/redis/tests/",
|
||||
"HyperfTest\\Rpc\\": "src/rpc/tests/",
|
||||
"HyperfTest\\Server\\": "src/server/tests/",
|
||||
"HyperfTest\\ServiceGovernance\\": "src/service-governance/tests/",
|
||||
"HyperfTest\\Task\\": "src/task/tests/",
|
||||
"HyperfTest\\Utils\\": "src/utils/tests/",
|
||||
|
@ -31,6 +31,7 @@
|
||||
<directory suffix="Test.php">./src/paginator/tests</directory>
|
||||
<directory suffix="Test.php">./src/redis/tests</directory>
|
||||
<directory suffix="Test.php">./src/rpc/tests</directory>
|
||||
<directory suffix="Test.php">./src/server/tests</directory>
|
||||
<directory suffix="Test.php">./src/service-governance/tests</directory>
|
||||
<directory suffix="Test.php">./src/task/tests</directory>
|
||||
<directory suffix="Test.php">./src/utils/tests</directory>
|
||||
|
@ -35,6 +35,11 @@ class Consumer extends AbstractAnnotation
|
||||
*/
|
||||
public $queue = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $name = 'Consumer';
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
@ -52,7 +52,7 @@ class ConsumerManager
|
||||
$nums = $annotation->nums;
|
||||
$process = $this->createProcess($instance);
|
||||
$process->nums = (int) $nums;
|
||||
$process->name = 'Consumer-' . $instance->getQueue();
|
||||
$process->name = $annotation->name . '-' . $instance->getQueue();
|
||||
ProcessManager::register($process);
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ use Hyperf\Amqp\Annotation\Consumer;
|
||||
use Hyperf\Amqp\Message\ConsumerMessage;
|
||||
|
||||
/**
|
||||
* @Consumer(exchange="hyperf", routingKey="hyperf", queue="hyperf", nums=1)
|
||||
* @Consumer(exchange="hyperf", routingKey="hyperf", queue="hyperf", name ="%CLASS%", nums=1)
|
||||
*/
|
||||
class %CLASS% extends ConsumerMessage
|
||||
{
|
||||
|
@ -8,7 +8,7 @@ use Hyperf\Process\AbstractProcess;
|
||||
use Hyperf\Process\Annotation\Process;
|
||||
|
||||
/**
|
||||
* @Process(name="custom_process")
|
||||
* @Process(name="%CLASS%")
|
||||
*/
|
||||
class %CLASS% extends AbstractProcess
|
||||
{
|
||||
|
42
src/event/tests/ListenerProviderTest.php
Normal file
42
src/event/tests/ListenerProviderTest.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?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\Event;
|
||||
|
||||
use Hyperf\Event\ListenerProvider;
|
||||
use HyperfTest\Event\Event\Alpha;
|
||||
use HyperfTest\Event\Event\Beta;
|
||||
use HyperfTest\Event\Listener\AlphaListener;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class ListenerProviderTest extends TestCase
|
||||
{
|
||||
public function testListenNotExistEvent()
|
||||
{
|
||||
$provider = new ListenerProvider();
|
||||
$provider->on(Alpha::class, [new AlphaListener(), 'process']);
|
||||
$provider->on('NotExistEvent', [new AlphaListener(), 'process']);
|
||||
|
||||
$it = $provider->getListenersForEvent(new Alpha());
|
||||
[$class, $method] = $it->current();
|
||||
$this->assertInstanceOf(AlphaListener::class, $class);
|
||||
$this->assertSame('process', $method);
|
||||
$this->assertNull($it->next());
|
||||
|
||||
$it = $provider->getListenersForEvent(new Beta());
|
||||
$this->assertNull($it->current());
|
||||
}
|
||||
}
|
35
src/framework/src/Bootstrap/ManagerStartCallback.php
Normal file
35
src/framework/src/Bootstrap/ManagerStartCallback.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?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 Hyperf\Framework\Bootstrap;
|
||||
|
||||
use Hyperf\Framework\Event\OnManagerStart;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use Swoole\Server as SwooleServer;
|
||||
|
||||
class ManagerStartCallback
|
||||
{
|
||||
/**
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
protected $dispatcher;
|
||||
|
||||
public function __construct(EventDispatcherInterface $eventDispatcher)
|
||||
{
|
||||
$this->dispatcher = $eventDispatcher;
|
||||
}
|
||||
|
||||
public function onManagerStart(SwooleServer $server)
|
||||
{
|
||||
$this->dispatcher->dispatch(new OnManagerStart($server));
|
||||
}
|
||||
}
|
35
src/framework/src/Bootstrap/StartCallback.php
Normal file
35
src/framework/src/Bootstrap/StartCallback.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?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 Hyperf\Framework\Bootstrap;
|
||||
|
||||
use Hyperf\Framework\Event\OnStart;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use Swoole\Server as SwooleServer;
|
||||
|
||||
class StartCallback
|
||||
{
|
||||
/**
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
protected $dispatcher;
|
||||
|
||||
public function __construct(EventDispatcherInterface $eventDispatcher)
|
||||
{
|
||||
$this->dispatcher = $eventDispatcher;
|
||||
}
|
||||
|
||||
public function onStart(SwooleServer $server)
|
||||
{
|
||||
$this->dispatcher->dispatch(new OnStart($server));
|
||||
}
|
||||
}
|
28
src/framework/src/Event/OnManagerStart.php
Normal file
28
src/framework/src/Event/OnManagerStart.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?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 Hyperf\Framework\Event;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
class OnManagerStart
|
||||
{
|
||||
/**
|
||||
* @var Server
|
||||
*/
|
||||
public $server;
|
||||
|
||||
public function __construct(Server $server)
|
||||
{
|
||||
$this->server = $server;
|
||||
}
|
||||
}
|
28
src/framework/src/Event/OnStart.php
Normal file
28
src/framework/src/Event/OnStart.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?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 Hyperf\Framework\Event;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
class OnStart
|
||||
{
|
||||
/**
|
||||
* @var Server
|
||||
*/
|
||||
public $server;
|
||||
|
||||
public function __construct(Server $server)
|
||||
{
|
||||
$this->server = $server;
|
||||
}
|
||||
}
|
@ -37,6 +37,7 @@
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"HyperfTest\\Server\\": "tests/"
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
|
@ -12,6 +12,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace Hyperf\Server;
|
||||
|
||||
use Hyperf\Server\Listener\InitProcessTitleListener;
|
||||
|
||||
class ConfigProvider
|
||||
{
|
||||
public function __invoke(): array
|
||||
@ -21,6 +23,9 @@ class ConfigProvider
|
||||
],
|
||||
'commands' => [
|
||||
],
|
||||
'listeners' => [
|
||||
InitProcessTitleListener::class,
|
||||
],
|
||||
'scan' => [
|
||||
'paths' => [
|
||||
__DIR__,
|
||||
|
@ -39,12 +39,9 @@ class AfterWorkerStartListener implements ListenerInterface
|
||||
*/
|
||||
public function listen(): array
|
||||
{
|
||||
if (class_exists(AfterWorkerStart::class)) {
|
||||
return [
|
||||
AfterWorkerStart::class,
|
||||
];
|
||||
}
|
||||
return [];
|
||||
return [
|
||||
AfterWorkerStart::class,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
|
49
src/server/src/Listener/InitProcessTitleListener.php
Normal file
49
src/server/src/Listener/InitProcessTitleListener.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?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 Hyperf\Server\Listener;
|
||||
|
||||
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;
|
||||
|
||||
class InitProcessTitleListener implements ListenerInterface
|
||||
{
|
||||
public function listen(): array
|
||||
{
|
||||
return [
|
||||
OnStart::class,
|
||||
OnManagerStart::class,
|
||||
AfterWorkerStart::class,
|
||||
BeforeProcessHandle::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function process(object $event)
|
||||
{
|
||||
if ($event instanceof OnStart) {
|
||||
@cli_set_process_title('Master');
|
||||
} elseif ($event instanceof OnManagerStart) {
|
||||
@cli_set_process_title('Manager');
|
||||
} elseif ($event instanceof AfterWorkerStart) {
|
||||
if ($event->server->taskworker) {
|
||||
@cli_set_process_title('TaskWorker.' . $event->workerId);
|
||||
} else {
|
||||
@cli_set_process_title('Worker.' . $event->workerId);
|
||||
}
|
||||
} elseif ($event instanceof BeforeProcessHandle) {
|
||||
@cli_set_process_title($event->process->name . '.' . $event->index);
|
||||
}
|
||||
}
|
||||
}
|
@ -14,6 +14,7 @@ namespace Hyperf\Server;
|
||||
|
||||
use Hyperf\Contract\MiddlewareInitializerInterface;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Framework\Bootstrap;
|
||||
use Hyperf\Framework\Event\BeforeMainServerStart;
|
||||
use Hyperf\Framework\Event\BeforeServerStart;
|
||||
use Hyperf\Server\Exception\RuntimeException;
|
||||
@ -207,6 +208,18 @@ class Server implements ServerInterface
|
||||
|
||||
protected function defaultCallbacks()
|
||||
{
|
||||
$hasCallback = class_exists(Bootstrap\StartCallback::class) &&
|
||||
class_exists(Bootstrap\ManagerStartCallback::class) &&
|
||||
class_exists(Bootstrap\WorkerStartCallback::class);
|
||||
|
||||
if ($hasCallback) {
|
||||
return [
|
||||
SwooleEvent::ON_START => [Bootstrap\StartCallback::class, 'onStart'],
|
||||
SwooleEvent::ON_MANAGER_START => [Bootstrap\ManagerStartCallback::class, 'onManagerStart'],
|
||||
SwooleEvent::ON_WORKER_START => [Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
SwooleEvent::ON_WORKER_START => function (SwooleServer $server, int $workerId) {
|
||||
printf('Worker %d started.' . PHP_EOL, $workerId);
|
||||
|
@ -84,6 +84,26 @@ class SwooleEvent
|
||||
*/
|
||||
const ON_FINISH = 'finish';
|
||||
|
||||
/**
|
||||
* Swoole onShutdown event.
|
||||
*/
|
||||
const ON_SHUTDOWN = 'shutdown';
|
||||
|
||||
/**
|
||||
* Swoole onPacket event.
|
||||
*/
|
||||
const ON_PACKET = 'packet';
|
||||
|
||||
/**
|
||||
* Swoole onManagerStart event.
|
||||
*/
|
||||
const ON_MANAGER_START = 'managerStart';
|
||||
|
||||
/**
|
||||
* Swoole onManagerStop event.
|
||||
*/
|
||||
const ON_MANAGER_STOP = 'managerStop';
|
||||
|
||||
/**
|
||||
* Before server start, it's not a swoole event.
|
||||
*/
|
||||
|
39
src/server/tests/Listener/InitProcessTitleListenerTest.php
Normal file
39
src/server/tests/Listener/InitProcessTitleListenerTest.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?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\Listener;
|
||||
|
||||
use Hyperf\Framework\Event\AfterWorkerStart;
|
||||
use Hyperf\Framework\Event\OnManagerStart;
|
||||
use Hyperf\Framework\Event\OnStart;
|
||||
use Hyperf\Process\Event\BeforeProcessHandle;
|
||||
use Hyperf\Server\Listener\InitProcessTitleListener;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class InitProcessTitleListenerTest extends TestCase
|
||||
{
|
||||
public function testInitProcessTitleListenerListen()
|
||||
{
|
||||
$listener = new InitProcessTitleListener();
|
||||
|
||||
$this->assertSame([
|
||||
OnStart::class,
|
||||
OnManagerStart::class,
|
||||
AfterWorkerStart::class,
|
||||
BeforeProcessHandle::class,
|
||||
], $listener->listen());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user