|
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\Jobs; |
|
16
|
|
|
|
|
17
|
|
|
use Illuminate\Queue\Jobs\Job; |
|
|
|
|
|
|
18
|
|
|
use Illuminate\Container\Container; |
|
|
|
|
|
|
19
|
|
|
use AliyunMNS\Exception\MnsException; |
|
20
|
|
|
use Milkmeowo\LaravelMns\Adaptors\MnsAdapter; |
|
21
|
|
|
use AliyunMNS\Responses\ReceiveMessageResponse; |
|
22
|
|
|
use Illuminate\Contracts\Queue\Job as JobContract; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
class MnsJob extends Job implements JobContract |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* 任务 |
|
28
|
|
|
* |
|
29
|
|
|
* @var \AliyunMNS\Responses\ReceiveMessageResponse |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $job; |
|
32
|
|
|
/** |
|
33
|
|
|
* Mns 适配器. |
|
34
|
|
|
* |
|
35
|
|
|
* @var \Milkmeowo\LaravelMns\Adaptors\MnsAdapter |
|
36
|
|
|
*/ |
|
37
|
|
|
private $mns; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Job 构造. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Illuminate\Container\Container $container Laravel容器 |
|
43
|
|
|
* @param \Milkmeowo\LaravelMns\Adaptors\MnsAdapter $mns Mns 适配器 |
|
44
|
|
|
* @param string $queue 队列 |
|
45
|
|
|
* @param \AliyunMNS\Responses\ReceiveMessageResponse $job 任务 |
|
46
|
|
|
*/ |
|
47
|
7 |
|
public function __construct(Container $container, MnsAdapter $mns, $queue, ReceiveMessageResponse $job) |
|
48
|
|
|
{ |
|
49
|
7 |
|
$this->container = $container; |
|
|
|
|
|
|
50
|
7 |
|
$this->mns = $mns; |
|
51
|
7 |
|
$this->queue = $queue; |
|
|
|
|
|
|
52
|
7 |
|
$this->job = $job; |
|
53
|
7 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* 获取 Job 的 RawBody. |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function getRawBody() |
|
61
|
|
|
{ |
|
62
|
1 |
|
return $this->job->getMessageBody(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* 从队列中删除. |
|
67
|
|
|
*/ |
|
68
|
2 |
|
public function delete() |
|
69
|
|
|
{ |
|
70
|
|
|
try { |
|
71
|
2 |
|
$receiptHandle = $this->job->getReceiptHandle(); |
|
72
|
2 |
|
$this->mns->useQueue($this->queue)->deleteMessage($receiptHandle); |
|
73
|
|
|
// 删除成功 |
|
74
|
1 |
|
$this->deleted = true; |
|
|
|
|
|
|
75
|
1 |
|
} catch (MnsException $exception) { |
|
76
|
|
|
// 删除失败 |
|
77
|
1 |
|
$this->deleted = false; |
|
78
|
|
|
} |
|
79
|
2 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* 释放 Job,重新回到队列. |
|
83
|
|
|
* |
|
84
|
|
|
* @param int $delay 延迟时间 |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function release($delay = 0) |
|
87
|
|
|
{ |
|
88
|
|
|
// 默认情况下 Laravel 将以 delay 0 来更改可见性,其预期的是使用队列服务默认的 |
|
89
|
|
|
// 下次可消费时间,但 Aliyun MNS PHP SDK 的接口要求这个值必须大于 0, |
|
90
|
|
|
// 指从现在起,多久后消息变为可消费。 |
|
91
|
1 |
|
if ($delay == 0) { |
|
92
|
1 |
|
$delay = $this->fromNowToNextVisibleTime($this->job->getNextVisibleTime()); |
|
93
|
|
|
} |
|
94
|
1 |
|
parent::release($delay); |
|
95
|
1 |
|
$this->mns->useQueue($this->queue)->changeMessageVisibility($this->job->getReceiptHandle(), $delay); |
|
96
|
1 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* 从现在起到消息变为可消费的秒数。 |
|
100
|
|
|
* |
|
101
|
|
|
* @param int $nextVisibleTime 下次可消费时的毫秒时间戳。 |
|
102
|
|
|
* |
|
103
|
|
|
* @return int |
|
104
|
|
|
*/ |
|
105
|
1 |
|
private function fromNowToNextVisibleTime($nextVisibleTime) |
|
106
|
|
|
{ |
|
107
|
1 |
|
$nowInMilliSeconds = 1000 * microtime(true); |
|
108
|
1 |
|
$fromNowToNextVisibleTime = $nextVisibleTime - $nowInMilliSeconds; |
|
109
|
1 |
|
$fromNowToNextVisibleTime = (int) ($fromNowToNextVisibleTime / 1000); |
|
110
|
|
|
|
|
111
|
1 |
|
return $fromNowToNextVisibleTime > 0 ? $fromNowToNextVisibleTime : 1; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Job 尝试次数. |
|
116
|
|
|
* |
|
117
|
|
|
* @return int |
|
118
|
|
|
*/ |
|
119
|
1 |
|
public function attempts() |
|
120
|
|
|
{ |
|
121
|
1 |
|
return (int) $this->job->getDequeueCount(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* 获取 Job Id. |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function getJobId() |
|
130
|
|
|
{ |
|
131
|
1 |
|
return $this->job->getMessageId(); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths