|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\LaravelQueue; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Queue\Queue as QueueContract; |
| 6 | +use Illuminate\Queue\Queue as BaseQueue; |
| 7 | +use Interop\Queue\PsrContext; |
| 8 | + |
| 9 | +class Queue extends BaseQueue implements QueueContract |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var string |
| 13 | + */ |
| 14 | + protected $queueName; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var int |
| 18 | + */ |
| 19 | + protected $timeToRun; |
| 20 | + /** |
| 21 | + * @var PsrContext |
| 22 | + */ |
| 23 | + private $psrContext; |
| 24 | + |
| 25 | + /** |
| 26 | + * @param PsrContext $psrContext |
| 27 | + * @param string $queueName |
| 28 | + * @param int $timeToRun |
| 29 | + */ |
| 30 | + public function __construct(PsrContext $psrContext, $queueName, $timeToRun) |
| 31 | + { |
| 32 | + $this->psrContext = $psrContext; |
| 33 | + $this->queueName = $queueName; |
| 34 | + $this->timeToRun = $timeToRun; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * {@inheritdoc} |
| 39 | + */ |
| 40 | + public function size($queue = null) |
| 41 | + { |
| 42 | + return 0; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * {@inheritdoc} |
| 47 | + */ |
| 48 | + public function push($job, $data = '', $queue = null) |
| 49 | + { |
| 50 | + return $this->pushRaw($this->createPayload($job, $data), $queue); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Push a new job onto the queue. |
| 55 | + * |
| 56 | + * @param string $queue |
| 57 | + * @param string $job |
| 58 | + * @param mixed $data |
| 59 | + * |
| 60 | + * @return mixed |
| 61 | + */ |
| 62 | + public function pushOn($queue, $job, $data = '') |
| 63 | + { |
| 64 | + new \LogicException('to be implemented'); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * {@inheritdoc} |
| 69 | + */ |
| 70 | + public function pushRaw($payload, $queue = null, array $options = []) |
| 71 | + { |
| 72 | + return $this->psrContext->createProducer()->send( |
| 73 | + $this->getQueue($queue), |
| 74 | + $this->psrContext->createMessage($payload) |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * {@inheritdoc} |
| 80 | + */ |
| 81 | + public function later($delay, $job, $data = '', $queue = null) |
| 82 | + { |
| 83 | + new \LogicException('to be implemented'); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * {@inheritdoc} |
| 88 | + */ |
| 89 | + public function pop($queue = null) |
| 90 | + { |
| 91 | + $queue = $this->getQueue($queue); |
| 92 | + |
| 93 | + $psrConsumer = $this->psrContext->createConsumer($queue); |
| 94 | + if ($psrMessage = $psrConsumer->receive(1000)) { // 1 sec |
| 95 | + return new Job( |
| 96 | + $this->container, |
| 97 | + $this->psrContext, |
| 98 | + $psrConsumer, |
| 99 | + $psrMessage, |
| 100 | + $this->connectionName |
| 101 | + ); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Get the queue or return the default. |
| 107 | + * |
| 108 | + * @param string|null $queue |
| 109 | + * |
| 110 | + * @return \Interop\Queue\PsrQueue |
| 111 | + */ |
| 112 | + public function getQueue($queue = null) |
| 113 | + { |
| 114 | + return $this->psrContext->createQueue($queue ?: $this->queueName); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @return PsrContext |
| 119 | + */ |
| 120 | + public function getPsrContext() |
| 121 | + { |
| 122 | + return $this->psrContext; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @return int |
| 127 | + */ |
| 128 | + public function getTimeToRun() |
| 129 | + { |
| 130 | + return $this->timeToRun; |
| 131 | + } |
| 132 | +} |
0 commit comments