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
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
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.
*
* @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
*/
const EVENT_WORKER_START = 'workerStart';
/**
* @event WorkerEvent that is triggered each iteration between requests to queue.
* @since 2.0.3
*/
const EVENT_WORKER_LOOP = 'workerLoop';
/**
* @event WorkerEvent that is triggered when the worker is stopped.
* @since 2.0.2
*/
const EVENT_WORKER_STOP = 'workerStop';
/**
* @var array|string
* @since 2.0.2
*/
public $loopConfig = SignalLoop::class;
/**
* @var string command class name
*/
public $commandClass = Command::class;
/**
* @var array of additional options of command
*/
public $commandOptions = [];
/**
* @var callable|null
* @internal for worker command only
*/
public $messageHandler;
/**
* @var int current process ID of a worker.
* @since 2.0.2
*/
private $_workerPid;
/**
* @return string command id
* @throws
*/
protected function getCommandId()
{
foreach (Yii::$app->getComponents(false) as $id => $component) {
if ($component === $this) {
return Inflector::camel2id($id);
}
}
throw new InvalidConfigException('Queue must be an application component.');
}
/**
* @inheritdoc
*/
public function bootstrap($app)
{
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)
{
$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;
}
$exitCode = null;
try {
call_user_func($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
* @since 2.0.2
*/
public function getWorkerPid()
{
return $this->_workerPid;
}
/**
* @inheritdoc
*/
protected function handleMessage($id, $message, $ttr, $attempt)
{
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 $workerPid of worker process
* @return bool
* @internal for worker command only
*/
public function execute($id, $message, $ttr, $attempt, $workerPid)
{
$this->_workerPid = $workerPid;
return parent::handleMessage($id, $message, $ttr, $attempt);
}
}