1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Starkerxp\RabbitmqBundle\Command; |
4
|
|
|
|
5
|
|
|
use PhpAmqpLib\Exception\AMQPTimeoutException; |
6
|
|
|
use Starkerxp\RabbitmqBundle\Command\Exception\NomChannelNonDefinitException; |
7
|
|
|
use Starkerxp\RabbitmqBundle\Command\Exception\NomServiceNonDefinitException; |
8
|
|
|
use Starkerxp\StructureBundle\Command\LockCommand; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
|
11
|
|
|
abstract class ReceiverLockCommand extends LockCommand |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
public static function getPushChannel() |
15
|
|
|
{ |
16
|
|
|
return (self::getDelaiTemporisation() > 0 ? 'tmp_exchange_' : '').self::getNomChannel(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
protected function getDelaiTemporisation() |
20
|
|
|
{ |
21
|
|
|
return 0; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
abstract public function getNomChannel(); |
28
|
|
|
|
29
|
|
|
public function lockerName() |
30
|
|
|
{ |
31
|
|
|
return parent::lockerName().'_'.$this->input->getOption('numeroScript'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function traitement() |
35
|
|
|
{ |
36
|
|
|
$nomService = $this->getNomService(); |
37
|
|
|
if (empty($nomService)) { |
38
|
|
|
throw new NomServiceNonDefinitException(); |
39
|
|
|
} |
40
|
|
|
$nomChannel = $this->getNomChannel(); |
41
|
|
|
if (empty($nomChannel)) { |
42
|
|
|
throw new NomChannelNonDefinitException(); |
43
|
|
|
} |
44
|
|
|
$connection = $this->getContainer()->get('starkerxp_rabbitmq.service.rabbitmq'); |
45
|
|
|
$channel = $connection->channel(); |
46
|
|
|
$channel->queue_declare($nomChannel, false, true, false, false); |
47
|
|
|
|
48
|
|
|
if ($this->getDelaiTemporisation() > 0) { |
49
|
|
|
$channel->exchange_declare('exchange_'.$nomChannel, 'direct'); |
50
|
|
|
$channel->queue_bind($nomChannel, 'exchange_'.$nomChannel); |
51
|
|
|
$channel->queue_declare( |
52
|
|
|
"tmp_".$nomChannel, |
53
|
|
|
false, |
54
|
|
|
false, |
55
|
|
|
false, |
56
|
|
|
true, |
57
|
|
|
true, |
58
|
|
|
[ |
59
|
|
|
'x-message-ttl' => ['I', $this->getDelaiTemporisation() * 1000], // delay in seconds to milliseconds |
60
|
|
|
"x-expires" => ["I", $this->getDelaiTemporisation() * 1000 + 1000], |
61
|
|
|
'x-dead-letter-exchange' => ['S', 'exchange_'.$nomChannel] // after message expiration in delay queue, move message to the right.now.queue |
62
|
|
|
] |
63
|
|
|
); |
64
|
|
|
$channel->exchange_declare('tmp_exchange_'.$nomChannel, 'direct'); |
65
|
|
|
$channel->queue_bind('tmp_'.$nomChannel, 'tmp_exchange_'.$nomChannel); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$callback = $this->getCallback(); |
69
|
|
|
$channel->basic_qos(null, 1, null); |
70
|
|
|
$channel->basic_consume($nomChannel, '', false, false, false, false, $callback); |
71
|
|
|
try { |
72
|
|
|
while (count($channel->callbacks)) { |
73
|
|
|
$channel->wait(null, false, $this->input->getOption('timeout')); |
74
|
|
|
} |
75
|
|
|
} catch (AMQPTimeoutException $e) { |
|
|
|
|
76
|
|
|
} |
77
|
|
|
$channel->close(); |
78
|
|
|
$connection->close(); |
79
|
|
|
$this->output->writeln('<info>Execution terminé.</info>'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
abstract public function getNomService(); |
86
|
|
|
|
87
|
|
|
abstract public function getCallback(); |
88
|
|
|
|
89
|
|
|
protected function configure() |
90
|
|
|
{ |
91
|
|
|
parent::configure(); |
92
|
|
|
$this->addOption('numeroScript', 'n', InputOption::VALUE_OPTIONAL, 'Numéro de script', 1); |
93
|
|
|
$this->addOption('timeout', 't', InputOption::VALUE_OPTIONAL, 'Timeout callback queue', 0); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
|
|
|
|
97
|
|
|
|