Skip to content
This repository was archived by the owner on Sep 3, 2020. It is now read-only.

Commit bc6a74d

Browse files
author
pufik
committed
declaring queue edited
1 parent 1523473 commit bc6a74d

File tree

3 files changed

+138
-164
lines changed

3 files changed

+138
-164
lines changed

src/FintechFab/LaravelQueueRabbitMQ/Queue/RabbitMQQueue.php

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class RabbitMQQueue extends Queue implements QueueContract
2020
protected $defaultQueue;
2121
protected $configQueue;
2222
protected $configExchange;
23+
protected $arguments;
2324

2425
protected static $initedQueues = [];
2526

@@ -63,10 +64,8 @@ public function push($job, $data = '', $queue = null)
6364
public function pushRaw($payload, $queue = null, array $options = [])
6465
{
6566
$queue = $this->getQueueName($queue);
66-
$this->declareQueue($queue);
67-
if (isset($options['delay'])) {
68-
$queue = $this->declareDelayedQueue($queue, $options['delay']);
69-
}
67+
$declaredQueue = $this->declareQueue($queue, $options);
68+
7069

7170
// push job to a queue
7271
$message = new AMQPMessage($payload, [
@@ -75,7 +74,7 @@ public function pushRaw($payload, $queue = null, array $options = [])
7574
]);
7675

7776
// push task to a queue
78-
$this->channel->basic_publish($message, $queue, $queue);
77+
$this->channel->basic_publish($message, $declaredQueue, $declaredQueue);
7978

8079
return true;
8180
}
@@ -135,51 +134,30 @@ private function getChannel()
135134
}
136135

