|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ccovey\LaravelRabbitMQ; |
|
4
|
|
|
|
|
5
|
|
|
use Ccovey\RabbitMQ\Consumer\ConsumableParameters; |
|
6
|
|
|
use Ccovey\RabbitMQ\Consumer\Consumer; |
|
7
|
|
|
use Ccovey\RabbitMQ\Producer\Message; |
|
8
|
|
|
use Ccovey\RabbitMQ\Producer\Producer; |
|
9
|
|
|
use DateTime; |
|
10
|
|
|
use Illuminate\Queue\Queue; |
|
11
|
|
|
use Illuminate\Contracts\Queue\Queue as QueueContract; |
|
12
|
|
|
|
|
13
|
|
|
class RabbitMqQueue extends Queue implements QueueContract |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Consumer |
|
17
|
|
|
*/ |
|
18
|
|
|
private $consumer; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Producer |
|
22
|
|
|
*/ |
|
23
|
|
|
private $producer; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $config; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(Consumer $consumer, Producer $producer, array $config) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->consumer = $consumer; |
|
33
|
|
|
$this->producer = $producer; |
|
34
|
|
|
$this->config = $config; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Push a new job onto the queue. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $job |
|
41
|
|
|
* @param mixed $data |
|
42
|
|
|
* @param string $queue |
|
43
|
|
|
* |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
*/ |
|
46
|
|
|
public function push($job, $data = '', $queue = null) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->pushRaw($this->createPayload($job, $data), $queue); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Push a raw payload onto the queue. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $payload |
|
55
|
|
|
* @param string $queue |
|
56
|
|
|
* @param array $options |
|
57
|
|
|
* |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
|
|
public function pushRaw($payload, $queue = null, array $options = []) |
|
61
|
|
|
{ |
|
62
|
|
|
if (isset($options['delay'])) { |
|
63
|
|
|
$payload['scheduledAt'] = new DateTime(sprintf('+%s', $options['delay'])); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$message = new Message($payload, $queue); |
|
|
|
|
|
|
67
|
|
|
$this->producer->publish($message); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Push a new job onto the queue after a delay. |
|
72
|
|
|
* |
|
73
|
|
|
* @param \DateTime|int $delay |
|
74
|
|
|
* @param string $job |
|
75
|
|
|
* @param mixed $data |
|
76
|
|
|
* @param string $queue |
|
77
|
|
|
* |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
public function later($delay, $job, $data = '', $queue = null) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->pushRaw($this->createPayload($job, $data), $queue, ['delay' => $delay]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Pop the next job off of the queue. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $queue |
|
89
|
|
|
* |
|
90
|
|
|
* @return \Illuminate\Contracts\Queue\Job|null |
|
91
|
|
|
*/ |
|
92
|
|
|
public function pop($queue = null) |
|
93
|
|
|
{ |
|
94
|
|
|
$params = new ConsumableParameters($queue); |
|
95
|
|
|
$message = $this->consumer->getMessage($params); |
|
96
|
|
|
|
|
97
|
|
|
if (!$message) { |
|
98
|
|
|
return null; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return new RabbitMqJob($this->container, $this->producer, $message); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get the size of the queue. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $queue |
|
108
|
|
|
* |
|
109
|
|
|
* @return int |
|
110
|
|
|
*/ |
|
111
|
|
|
public function size($queue = null) |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->consumer->getSize($queue); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: