1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Laravel-Mns -- 阿里云消息队列(MNS)的 Laravel 适配。 |
5
|
|
|
* |
6
|
|
|
* This file is part of the abe/laravel-mns. |
7
|
|
|
* |
8
|
|
|
* (c) Abraham Greyson <[email protected]> |
9
|
|
|
* @link: https://github.com/abrahamgreyson/laravel-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 LaravelMns; |
16
|
|
|
|
17
|
|
|
use AliyunMNS\Exception\MessageNotExistException; |
18
|
|
|
use AliyunMNS\Requests\SendMessageRequest; |
19
|
|
|
use Illuminate\Contracts\Queue\Queue as QueueContract; |
20
|
|
|
use Illuminate\Queue\Queue; |
21
|
|
|
|
22
|
|
|
class MnsQueue extends Queue implements QueueContract |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var MnsAdapter |
26
|
|
|
*/ |
27
|
|
|
protected $mns; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Custom callable to handle jobs. |
31
|
|
|
* |
32
|
|
|
* @var callable |
33
|
|
|
*/ |
34
|
|
|
protected $jobCreator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The name of default queue. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $default; |
42
|
|
|
|
43
|
7 |
|
public function __construct(MnsAdapter $mns) |
44
|
|
|
{ |
45
|
7 |
|
$this->mns = $mns; |
46
|
|
|
|
47
|
7 |
|
$this->default = $this->mns->getQueueName(); |
48
|
7 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Push a new job onto the queue. |
52
|
|
|
* |
53
|
|
|
* @param string $job |
54
|
|
|
* @param mixed $data |
55
|
|
|
* @param string $queue |
56
|
|
|
* |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
1 |
|
public function push($job, $data = '', $queue = null) |
60
|
|
|
{ |
61
|
1 |
|
$payload = $this->createPayload($job, $data); |
62
|
|
|
|
63
|
1 |
|
return $this->pushRaw($payload, $queue); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Push a raw payload onto the queue. |
68
|
|
|
* |
69
|
|
|
* @param string $payload |
70
|
|
|
* @param string $queue |
71
|
|
|
* @param array $options |
72
|
|
|
* |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
1 |
|
public function pushRaw($payload, $queue = null, array $options = []) |
76
|
|
|
{ |
77
|
1 |
|
$message = new SendMessageRequest($payload); |
78
|
1 |
|
$response = $this->mns->setQueue($queue)->sendMessage($message); |
79
|
|
|
|
80
|
1 |
|
return $response->getMessageId(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Push a new job onto the queue after a delay. |
85
|
|
|
* |
86
|
|
|
* @param \DateTime|int $delay |
87
|
|
|
* @param string $job |
88
|
|
|
* @param mixed $data |
89
|
|
|
* @param string $queue |
90
|
|
|
* |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
2 |
|
public function later($delay, $job, $data = '', $queue = null) |
94
|
|
|
{ |
95
|
2 |
|
$seconds = $this->getSeconds($delay); |
96
|
2 |
|
$payload = $this->createPayload($job, $data); |
97
|
2 |
|
$message = new SendMessageRequest($payload, $seconds); |
98
|
2 |
|
$response = $this->mns->setQueue($queue)->sendMessage($message); |
99
|
|
|
|
100
|
2 |
|
return $response->getMessageId(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Pop the next job off of the queue. |
105
|
|
|
* |
106
|
|
|
* @param string $queue |
107
|
|
|
* |
108
|
|
|
* @return \Illuminate\Queue\Jobs\Job|null |
109
|
|
|
*/ |
110
|
3 |
|
public function pop($queue = null) |
111
|
|
|
{ |
112
|
3 |
|
$queue = $this->getDefaultIfNull($queue); |
113
|
|
|
|
114
|
|
|
try { |
115
|
3 |
|
$response = $this->mns->setQueue($queue)->receiveMessage(); |
116
|
3 |
|
} catch (MessageNotExistException $e) { |
|
|
|
|
117
|
1 |
|
$response = null; |
118
|
|
|
} |
119
|
|
|
|
120
|
3 |
|
if ($response) { |
121
|
2 |
|
if ($this->jobCreator) { |
122
|
1 |
|
return call_user_func($this->jobCreator, $this->container, $queue, $response); |
123
|
|
|
} else { |
124
|
1 |
|
return new Jobs\MnsJob($this->container, $this->mns, $queue, $response); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
return; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* 获取默认队列名(如果当前队列名为 null)。 |
133
|
|
|
* |
134
|
|
|
* @param string|null $wanted |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
4 |
|
public function getDefaultIfNull($wanted) |
139
|
|
|
{ |
140
|
4 |
|
return $wanted ? $wanted : $this->default; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* 设置使用特定的回调函数处理 job。 |
145
|
|
|
* |
146
|
|
|
* @param callable $callback |
147
|
|
|
* |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
1 |
|
public function createJobsUsing(callable $callback) |
151
|
|
|
{ |
152
|
1 |
|
$this->jobCreator = $callback; |
153
|
|
|
|
154
|
1 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.