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
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\queue\amqp;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use yii\base\Application as BaseApp;
use yii\base\Event;
use yii\base\NotSupportedException;
use yii\queue\cli\Queue as CliQueue;
/**
* Amqp Queue.
*
* @deprecated since 2.0.2 and will be removed in 2.1. Consider using amqp_interop driver instead.
*
* @author Roman Zhuravlev <zhuravljov@gmail.com>
*/
class Queue extends CliQueue
{
public $host = 'localhost';
public $port = 5672;
public $user = 'guest';
public $password = 'guest';
public $queueName = 'queue';
public $exchangeName = 'exchange';
public $vhost = '/';
/**
* @var string command class name
*/
public $commandClass = Command::class;
/**
* @var AMQPStreamConnection
*/
protected $connection;
/**
* @var AMQPChannel
*/
protected $channel;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
Event::on(BaseApp::class, BaseApp::EVENT_AFTER_REQUEST, function () {
$this->close();
});
}
/**
* Listens amqp-queue and runs new jobs.
*/
public function listen()
{
$this->open();
$callback = function (AMQPMessage $payload) {
$id = $payload->get('message_id');
list($ttr, $message) = explode(';', $payload->body, 2);
if ($this->handleMessage($id, $message, $ttr, 1)) {
$payload->delivery_info['channel']->basic_ack($payload->delivery_info['delivery_tag']);
}
};
$this->channel->basic_qos(null, 1, null);
$this->channel->basic_consume($this->queueName, '', false, false, false, false, $callback);
while (count($this->channel->callbacks)) {
$this->channel->wait();
}
}
/**
* @inheritdoc
*/
protected function pushMessage($message, $ttr, $delay, $priority)
{
if ($delay) {
throw new NotSupportedException('Delayed work is not supported in the driver.');
}
if ($priority !== null) {
throw new NotSupportedException('Job priority is not supported in the driver.');
}
$this->open();
$id = uniqid('', true);
$this->channel->basic_publish(
new AMQPMessage("$ttr;$message", [
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
'message_id' => $id,
]),
$this->exchangeName
);
return $id;
}
/**
* @inheritdoc
*/
public function status($id)
{
throw new NotSupportedException('Status is not supported in the driver.');
}
/**
* Opens connection and channel.
*/
protected function open()
{
if ($this->channel) {
return;
}
$this->connection = new AMQPStreamConnection($this->host, $this->port, $this->user, $this->password, $this->vhost);
$this->channel = $this->connection->channel();
$this->channel->queue_declare($this->queueName, false, true, false, false);
$this->channel->exchange_declare($this->exchangeName, 'direct', false, true, false);
$this->channel->queue_bind($this->queueName, $this->exchangeName);
}
/**
* Closes connection and channel.
*/
protected function close()
{
if (!$this->channel) {
return;
}
$this->channel->close();
$this->connection->close();
}
}