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\Jobs; |
16
|
|
|
|
17
|
|
|
use AliyunMNS\Responses\ReceiveMessageResponse; |
18
|
|
|
use Illuminate\Container\Container; |
19
|
|
|
use Illuminate\Contracts\Queue\Job as JobContract; |
20
|
|
|
use Illuminate\Queue\Jobs\Job; |
21
|
|
|
use LaravelMns\MnsAdapter; |
22
|
|
|
|
23
|
|
|
class MnsJob extends Job implements JobContract |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The class name of the job. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $job; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The queue message data. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $data; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \LaravelMns\MnsAdapter |
41
|
|
|
*/ |
42
|
|
|
private $mns; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create a new job instance. |
46
|
|
|
* |
47
|
|
|
* @param \Illuminate\Container\Container $container |
48
|
|
|
* @param \LaravelMns\MnsAdapter $mns |
49
|
|
|
* @param string $queue |
50
|
|
|
* @param \AliyunMNS\Responses\ReceiveMessageResponse $job |
51
|
|
|
*/ |
52
|
6 |
|
public function __construct( |
53
|
|
|
Container $container, |
54
|
|
|
MnsAdapter $mns, |
55
|
|
|
$queue, |
56
|
|
|
ReceiveMessageResponse $job |
57
|
|
|
) { |
58
|
6 |
|
$this->container = $container; |
59
|
6 |
|
$this->mns = $mns; |
60
|
6 |
|
$this->queue = $queue; |
61
|
6 |
|
$this->job = $job; |
|
|
|
|
62
|
6 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Fire the job. |
66
|
|
|
*/ |
67
|
2 |
|
public function fire() |
68
|
|
|
{ |
69
|
2 |
|
$body = json_decode($this->getRawBody(), true); |
70
|
2 |
|
if (!is_array($body)) { |
71
|
1 |
|
throw new \InvalidArgumentException( |
72
|
|
|
"Seems it's not a Laravel enqueued job. \r\n |
73
|
1 |
|
[$body]" |
74
|
1 |
|
); |
75
|
|
|
} |
76
|
1 |
|
$this->resolveAndFire($body); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the raw body string for the job. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
2 |
|
public function getRawBody() |
85
|
|
|
{ |
86
|
2 |
|
return $this->job->getMessageBody(); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Delete the job from the queue. |
91
|
|
|
*/ |
92
|
1 |
|
public function delete() |
93
|
|
|
{ |
94
|
1 |
|
parent::delete(); |
95
|
1 |
|
$receiptHandle = $this->job->getReceiptHandle(); |
|
|
|
|
96
|
1 |
|
$this->mns->deleteMessage($receiptHandle); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Release the job back into the queue. |
101
|
|
|
* |
102
|
|
|
* @param int $delay |
103
|
|
|
*/ |
104
|
1 |
|
public function release($delay = 0) |
105
|
|
|
{ |
106
|
|
|
// 默认情况下 Laravel 将以 delay 0 来更改可见性,其预期的是使用队列服务默认的 |
107
|
|
|
// 下次可消费时间,但 Aliyun MNS PHP SDK 的接口要求这个值必须大于 0, |
108
|
|
|
// 指从现在起,多久后消息变为可消费。 |
109
|
|
|
$delay = 0 !== $delay |
110
|
1 |
|
? $delay |
111
|
1 |
|
: $this->fromNowToNextVisibleTime($this->job->getNextVisibleTime()); |
|
|
|
|
112
|
1 |
|
parent::release($delay); |
113
|
1 |
|
$this->mns->changeMessageVisibility( |
114
|
1 |
|
$this->job->getReceiptHandle(), |
|
|
|
|
115
|
|
|
$delay |
116
|
1 |
|
); |
117
|
1 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the number of times the job has been attempted. |
121
|
|
|
* |
122
|
|
|
* @return int |
123
|
|
|
*/ |
124
|
1 |
|
public function attempts() |
125
|
|
|
{ |
126
|
1 |
|
return (int) $this->job->getDequeueCount(); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* 从现在起到消息变为可消费的秒数。 |
131
|
|
|
* |
132
|
|
|
* @param int $nextVisibleTime 下次可消费时的微妙时间戳。 |
133
|
|
|
* |
134
|
|
|
* @return int |
135
|
|
|
*/ |
136
|
1 |
|
private function fromNowToNextVisibleTime($nextVisibleTime) |
137
|
|
|
{ |
138
|
1 |
|
$nowInMilliSeconds = 1000 * microtime(true); |
139
|
1 |
|
$fromNowToNextVisibleTime = $nextVisibleTime - $nowInMilliSeconds; |
140
|
|
|
|
141
|
1 |
|
return (int) ($fromNowToNextVisibleTime / 1000); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the IoC container instance. |
146
|
|
|
* |
147
|
|
|
* @return \Illuminate\Container\Container |
148
|
|
|
*/ |
149
|
1 |
|
public function getContainer() |
150
|
|
|
{ |
151
|
1 |
|
return $this->container; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
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..