|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace VladimirYuldashev\LaravelQueueRabbitMQ\Horizon; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Events\Dispatcher; |
| 6 | +use Laravel\Horizon\Events\JobDeleted; |
| 7 | +use Laravel\Horizon\Events\JobPushed; |
| 8 | +use Laravel\Horizon\Events\JobReserved; |
| 9 | +use Laravel\Horizon\JobId; |
| 10 | +use Laravel\Horizon\JobPayload; |
| 11 | +use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob; |
| 12 | +use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue as BaseRabbitMQQueue; |
| 13 | + |
| 14 | +class RabbitMQQueue extends BaseRabbitMQQueue |
| 15 | +{ |
| 16 | + /** |
| 17 | + * The job that last pushed to queue via the "push" method. |
| 18 | + * |
| 19 | + * @var object|string |
| 20 | + */ |
| 21 | + protected $lastPushed; |
| 22 | + |
| 23 | + /** |
| 24 | + * Get the number of queue jobs that are ready to process. |
| 25 | + * |
| 26 | + * @param string|null $queue |
| 27 | + * @return int |
| 28 | + */ |
| 29 | + public function readyNow($queue = null): int |
| 30 | + { |
| 31 | + return $this->size($queue); |
| 32 | + } |
| 33 | + |
| 34 | + /** {@inheritdoc} */ |
| 35 | + public function push($job, $data = '', $queue = null) |
| 36 | + { |
| 37 | + $this->lastPushed = $job; |
| 38 | + |
| 39 | + return parent::push($job, $data, $queue); |
| 40 | + } |
| 41 | + |
| 42 | + /** {@inheritdoc} */ |
| 43 | + public function pushRaw($payload, $queueName = null, array $options = []) |
| 44 | + { |
| 45 | + $payload = (new JobPayload($payload))->prepare($this->lastPushed)->value; |
| 46 | + |
| 47 | + return tap(parent::pushRaw($payload, $queueName, $options), function () use ($queueName, $payload) { |
| 48 | + $this->event($queueName ?: $this->queueName, new JobPushed($payload)); |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + /** {@inheritdoc} */ |
| 53 | + public function later($delay, $job, $data = '', $queueName = null) |
| 54 | + { |
| 55 | + $payload = (new JobPayload($this->createPayload($job, $data)))->prepare($job)->value; |
| 56 | + |
| 57 | + return tap(parent::pushRaw($payload, $queueName, ['delay' => $this->secondsUntil($delay)]), function () use ($payload, $queueName) { |
| 58 | + $this->event($queueName ?: $this->queueName, new JobPushed($payload)); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + /** {@inheritdoc} */ |
| 63 | + public function pop($queueName = null) |
| 64 | + { |
| 65 | + return tap(parent::pop($queueName), function ($result) use ($queueName) { |
| 66 | + if ($result instanceof RabbitMQJob) { |
| 67 | + $this->event($queueName ?: $this->queueName, new JobReserved($result->getRawBody())); |
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + /** {@inheritdoc} */ |
| 73 | + public function release($delay, $job, $data, $queue, $attempts = 0) |
| 74 | + { |
| 75 | + $this->lastPushed = $job; |
| 76 | + |
| 77 | + return parent::release($delay, $job, $data, $queue, $attempts); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Fire the job deleted event. |
| 82 | + * |
| 83 | + * @param string $queueName |
| 84 | + * @param RabbitMQJob $job |
| 85 | + * @return void |
| 86 | + * @throws \Illuminate\Contracts\Container\BindingResolutionException |
| 87 | + */ |
| 88 | + public function deleteReserved($queueName, $job): void |
| 89 | + { |
| 90 | + $this->event($queueName ?: $this->queueName, new JobDeleted($job, $job->getRawBody())); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Fire the given event if a dispatcher is bound. |
| 95 | + * |
| 96 | + * @param string $queue |
| 97 | + * @param mixed $event |
| 98 | + * @return void |
| 99 | + * @throws \Illuminate\Contracts\Container\BindingResolutionException |
| 100 | + */ |
| 101 | + protected function event($queue, $event): void |
| 102 | + { |
| 103 | + if ($this->container && $this->container->bound(Dispatcher::class)) { |
| 104 | + $this->container->make(Dispatcher::class)->dispatch( |
| 105 | + $event->connection($this->getConnectionName())->queue($queue) |
| 106 | + ); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** {@inheritdoc} */ |
| 111 | + protected function getRandomId(): string |
| 112 | + { |
| 113 | + return JobId::generate(); |
| 114 | + } |
| 115 | +} |
0 commit comments