Skip to content

Added extra listener when horizon is used as worker. #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/Horizon/Listeners/RabbitMQFailedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace VladimirYuldashev\LaravelQueueRabbitMQ\Horizon\Listeners;

use Illuminate\Contracts\Events\Dispatcher;
use Laravel\Horizon\Events\JobFailed as HorizonJobFailed;
use Illuminate\Queue\Events\JobFailed as LaravelJobFailed;
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob;

class RabbitMQFailedEvent
{
/**
* The event dispatcher implementation.
*
* @var \Illuminate\Contracts\Events\Dispatcher
*/
public $events;

/**
* Create a new listener instance.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*/
public function __construct(Dispatcher $events)
{
$this->events = $events;
}

/**
* Handle the event.
*
* @param \Illuminate\Queue\Events\JobFailed $event
* @return void
*/
public function handle(LaravelJobFailed $event)
{
if (! $event->job instanceof RabbitMQJob) {
return;
}

$this->events->dispatch((new HorizonJobFailed(
$event->exception, $event->job, $event->job->getRawBody()
))->connection($event->connectionName)->queue($event->job->getQueue()));
}
}
4 changes: 4 additions & 0 deletions src/Queue/Connectors/RabbitMQConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Interop\Amqp\AmqpContext;
use InvalidArgumentException;
use Illuminate\Contracts\Queue\Queue;
use Illuminate\Queue\Events\JobFailed;
use Interop\Amqp\AmqpConnectionFactory;
use Enqueue\AmqpTools\DelayStrategyAware;
use Illuminate\Contracts\Events\Dispatcher;
Expand All @@ -15,6 +16,7 @@
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue;
use Interop\Amqp\AmqpConnectionFactory as InteropAmqpConnectionFactory;
use Enqueue\AmqpLib\AmqpConnectionFactory as EnqueueAmqpConnectionFactory;
use VladimirYuldashev\LaravelQueueRabbitMQ\Horizon\Listeners\RabbitMQFailedEvent;
use VladimirYuldashev\LaravelQueueRabbitMQ\Horizon\RabbitMQQueue as HorizonRabbitMQQueue;

class RabbitMQConnector implements ConnectorInterface
Expand Down Expand Up @@ -79,6 +81,8 @@ public function connect(array $config): Queue
}

if ($worker === 'horizon') {
$this->dispatcher->listen(JobFailed::class, RabbitMQFailedEvent::class);

return new HorizonRabbitMQQueue($context, $config);
}

Expand Down