Merge pull request #366 from daydaygo/feat-201908081359

feat: cli_set_process_title
This commit is contained in:
李铭昕 2019-08-09 11:41:14 +08:00 committed by GitHub
commit ef905de796
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 312 additions and 9 deletions

View File

@ -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

View File

@ -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/",

View File

@ -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>

View File

@ -35,6 +35,11 @@ class Consumer extends AbstractAnnotation
*/
public $queue = '';
/**
* @var string
*/
public $name = 'Consumer';
/**
* @var int
*/

View File

@ -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);
}
}

View File

@ -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
{

View File

@ -8,7 +8,7 @@ use Hyperf\Process\AbstractProcess;
use Hyperf\Process\Annotation\Process;
/**
* @Process(name="custom_process")
* @Process(name="%CLASS%")
*/
class %CLASS% extends AbstractProcess
{

View 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());
}
}

View 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));
}
}

View 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));
}
}

View 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;
}
}

View 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;
}
}

View File

@ -37,6 +37,7 @@
},
"autoload-dev": {
"psr-4": {
"HyperfTest\\Server\\": "tests/"
}
},
"config": {

View File

@ -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__,

View File

@ -39,12 +39,9 @@ class AfterWorkerStartListener implements ListenerInterface
*/
public function listen(): array
{
if (class_exists(AfterWorkerStart::class)) {
return [
AfterWorkerStart::class,
];
}
return [];
return [
AfterWorkerStart::class,
];
}
/**

View 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);
}
}
}

View File

@ -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);

View File

@ -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.
*/

View 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());
}
}