1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\RabbitMQ; |
4
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\Model\Job; |
6
|
|
|
use Dtc\QueueBundle\Model\JobManagerInterface; |
7
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
8
|
|
|
use PhpAmqpLib\Connection\AbstractConnection; |
9
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
10
|
|
|
|
11
|
|
|
class JobManager implements JobManagerInterface |
12
|
|
|
{ |
13
|
|
|
/** @var AMQPChannel */ |
14
|
|
|
protected $channel; |
15
|
|
|
|
16
|
|
|
/** @var AbstractConnection */ |
17
|
|
|
protected $connection; |
18
|
|
|
protected $queueArgs; |
19
|
|
|
protected $exchangeArgs; |
20
|
|
|
|
21
|
|
|
protected $channelSetup = false; |
22
|
|
|
|
23
|
|
|
public function setExchangeArgs($exchange, $type, $passive, $durable, $autoDelete) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$this->exchangeArgs = func_get_args(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function setQueueArgs($queue, $passive, $durable, $exclusive, $autoDelete) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$this->queueArgs = func_get_args(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setAMQPConnection(AbstractConnection $connection) |
34
|
|
|
{ |
35
|
|
|
$this->connection = $connection; |
36
|
|
|
$this->channel = $connection->channel(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function setupChannel() |
40
|
|
|
{ |
41
|
|
|
if (!$this->channelSetup) { |
42
|
|
|
call_user_func_array([$this->channel, 'exchange_declare'], $this->exchangeArgs); |
43
|
|
|
call_user_func_array([$this->channel, 'queue_declare'], $this->queueArgs); |
44
|
|
|
$this->channel->queue_bind($this->queueArgs[0], $this->exchangeArgs[0]); |
45
|
|
|
$this->channelSetup = true; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function save(\Dtc\QueueBundle\Model\Job $job) |
50
|
|
|
{ |
51
|
|
|
$this->setupChannel(); |
52
|
|
|
$msg = new AMQPMessage($job->toMessage()); |
53
|
|
|
$this->channel->basic_publish($msg, $this->exchangeArgs[0]); |
54
|
|
|
|
55
|
|
|
return $job; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getJob($workerName = null, $methodName = null, $prioritize = true) |
59
|
|
|
{ |
60
|
|
|
if ($methodName) { |
61
|
|
|
throw new \Exception('Unsupported'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->setupChannel(); |
65
|
|
|
$message = $this->channel->basic_get($this->queueArgs[0]); |
66
|
|
|
if ($message) { |
67
|
|
|
$job = new Job(); |
68
|
|
|
$job->fromMessage($message->body); |
69
|
|
|
$job->setId($message->delivery_info['delivery_tag']); |
70
|
|
|
|
71
|
|
|
return $job; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function deleteJob(\Dtc\QueueBundle\Model\Job $job) |
78
|
|
|
{ |
79
|
|
|
throw new \Exception('unsupported'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Save History get called upon completion of the job |
83
|
|
|
public function saveHistory(\Dtc\QueueBundle\Model\Job $job) |
84
|
|
|
{ |
85
|
|
|
$deliveryTag = $job->getId(); |
86
|
|
|
$this->channel->basic_ack($deliveryTag); |
87
|
|
|
|
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function __destruct() |
92
|
|
|
{ |
93
|
|
|
$this->channel->close(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function getJobCount($workerName = null, $methodName = null) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
if ($methodName) { |
99
|
|
|
throw new \Exception('Unsupported'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ($workerName) { |
103
|
|
|
throw new \Exception('Unsupported'); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function resetErroneousJobs($workerName = null, $methodName = null) |
108
|
|
|
{ |
109
|
|
|
throw new \Exception('Unsupported'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function pruneErroneousJobs($workerName = null, $methodName = null) |
113
|
|
|
{ |
114
|
|
|
throw new \Exception('Unsupported'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getStatus() |
118
|
|
|
{ |
119
|
|
|
throw new \Exception('Unsupported'); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.