Skip to content

Commit a0a5251

Browse files
committed
Merge branch 'feature/use-custom-job-class'
2 parents 8f01172 + 503158a commit a0a5251

File tree

5 files changed

+162
-32
lines changed

5 files changed

+162
-32
lines changed

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ Add connection to `config/queue.php`:
6161
'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
6262
'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
6363
],
64+
'queue' => [
65+
'job' => VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob::class,
66+
],
6467
],
6568

6669
/*
@@ -162,6 +165,92 @@ When you want to instruct RabbitMQ to reroute failed messages to a exchange or a
162165
],
163166
```
164167

168+
### Use your own RabbitMQJob class
169+
Sometimes you have to work with messages published by another application.
170+
Those messages probably won't respect Laravel's job payload schema.
171+
The problem with these messages is that, Laravel workers won't be able to determine the actual job or class to execute.
172+
173+
You can extend the build-in `RabbitMQJob::class` and within the queue connection config, you can define your own class.
174+
When you specify an `job` key in the config, with your own class name, every message retrieved from the broker will get wrapped by your own class.
175+
176+
An example for the config:
177+
178+
```php
179+
'connections' => [
180+
// ...
181+
182+
'rabbitmq' => [
183+
// ...
184+
185+
'options' => [
186+
'queue' => [
187+
// ...
188+
189+
'job' => \App\Queue\Jobs\RabbitMQJob::class,
190+
],
191+
],
192+
],
193+
194+
// ...
195+
],
196+
```
197+
198+
An example of your own job class:
199+
200+
```php
201+
<?php
202+
203+
namespace App\Queue\Jobs;
204+
205+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob as BaseJob;
206+
207+
class RabbitMQJob extends BaseJob
208+
{
209+
210+
/**
211+
* Fire the job.
212+
*
213+
* @return void
214+
*/
215+
public function fire()
216+
{
217+
$payload = $this->payload();
218+
219+
$class = WhatheverClassNameToExecute::class;
220+
$method = 'handle';
221+
222+
($this->instance = $this->resolve($class))->{$method}($this, $payload);
223+
}
224+
}
225+
226+
```
227+
228+
Or maybe you want to add extra properties to the payload:
229+
230+
```php
231+
<?php
232+
233+
namespace App\Queue\Jobs;
234+
235+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob as BaseJob;
236+
237+
class RabbitMQJob extends BaseJob
238+
{
239+
/**
240+
* Get the decoded body of the job.
241+
*
242+
* @return array
243+
*/
244+
public function payload()
245+
{
246+
return [
247+
'job' => 'WhatheverFullyQualifiedClassNameToExecute@handle',
248+
'data' => json_decode($this->getRawBody(), true)
249+
];
250+
}
251+
}
252+
```
253+
165254
## Laravel Usage
166255

167256
Once you completed the configuration you can use Laravel Queue API. If you used other queue drivers you do not need to change anything else. If you do not know how to use Queue API, please refer to the official Laravel documentation: http://laravel.com/docs/queues

config/rabbitmq.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
3030
'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
3131
],
32+
'queue' => [
33+
'job' => VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob::class,
34+
],
3235
],
3336

3437
/*

src/Consumer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use PhpAmqpLib\Message\AMQPMessage;
1313
use Symfony\Component\Debug\Exception\FatalThrowableError;
1414
use Throwable;
15-
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob;
1615
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue;
1716

1817
class Consumer extends Worker
@@ -74,17 +73,19 @@ public function daemon($connectionName, $queue, WorkerOptions $options): void
7473
null
7574
);
7675

76+
$jobClass = $connection->getJobClass();
77+
7778
$this->channel->basic_consume(
7879
$queue,
7980
$this->consumerTag,
8081
false,
8182
false,
8283
false,
8384
false,
84-
function (AMQPMessage $message) use ($connection, $options, $connectionName, $queue): void {
85+
function (AMQPMessage $message) use ($connection, $options, $connectionName, $queue, $jobClass): void {
8586
$this->gotJob = true;
8687

87-
$job = new RabbitMQJob(
88+
$job = new $jobClass(
8889
$this->container,
8990
$connection,
9091
$message,

src/Horizon/RabbitMQQueue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function later($delay, $job, $data = '', $queue = null)
7474
public function pop($queue = null)
7575
{
7676
return tap(parent::pop($queue), function ($result) use ($queue): void {
77-
if ($result instanceof RabbitMQJob) {
77+
if (is_a($result, RabbitMQJob::class, true)) {
7878
$this->event($this->getQueue($queue), new JobReserved($result->getRawBody()));
7979
}
8080
});

0 commit comments

Comments
 (0)