-
-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathQueue.php
More file actions
166 lines (149 loc) · 4.24 KB
/
Queue.php
File metadata and controls
166 lines (149 loc) · 4.24 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
declare(strict_types=1);
namespace yii\queue\cli;
use Yii;
use yii\base\BootstrapInterface;
use yii\base\InvalidConfigException;
use yii\console\Application as ConsoleApp;
use yii\helpers\Inflector;
use yii\queue\Queue as BaseQueue;
/**
* Queue with CLI.
*
* @property-read int|null $workerPid
*
* @author Roman Zhuravlev <zhuravljov@gmail.com>
*/
abstract class Queue extends BaseQueue implements BootstrapInterface
{
/**
* @event WorkerEvent that is triggered when the worker is started.
* @since 2.0.2
*/
public const EVENT_WORKER_START = 'workerStart';
/**
* @event WorkerEvent that is triggered each iteration between requests to queue.
* @since 2.0.3
*/
public const EVENT_WORKER_LOOP = 'workerLoop';
/**
* @event WorkerEvent that is triggered when the worker is stopped.
* @since 2.0.2
*/
public const EVENT_WORKER_STOP = 'workerStop';
/**
* @var array|string
* @since 2.0.2
*/
public string|array $loopConfig = SignalLoop::class;
/**
* @var string command class name
*/
public string $commandClass = Command::class;
/**
* @var array of additional options of command
*/
public array $commandOptions = [];
/**
* @var callable|null
* @internal for worker command only
*/
public $messageHandler;
/**
* @var int|null current process ID of a worker.
* @since 2.0.2
*/
private ?int $workerPid = null;
/**
* @return string command id
*/
protected function getCommandId(): string
{
foreach (Yii::$app->getComponents(false) as $id => $component) {
if ($component === $this) {
return Inflector::camel2id((string)$id);
}
}
throw new InvalidConfigException('Queue must be an application component.');
}
/**
* @inheritdoc
*/
public function bootstrap($app): void
{
if ($app instanceof ConsoleApp) {
$app->controllerMap[$this->getCommandId()] = [
'class' => $this->commandClass,
'queue' => $this,
] + $this->commandOptions;
}
}
/**
* Runs worker.
*
* @param callable $handler
* @return null|int exit code
* @since 2.0.2
*/
protected function runWorker(callable $handler): ?int
{
$this->workerPid = getmypid();
/** @var LoopInterface $loop */
$loop = Yii::createObject($this->loopConfig, [$this]);
$event = new WorkerEvent(['loop' => $loop]);
$this->trigger(self::EVENT_WORKER_START, $event);
if ($event->exitCode !== null) {
return $event->exitCode;
}
try {
$handler(function () use ($loop, $event) {
$this->trigger(self::EVENT_WORKER_LOOP, $event);
return $event->exitCode === null && $loop->canContinue();
});
} finally {
$this->trigger(self::EVENT_WORKER_STOP, $event);
$this->workerPid = null;
}
return $event->exitCode;
}
/**
* Gets process ID of a worker.
*
* @inheritdoc
* @return int|null
* @since 2.0.2
*/
public function getWorkerPid(): ?int
{
return $this->workerPid;
}
/**
* @inheritdoc
*/
protected function handleMessage(int|string $id, string $message, int $ttr, int $attempt): bool
{
if ($this->messageHandler) {
return call_user_func($this->messageHandler, $id, $message, $ttr, $attempt);
}
return parent::handleMessage($id, $message, $ttr, $attempt);
}
/**
* @param string $id of a message
* @param string $message
* @param int $ttr time to reserve
* @param int $attempt number
* @param int|null $workerPid of worker process
* @return bool
* @internal for worker command only
*/
public function execute(string $id, string $message, int $ttr, int $attempt, ?int $workerPid): bool
{
$this->workerPid = $workerPid;
return parent::handleMessage($id, $message, $ttr, $attempt);
}
}