milkmeowo /
laravel-queue-aliyun-mns
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * Laravel-Mns -- 阿里云消息队列(MNS)的 Laravel 适配。 |
||
| 5 | * |
||
| 6 | * This file is part of the milkmeowo/laravel-mns. |
||
| 7 | * |
||
| 8 | * (c) Milkmeowo <[email protected]> |
||
| 9 | * @link: https://github.com/milkmeowo/laravel-queue-aliyun-mns |
||
| 10 | * |
||
| 11 | * This source file is subject to the MIT license that is bundled |
||
| 12 | * with this source code in the file LICENSE. |
||
| 13 | */ |
||
| 14 | |||
| 15 | namespace Milkmeowo\LaravelMns; |
||
| 16 | |||
| 17 | use Illuminate\Queue\Queue; |
||
| 18 | use Milkmeowo\LaravelMns\Jobs\MnsJob; |
||
| 19 | use AliyunMNS\Requests\SendMessageRequest; |
||
| 20 | use Milkmeowo\LaravelMns\Adaptors\MnsAdapter; |
||
| 21 | use AliyunMNS\Exception\MessageNotExistException; |
||
| 22 | use Illuminate\Contracts\Queue\Queue as QueueContract; |
||
| 23 | |||
| 24 | class MnsQueue extends Queue implements QueueContract |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Mns 适配器. |
||
| 28 | * |
||
| 29 | * @var MnsAdapter |
||
| 30 | */ |
||
| 31 | protected $adapter; |
||
| 32 | /** |
||
| 33 | * 默认队列. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $default; |
||
| 38 | /** |
||
| 39 | * 等待秒数. |
||
| 40 | * |
||
| 41 | * @var null |
||
| 42 | */ |
||
| 43 | private $waitSeconds; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * MnsQueue 构造. |
||
| 47 | * |
||
| 48 | * @param MnsAdapter $adapter Mns 适配器 |
||
| 49 | * @param int $waitSeconds 等待秒数 |
||
| 50 | */ |
||
| 51 | 8 | public function __construct(MnsAdapter $adapter, int $waitSeconds = 0) |
|
| 52 | { |
||
| 53 | 8 | $this->adapter = $adapter; |
|
| 54 | 8 | $this->default = $this->adapter->getQueueName(); |
|
| 55 | 8 | $this->waitSeconds = $waitSeconds; |
|
|
0 ignored issues
–
show
|
|||
| 56 | 8 | } |
|
| 57 | |||
| 58 | /** |
||
| 59 | * 获取队列长度. |
||
| 60 | * |
||
| 61 | * @param null $queue |
||
|
0 ignored issues
–
show
|
|||
| 62 | * |
||
| 63 | * @throws \Exception |
||
| 64 | * |
||
| 65 | * @return int|void |
||
| 66 | */ |
||
| 67 | 1 | public function size($queue = null) |
|
| 68 | { |
||
| 69 | 1 | throw new \Exception('The size method is not support for aliyun-mns'); |
|
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * 推送新的 Job 进队列. |
||
| 74 | * |
||
| 75 | * @param string|object $job 任务 |
||
| 76 | * @param mixed $data 数据 |
||
| 77 | * @param string $queue 队列 |
||
| 78 | * |
||
| 79 | * @return mixed |
||
| 80 | */ |
||
| 81 | 1 | public function push($job, $data = '', $queue = null) |
|
| 82 | { |
||
| 83 | 1 | $payload = $this->createPayload($job, $data); |
|
| 84 | |||
| 85 | 1 | return $this->pushRaw($payload, $queue); |
|
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * 推送 raw payload 进队列. |
||
| 90 | * |
||
| 91 | * @param string $payload 数据 |
||
| 92 | * @param string $queue 队列 |
||
| 93 | * @param array $options 选项 |
||
| 94 | * |
||
| 95 | * @return mixed |
||
| 96 | */ |
||
| 97 | 1 | public function pushRaw($payload, $queue = null, array $options = []) |
|
| 98 | { |
||
| 99 | 1 | $message = new SendMessageRequest($payload); |
|
| 100 | |||
| 101 | 1 | $queue = $this->getQueue($queue); |
|
| 102 | |||
| 103 | 1 | $response = $this->adapter->useQueue($queue)->sendMessage($message); |
|
| 104 | |||
| 105 | 1 | return $response->getMessageId(); |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * 获取默认队列名(如果当前队列名为 null)。 |
||
| 110 | * |
||
| 111 | * @param $queue |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | 6 | public function getQueue($queue) |
|
| 116 | { |
||
| 117 | 6 | return $queue ?: $this->default; |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * 延迟推送 Job 进队列. |
||
| 122 | * |
||
| 123 | * @param \DateTimeInterface|\DateInterval|int $delay 延迟时间 秒 |
||
| 124 | * @param string|object $job 任务 |
||
| 125 | * @param mixed $data 数据 |
||
| 126 | * @param string $queue 队列 |
||
| 127 | * |
||
| 128 | * @return mixed |
||
| 129 | */ |
||
| 130 | 2 | public function later($delay, $job, $data = '', $queue = null) |
|
| 131 | { |
||
| 132 | 2 | $seconds = $this->secondsUntil($delay); |
|
| 133 | 2 | $payload = $this->createPayload($job, $data); |
|
| 134 | 2 | $queue = $this->getQueue($queue); |
|
| 135 | |||
| 136 | 2 | $message = new SendMessageRequest($payload, $seconds); |
|
| 137 | |||
| 138 | 2 | $response = $this->adapter->useQueue($queue)->sendMessage($message); |
|
| 139 | |||
| 140 | 2 | return $response->getMessageId(); |
|
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * 从队列弹出下一个 Job. |
||
| 145 | * |
||
| 146 | * @param string $queue 队列 |
||
| 147 | * |
||
| 148 | * @return \Illuminate\Contracts\Queue\Job|null |
||
| 149 | */ |
||
| 150 | 2 | public function pop($queue = null) |
|
| 151 | { |
||
| 152 | 2 | $queue = $this->getQueue($queue); |
|
| 153 | |||
| 154 | try { |
||
| 155 | 2 | $response = $this->adapter->useQueue($queue)->receiveMessage($this->waitSeconds); |
|
| 156 | 1 | } catch (MessageNotExistException $e) { |
|
| 157 | 1 | $response = null; |
|
| 158 | } |
||
| 159 | 2 | if ($response) { |
|
| 160 | 1 | return new MnsJob($this->container, $this->adapter, $queue, $response); |
|
| 161 | } |
||
| 162 | 1 | } |
|
| 163 | |||
| 164 | /** |
||
| 165 | * 获取 Mns 适配器. |
||
| 166 | * |
||
| 167 | * @return MnsAdapter |
||
| 168 | */ |
||
| 169 | 1 | public function getMns() |
|
| 170 | { |
||
| 171 | 1 | return $this->adapter; |
|
| 172 | } |
||
| 173 | } |
||
| 174 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..