Repository navigation
Expand file tree
/
Copy pathWorkerStartCallback.php
More file actions
53 lines (45 loc) · 1.58 KB
/
Copy pathWorkerStartCallback.php
File metadata and controls
53 lines (45 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Framework\Bootstrap;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Coordinator\Constants;
use Hyperf\Coordinator\CoordinatorManager;
use Hyperf\Framework\Event\AfterWorkerStart;
use Hyperf\Framework\Event\BeforeWorkerStart;
use Hyperf\Framework\Event\MainWorkerStart;
use Hyperf\Framework\Event\OtherWorkerStart;
use Psr\EventDispatcher\EventDispatcherInterface;
use Swoole\Server as SwooleServer;
class WorkerStartCallback
{
public function __construct(protected EventDispatcherInterface $dispatcher, protected StdoutLoggerInterface $logger)
{
}
/**
* Handle Swoole onWorkerStart event.
*/
public function onWorkerStart(SwooleServer $server, int $workerId)
{
$this->dispatcher->dispatch(new BeforeWorkerStart($server, $workerId));
if ($workerId === 0) {
$this->dispatcher->dispatch(new MainWorkerStart($server, $workerId));
} else {
$this->dispatcher->dispatch(new OtherWorkerStart($server, $workerId));
}
if ($server->taskworker) {
$this->logger->info("TaskWorker#{$workerId} started.");
} else {
$this->logger->info("Worker#{$workerId} started.");
}
$this->dispatcher->dispatch(new AfterWorkerStart($server, $workerId));
CoordinatorManager::until(Constants::WORKER_START)->resume();
}
}