Skip to content

Commit c022ad3

Browse files
committed
first commit
1 parent a798583 commit c022ad3

22 files changed

+1723
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
composer.lock

README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,52 @@
1-
laravel-queue-rabbitmq
1+
RabbitMQ driver for Laravel
22
======================
3+
4+
####Installation
5+
6+
Require this package in your composer.json and run composer update:
7+
8+
fintech-fab/laravel-queue-rabbitmq
9+
10+
or run:
11+
12+
composer require fintech-fab/laravel-queue-rabbitmq:*
13+
14+
After composer update is finished you need to add ServiceProvider to your `providers` array in app.php:
15+
16+
17+
'FintechFab\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider',
18+
19+
20+
now you are able to configure your connections in queue.php:
21+
22+
return array(
23+
24+
'default' => 'rabbitmq',
25+
26+
'connections' => array(
27+
28+
'rabbitmq' => array(
29+
'driver' => 'rabbitmq',
30+
31+
'host' => '',
32+
'port' => '',
33+
34+
'vhost' => '',
35+
'login' => '',
36+
'password' => '',
37+
38+
'queue' => '', // name of the default queue
39+
'exchange_name' => '', // name of the exchange
40+
),
41+
42+
),
43+
44+
);
45+
46+
You can also find these examples in src/examples folder.
47+
48+
####PHPUnit
49+
Unit tests will be provided soon.
50+
51+
####Contribution
52+
You can contribute to this package by discovering buys and opening issues. Enjoy!

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "fintech-fab/laravel-queue-rabbitmq",
3+
"description": "RabbitMQ driver for Laravel Queue",
4+
"license": "Apache-2.0",
5+
"authors": [
6+
{
7+
"name": "FINTECH_FAB",
8+
"email": "dev@fintech-fab.ru"
9+
}
10+
],
11+
"require": {
12+
"php": ">=5.3.0",
13+
"illuminate/support": "4.0.x",
14+
"illuminate/queue": "4.0.*@dev"
15+
},
16+
"suggest": {
17+
"ext-amqp": "PECL extension for the AMQP protocol - this is preferred",
18+
"videlalvaro/php-amqplib": "Pure PHP implementation of the AMQP protocol. If you are not able to install PECL extension - feel free to use this one"
19+
},
20+
"autoload": {
21+
"psr-0": {
22+
"FintechFab\\LaravelQueueRabbitMQ": "src/"
23+
}
24+
},
25+
"minimum-stability": "dev"
26+
}

phpunit.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Package Test Suite">
15+
<directory suffix=".php">./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
</phpunit>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php namespace FintechFab\LaravelQueueRabbitMQ;
2+
3+
use FintechFab\LaravelQueueRabbitMQ\Queue\Connectors\RabbitMQConnector;
4+
use Illuminate\Support\ServiceProvider;
5+
use Queue;
6+
7+
class LaravelQueueRabbitMQServiceProvider extends ServiceProvider
8+
{
9+
10+
/**
11+
* Register the service provider.
12+
*
13+
* @return void
14+
*/
15+
public function register()
16+
{
17+
$this->app->booted(function () {
18+
19+
Queue::extend('rabbitmq', function () {
20+
return new RabbitMQConnector;
21+
});
22+
23+
});
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php namespace FintechFab\LaravelQueueRabbitMQ\Queue\Connectors;
2+
3+
use AMQPConnection;
4+
use FintechFab\LaravelQueueRabbitMQ\Queue\RabbitMQQueue;
5+
use Illuminate\Queue\Connectors\ConnectorInterface;
6+
7+
class RabbitMQConnector implements ConnectorInterface
8+
{
9+
10+
/**
11+
* Establish a queue connection.
12+
*
13+
* @param array $config
14+
*
15+
* @return \Illuminate\Queue\QueueInterface
16+
*/
17+
public function connect(array $config)
18+
{
19+
20+
// create connection with AMQP
21+
$connection = new AMQPConnection($config);
22+
$connection->connect();
23+
24+
return new RabbitMQQueue(
25+
$connection,
26+
$config['queue'],
27+
$config['exchange_name']
28+
);
29+
}
30+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php namespace FintechFab\LaravelQueueRabbitMQ\Queue\Jobs;
2+
3+
use AMQPEnvelope;
4+
use AMQPQueue;
5+
use Illuminate\Queue\Jobs\Job;
6+
use Queue;
7+
8+
class RabbitMQJob extends Job
9+
{
10+
11+
protected $queue;
12+
protected $envelope;
13+
14+
public function __construct($container, AMQPQueue $queue, AMQPEnvelope $envelope)
15+
{
16+
$this->container = $container;
17+
$this->queue = $queue;
18+
$this->envelope = $envelope;
19+
}
20+
21+
/**
22+
* Fire the job.
23+
*
24+
* @return void
25+
*/
26+
public function fire()
27+
{
28+
$this->resolveAndFire(json_decode($this->envelope->getBody(), true));
29+
}
30+
31+
/**
32+
* Delete the job from the queue.
33+
*
34+
* @return void
35+
*/
36+
public function delete()
37+
{
38+
$this->queue->ack($this->envelope->getDeliveryTag());
39+
}
40+
41+
/**
42+
* Release the job back into the queue.
43+
*
44+
* @param int $delay
45+
*
46+
* @return void
47+
*/
48+
public function release($delay = 0)
49+
{
50+
$this->delete();
51+
52+
$body = $this->envelope->getBody();
53+
$body = json_decode($body, true);
54+
55+
$attempts = $this->attempts();
56+
57+
// write attempts to body
58+
$body['data']['attempts'] = $attempts + 1;
59+
60+
// push back to a queue
61+
Queue::push($body['job'], $body['data']);
62+
}
63+
64+
/**
65+
* Get the number of times the job has been attempted.
66+
*
67+
* @return int
68+
*/
69+
public function attempts()
70+
{
71+
$body = json_decode($this->envelope->getBody(), true);
72+
73+
return isset($body['data']['attempts']) ? $body['data']['attempts'] : 0;
74+
}
75+
76+
/**
77+
* Get the job identifier.
78+
*
79+
* @return string
80+
*/
81+
public function getJobId()
82+
{
83+
return $this->envelope->getMessageId();
84+
}
85+
86+
}

0 commit comments

Comments
 (0)