-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathJob.php
126 lines (104 loc) · 2.96 KB
/
Job.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace Enqueue\LaravelQueue;
use Illuminate\Container\Container;
use Illuminate\Contracts\Queue\Job as JobContract;
use Illuminate\Queue\Jobs\Job as BaseJob;
use Interop\Queue\Consumer;
use Interop\Queue\Context;
use Interop\Queue\Exception\DeliveryDelayNotSupportedException;
use Interop\Queue\Message;
use ReflectionClass;
class Job extends BaseJob implements JobContract
{
/**
* @var Context
*/
private $context;
/**
* @var Consumer
*/
private $consumer;
/**
* @var Message
*/
private $message;
public function __construct(Container $container, Context $context, Consumer $consumer, Message $message, $connectionName)
{
$this->container = $container;
$this->context = $context;
$this->consumer = $consumer;
$this->message = $message;
$this->connectionName = $connectionName;
}
public function getJobId()
{
return $this->message->getMessageId();
}
/**
* {@inheritdoc}
*/
public function delete()
{
parent::delete();
$this->consumer->acknowledge($this->message);
}
/**
* {@inheritdoc}
*/
public function fire()
{
$handlerClass = config('queue.connections.' . $this->getConnectionName() . '.handler');
$timeout = config('queue.connections.' . $this->getConnectionName() . '.timeout');
if (! empty($handlerClass)) {
return (new $handlerClass($this->consumer->receive($timeout)))->handle();
} else {
return parent::fire();
}
}
/**
* In case the payload is not a serialised PHP message, provide a default fall back
*
* @return array
* @throws \ReflectionException
*/
public function payload()
{
if (empty(parent::payload())) {
$handlerClass = config('queue.connections.' . $this->getConnectionName() . '.handler');
if (! empty($handlerClass)) {
return [
'job' => $handlerClass
];
}
}
return parent::payload();
}
/**
* {@inheritdoc}
*/
public function release($delay = 0)
{
parent::release($delay);
$requeueMessage = clone $this->message;
$requeueMessage->setProperty('x-attempts', $this->attempts() + 1);
$producer = $this->context->createProducer();
try {
$producer->setDeliveryDelay($this->secondsUntil($delay) * 1000);
} catch (DeliveryDelayNotSupportedException $e) {
}
$this->consumer->acknowledge($this->message);
$producer->send($this->consumer->getQueue(), $requeueMessage);
}
public function getQueue()
{
return $this->consumer->getQueue()->getQueueName();
}
public function attempts()
{
return $this->message->getProperty('x-attempts', 1);
}
public function getRawBody()
{
return $this->message->getBody();
}
}