|
| 1 | +<?php |
| 2 | +namespace Enqueue\LaravelQueue; |
| 3 | + |
| 4 | +use Enqueue\Consumption\ChainExtension; |
| 5 | +use Enqueue\Consumption\Context\MessageReceived; |
| 6 | +use Enqueue\Consumption\Context\PostMessageReceived; |
| 7 | +use Enqueue\Consumption\Context\PreConsume; |
| 8 | +use Enqueue\Consumption\Context\Start; |
| 9 | +use Enqueue\Consumption\Extension\LimitConsumedMessagesExtension; |
| 10 | +use Enqueue\Consumption\MessageReceivedExtensionInterface; |
| 11 | +use Enqueue\Consumption\PostMessageReceivedExtensionInterface; |
| 12 | +use Enqueue\Consumption\PreConsumeExtensionInterface; |
| 13 | +use Enqueue\Consumption\QueueConsumer; |
| 14 | +use Enqueue\Consumption\Result; |
| 15 | +use Enqueue\Consumption\StartExtensionInterface; |
| 16 | +use Enqueue\LaravelQueue\Queue; |
| 17 | +use Illuminate\Queue\WorkerOptions; |
| 18 | + |
| 19 | +class Worker extends \Illuminate\Queue\Worker implements |
| 20 | + StartExtensionInterface, |
| 21 | + PreConsumeExtensionInterface, |
| 22 | + MessageReceivedExtensionInterface, |
| 23 | + PostMessageReceivedExtensionInterface |
| 24 | +{ |
| 25 | + protected $connectionName; |
| 26 | + |
| 27 | + protected $queueNames; |
| 28 | + |
| 29 | + protected $queue; |
| 30 | + |
| 31 | + protected $options; |
| 32 | + |
| 33 | + protected $lastRestart; |
| 34 | + |
| 35 | + protected $interop = false; |
| 36 | + |
| 37 | + protected $stopped = false; |
| 38 | + |
| 39 | + protected $job; |
| 40 | + |
| 41 | + public function daemon($connectionName, $queueNames, WorkerOptions $options) |
| 42 | + { |
| 43 | + $this->connectionName = $connectionName; |
| 44 | + $this->queueNames = $queueNames; |
| 45 | + $this->options = $options; |
| 46 | + |
| 47 | + /** @var Queue $queue */ |
| 48 | + $this->queue = $this->getManager()->connection($connectionName); |
| 49 | + $this->interop = $this->queue instanceof Queue; |
| 50 | + |
| 51 | + if (false == $this->interop) { |
| 52 | + parent::daemon($connectionName, $this->queue, $options); |
| 53 | + } |
| 54 | + |
| 55 | + $context = $this->queue->getQueueInteropContext(); |
| 56 | + $queueConsumer = new QueueConsumer($context, new ChainExtension([$this])); |
| 57 | + foreach (explode(',', $queueNames) as $queueName) { |
| 58 | + $queueConsumer->bindCallback($queueName, function() { |
| 59 | + $this->runJob($this->job, $this->connectionName, $this->options); |
| 60 | + |
| 61 | + return Result::ALREADY_ACKNOWLEDGED; |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + $queueConsumer->consume(); |
| 66 | + } |
| 67 | + |
| 68 | + public function runNextJob($connectionName, $queueNames, WorkerOptions $options) |
| 69 | + { |
| 70 | + $this->connectionName = $connectionName; |
| 71 | + $this->queueNames = $queueNames; |
| 72 | + $this->options = $options; |
| 73 | + |
| 74 | + /** @var Queue $queue */ |
| 75 | + $this->queue = $this->getManager()->connection($connectionName); |
| 76 | + $this->interop = $this->queue instanceof Queue; |
| 77 | + |
| 78 | + if (false == $this->interop) { |
| 79 | + parent::daemon($connectionName, $this->queue, $options); |
| 80 | + } |
| 81 | + |
| 82 | + $context = $this->queue->getQueueInteropContext(); |
| 83 | + |
| 84 | + $queueConsumer = new QueueConsumer($context, new ChainExtension([ |
| 85 | + $this, |
| 86 | + new LimitConsumedMessagesExtension(1), |
| 87 | + ])); |
| 88 | + |
| 89 | + foreach (explode(',', $queueNames) as $queueName) { |
| 90 | + $queueConsumer->bindCallback($queueName, function() { |
| 91 | + $this->runJob($this->job, $this->connectionName, $this->options); |
| 92 | + |
| 93 | + return Result::ALREADY_ACKNOWLEDGED; |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + $queueConsumer->consume(); |
| 98 | + } |
| 99 | + |
| 100 | + public function onStart(Start $context): void |
| 101 | + { |
| 102 | + if ($this->supportsAsyncSignals()) { |
| 103 | + $this->listenForSignals(); |
| 104 | + } |
| 105 | + |
| 106 | + $this->lastRestart = $this->getTimestampOfLastQueueRestart(); |
| 107 | + |
| 108 | + if ($this->stopped) { |
| 109 | + $context->interruptExecution(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public function onPreConsume(PreConsume $context): void |
| 114 | + { |
| 115 | + if (! $this->daemonShouldRun($this->options, $this->connectionName, $this->queueNames)) { |
| 116 | + $this->pauseWorker($this->options, $this->lastRestart); |
| 117 | + } |
| 118 | + |
| 119 | + if ($this->stopped) { |
| 120 | + $context->interruptExecution(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public function onMessageReceived(MessageReceived $context): void |
| 125 | + { |
| 126 | + $this->job = $this->queue->convertMessageToJob( |
| 127 | + $context->getMessage(), |
| 128 | + $context->getConsumer() |
| 129 | + ); |
| 130 | + |
| 131 | + if ($this->supportsAsyncSignals()) { |
| 132 | + $this->registerTimeoutHandler($this->job, $this->options); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public function onPostMessageReceived(PostMessageReceived $context): void |
| 137 | + { |
| 138 | + $this->stopIfNecessary($this->options, $this->lastRestart, $this->job); |
| 139 | + |
| 140 | + if ($this->stopped) { |
| 141 | + $context->interruptExecution(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + public function stop($status = 0) |
| 146 | + { |
| 147 | + if ($this->interop) { |
| 148 | + $this->stopped = true; |
| 149 | + |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + parent::stop($status); |
| 154 | + } |
| 155 | +} |
| 156 | + |
0 commit comments