137136
/**
138-
* @param string $name
137+
* @param $name
138+
* @param $options
139+
* @return string
139140
*/
140-
private function declareQueue($name)
141+
private function declareQueue($name, $options)
141142
{
143+
$arguments = null;
142144
$name = $this->getQueueName($name);
143145

144-
if (in_array($name, self::$initedQueues)) {
145-
return;
146-
} else {
147-
self::$initedQueues[] = $name;
146+
if (isset($options['delay'])) {
147+
$delay = $this->getSeconds($options['delay']);
148+
$name .= '_deferred_' . $delay;
149+
$arguments = new AMQPTable([
150+
'x-dead-letter-exchange' => $name,
151+
'x-dead-letter-routing-key' => $name,
152+
'x-message-ttl' => $delay * 1000,
153+
]);
148154
}
149155

150-
$this->channel->exchange_declare(
151-
$name,
152-
$this->configExchange['type'],
153-
$this->configExchange['passive'],
154-
$this->configExchange['durable'],
155-
$this->configExchange['auto_delete']
156-
);
157-
158-
$this->channel->queue_declare(
159-
$name,
160-
$this->configQueue['passive'],
161-
$this->configQueue['durable'],
162-
$this->configQueue['exclusive'],
163-
$this->configQueue['auto_delete']
164-
);
165-
166-
$this->channel->queue_bind($name, $name, $name);
167-
}
168-
169-
/**
170-
* @param string $destination
171-
* @param DateTime|int $delay
172-
*
173-
* @return string
174-
*/
175-
private function declareDelayedQueue($destination, $delay)
176-
{
177-
$delay = $this->getSeconds($delay);
178-
$destination = $this->getQueueName($destination);
179-
$name = $this->getQueueName($destination) . '_deferred_' . $delay;
180-
156+
//avoid redeclaring queue
181157
if (!in_array($name, self::$initedQueues)) {
158+
// set flag
182159
self::$initedQueues[] = $name;
160+
183161
// declare exchange
184162
$this->channel->exchange_declare(
185163
$name,
@@ -197,11 +175,7 @@ private function declareDelayedQueue($destination, $delay)
197175
$this->configQueue['exclusive'],
198176
$this->configQueue['auto_delete'],
199177
false,
200-
new AMQPTable([
201-
'x-dead-letter-exchange' => $destination,
202-
'x-dead-letter-routing-key' => $destination,
203-
'x-message-ttl' => $delay * 1000,
204-
])
178+
$arguments
205179
);
206180

207181
// bind queue to the exchange

src/examples/app.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
return [
44

5-
'providers' => append_config([
6-
'FintechFab\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider',
7-
]),
5+
'providers' => append_config([
6+
'FintechFab\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider',
7+
]),
88

99
];

src/examples/queue.php

Lines changed: 114 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -2,119 +2,119 @@
22

33
return [
44

5-
/*
6-
|--------------------------------------------------------------------------
7-
| Default Queue Driver
8-
|--------------------------------------------------------------------------
9-
|
10-
| The Laravel queue API supports a variety of back-ends via an unified
11-
| API, giving you convenient access to each back-end using the same
12-
| syntax for each one. Here you may set the default queue driver.
13-
|
14-
| Supported: "null", "sync", "database", "beanstalkd",
15-
| "sqs", "iron", "redis"
16-
|
17-
*/
18-
19-
'default' => env('QUEUE_DRIVER', 'sync'),
20-
21-
/*
22-
|--------------------------------------------------------------------------
23-
| Queue Connections
24-
|--------------------------------------------------------------------------
25-
|
26-
| Here you may configure the connection information for each server that
27-
| is used by your application. A default configuration has been added
28-
| for each back-end shipped with Laravel. You are free to add more.
29-
|
30-
*/
31-
32-
'connections' => [
33-
34-
'sync' => [
35-
'driver' => 'sync',
36-
],
37-
38-
'database' => [
39-
'driver' => 'database',
40-
'table' => 'jobs',
41-
'queue' => 'default',
42-
'expire' => 60,
43-
],
44-
45-
'beanstalkd' => [
46-
'driver' => 'beanstalkd',
47-
'host' => 'localhost',
48-
'queue' => 'default',
49-
'ttr' => 60,
50-
],
51-
52-
'sqs' => [
53-
'driver' => 'sqs',
54-
'key' => 'your-public-key',
55-
'secret' => 'your-secret-key',
56-
'queue' => 'your-queue-url',
57-
'region' => 'us-east-1',
58-
],
59-
60-
'iron' => [
61-
'driver' => 'iron',
62-
'host' => 'mq-aws-us-east-1.iron.io',
63-
'token' => 'your-token',
64-
'project' => 'your-project-id',
65-
'queue' => 'your-queue-name',
66-
'encrypt' => true,
67-
],
68-
69-
'redis' => [
70-
'driver' => 'redis',
71-
'queue' => 'default',
72-
'expire' => 60,
73-
],
74-
75-
'rabbitmq' => [
76-
'driver' => 'rabbitmq',
77-
78-
'host' => env('RABBITMQ_HOST', '127.0.0.1'),
79-
'port' => env('RABBITMQ_PORT', 5672),
80-
81-
'vhost' => env('RABBITMQ_VHOST', '/'),
82-
'login' => env('RABBITMQ_LOGIN', 'guest'),
83-
'password' => env('RABBITMQ_PASSWORD', 'guest'),
84-
85-
'queue' => env('RABBITMQ_QUEUE'), // name of the default queue,
86-
87-
'queue_params' => [
88-
'passive' => env('RABBITMQ_QUEUE_PASSIVE', false),
89-
'durable' => env('RABBITMQ_QUEUE_DURABLE', true),
90-
'exclusive' => env('RABBITMQ_QUEUE_EXCLUSIVE', false),
91-
'auto_delete' => env('RABBITMQ_QUEUE_AUTODELETE', false),
92-
],
93-
94-
'exchange_params' => [
95-
'type' => env('RABBITMQ_EXCHANGE_TYPE', 'direct'), // more info at http://www.rabbitmq.com/tutorials/amqp-concepts.html
96-
'passive' => env('RABBITMQ_EXCHANGE_PASSIVE', false),
97-
'durable' => env('RABBITMQ_EXCHANGE_DURABLE', true), // the exchange will survive server restarts
98-
'auto_delete' => env('RABBITMQ_EXCHANGE_AUTODELETE', false),
99-
],
100-
101-
],
102-
103-
],
104-
105-
/*
106-
|--------------------------------------------------------------------------
107-
| Failed Queue Jobs
108-
|--------------------------------------------------------------------------
109-
|
110-
| These options configure the behavior of failed queue job logging so you
111-
| can control which database and table are used to store the jobs that
112-
| have failed. You may change them to any database / table you wish.
113-
|
114-
*/
115-
116-
'failed' => [
117-
'database' => 'mysql', 'table' => 'failed_jobs',
118-
],
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Default Queue Driver
8+
|--------------------------------------------------------------------------
9+
|
10+
| The Laravel queue API supports a variety of back-ends via an unified
11+
| API, giving you convenient access to each back-end using the same
12+
| syntax for each one. Here you may set the default queue driver.
13+
|
14+
| Supported: "null", "sync", "database", "beanstalkd",
15+
| "sqs", "iron", "redis"
16+
|
17+
*/
18+
19+
'default' => env('QUEUE_DRIVER', 'sync'),
20+
21+
/*
22+
|--------------------------------------------------------------------------
23+
| Queue Connections
24+
|--------------------------------------------------------------------------
25+
|
26+
| Here you may configure the connection information for each server that
27+
| is used by your application. A default configuration has been added
28+
| for each back-end shipped with Laravel. You are free to add more.
29+
|
30+
*/
31+
32+
'connections' => [
33+
34+
'sync' => [
35+
'driver' => 'sync',
36+
],
37+
38+
'database' => [
39+
'driver' => 'database',
40+
'table' => 'jobs',
41+
'queue' => 'default',
42+
'expire' => 60,
43+
],
44+
45+
'beanstalkd' => [
46+
'driver' => 'beanstalkd',
47+
'host' => 'localhost',
48+
'queue' => 'default',
49+
'ttr' => 60,
50+
],
51+
52+
'sqs' => [
53+
'driver' => 'sqs',
54+
'key' => 'your-public-key',
55+
'secret' => 'your-secret-key',
56+
'queue' => 'your-queue-url',
57+
'region' => 'us-east-1',
58+
],
59+
60+
'iron' => [
61+
'driver' => 'iron',
62+
'host' => 'mq-aws-us-east-1.iron.io',
63+
'token' => 'your-token',
64+
'project' => 'your-project-id',
65+
'queue' => 'your-queue-name',
66+
'encrypt' => true,
67+
],
68+
69+
'redis' => [
70+
'driver' => 'redis',
71+
'queue' => 'default',
72+
'expire' => 60,
73+
],
74+
75+
'rabbitmq' => [
76+
'driver' => 'rabbitmq',
77+
78+
'host' => env('RABBITMQ_HOST', '127.0.0.1'),
79+
'port' => env('RABBITMQ_PORT', 5672),
80+
81+
'vhost' => env('RABBITMQ_VHOST', '/'),
82+
'login' => env('RABBITMQ_LOGIN', 'guest'),
83+
'password' => env('RABBITMQ_PASSWORD', 'guest'),
84+
85+
'queue' => env('RABBITMQ_QUEUE'), // name of the default queue,
86+
87+
'queue_params' => [
88+
'passive' => env('RABBITMQ_QUEUE_PASSIVE', false),
89+
'durable' => env('RABBITMQ_QUEUE_DURABLE', true),
90+
'exclusive' => env('RABBITMQ_QUEUE_EXCLUSIVE', false),
91+
'auto_delete' => env('RABBITMQ_QUEUE_AUTODELETE', false),
92+
],
93+
94+
'exchange_params' => [
95+
'type' => env('RABBITMQ_EXCHANGE_TYPE', 'direct'), // more info at http://www.rabbitmq.com/tutorials/amqp-concepts.html
96+
'passive' => env('RABBITMQ_EXCHANGE_PASSIVE', false),
97+
'durable' => env('RABBITMQ_EXCHANGE_DURABLE', true), // the exchange will survive server restarts
98+
'auto_delete' => env('RABBITMQ_EXCHANGE_AUTODELETE', false),
99+
],
100+
101+
],
102+
103+
],
104+
105+
/*
106+
|--------------------------------------------------------------------------
107+
| Failed Queue Jobs
108+
|--------------------------------------------------------------------------
109+
|
110+
| These options configure the behavior of failed queue job logging so you
111+
| can control which database and table are used to store the jobs that
112+
| have failed. You may change them to any database / table you wish.
113+
|
114+
*/
115+
116+
'failed' => [
117+
'database' => 'mysql', 'table' => 'failed_jobs',
118+
],
119119

120120
];

0 commit comments

Comments
 (0)