public function testPurgeQueue() { $queue = $this->createQueue(); $exchange = new Exchange(); $exchange->name = self::EXCHANGE_TEST_NAME; $exchange->vhost = '/'; $this->object->addExchange($exchange); $binding = new Binding(); $binding->vhost = '/'; $binding->source = self::EXCHANGE_TEST_NAME; $binding->destination = self::QUEUE_TEST_NAME; $binding->routing_key = self::QUEUE_TEST_NAME; $this->object->addBinding($binding); $message = json_encode(array('properties' => array(), 'routing_key' => self::QUEUE_TEST_NAME, 'payload' => 'body', 'payload_encoding' => 'string')); $n = 12; while ($n > 0) { $this->client->post('/api/exchanges/' . urlencode('/') . '/' . self::EXCHANGE_TEST_NAME . '/publish', array('content-type' => 'application/json'), $message)->send(); $n--; } usleep(2000000); $this->object->refreshQueue($queue); $this->assertEquals(12, $queue->messages_ready); $this->object->purgeQueue('/', self::QUEUE_TEST_NAME); usleep(4000000); $this->object->refreshQueue($queue); $this->assertEquals(0, $queue->messages_ready); }
/** * @covers RabbitMQ\Management\Guarantee::ensureBinding */ public function testEnsureBindingAlreadyBound() { $exchange = new Exchange(); $exchange->type = 'fanout'; $exchange->vhost = '/'; $exchange->name = 'test.exchange'; $queue = new Queue(); $queue->name = 'test.queue'; $queue->vhost = '/'; $this->client->addExchange($exchange); $this->client->addQueue($queue); $binding = new Binding(); $binding->vhost = '/'; $binding->source = 'test.exchange'; $binding->destination = 'test.queue'; $binding->routing_key = 'special.routing.key'; $this->client->addBinding($binding); $this->object->ensureBinding($binding); $found = false; foreach ($this->client->listBindingsByExchangeAndQueue('/', 'test.exchange', 'test.queue') as $binding) { if ('special.routing.key' === $binding->routing_key) { $found = true; break; } } $this->client->deleteExchange($exchange->vhost, $exchange->name); $this->client->deleteQueue($queue->vhost, $queue->name); if (!$found) { $this->fail('Unable to find back the binding'); } }
public function getAllFailedJobs() { $returnArray = array(); $client = APIClient::factory(array('host' => env('RABBITMQ_QUEUE_QUERY_HOST'))); $queues = $client->listQueues(); foreach ($queues as $k => $v) { if (preg_match('/_failed_/', $v->name)) { $url = 'http://' . env('RABBITMQ_QUEUE_QUERY_HOST') . ':' . env('RABBITMQ_QUEUE_QUERY_HOST_PORT') . '/api/queues/%2f/' . $v->name . '/get'; $client = new Client(); $body = ['count' => $v->messages, 'requeue' => true, 'encoding' => 'auto']; $request = $client->post($url, ['content-type' => 'application/json', 'auth' => ['guest', 'guest'], 'body' => \GuzzleHttp\json_encode($body)]); $messagesArray = \GuzzleHttp\json_decode($request->getBody()->getContents()); for ($i = 0; $i < sizeof($messagesArray); $i++) { $a = new \stdClass(); $a->id = $messagesArray[$i]->message_count; $a->connection = env('QUEUE_DRIVER'); $a->queue = $v->name; $a->payload = $messagesArray[$i]->payload; //$a->failed_at = date('Y-m-d H:i:s'); $returnArray[] = $a; } } } return $returnArray; }
public function doExecute(InputInterface $input, OutputInterface $output) { $this->guaranteeManager = new Guarantee(APIClient::factory(array_merge($this->conf['server'], $this->conf['server']['server-management']))); $output->getFormatter()->setStyle('error', new OutputFormatterStyle('red')); $output->getFormatter()->setStyle('alert', new OutputFormatterStyle('yellow')); $output->getFormatter()->setStyle('ok', new OutputFormatterStyle('green')); $output->getFormatter()->setStyle('title', new OutputFormatterStyle('white', 'cyan', array('bold', 'blink'))); $dry = $input->getOption('dry-run'); $errors = 0; $changesRequired = false; $pendingAction = '...'; if ($dry) { $pendingAction = ''; $output->writeln(""); $output->writeln("<alert>Running command as dry-run</alert>"); } $queues = array(RabbitMQConf::QUEUE_ERRORS, RabbitMQConf::QUEUE_IMAGE_PROCESSING, RabbitMQConf::QUEUE_LOGS, RabbitMQConf::QUEUE_VIDEO_PROCESSING); $output->writeln(""); $output->writeln("<title> Queues configuration </title>"); foreach ($queues as $name) { try { $queue = new RabbitMQQueue(); $queue->vhost = $this->conf['server']['vhost']; $queue->name = $name; $queue->durable = true; $queue->auto_delete = false; $status = $this->guaranteeManager->probeQueue($queue); $expectedResult = ''; switch ($status) { case Guarantee::PROBE_ABSENT: $output->write(" [ ] "); $output->write(sprintf(" %s %s ", $name, $pendingAction)); $expectedResult = 'Added'; $changesRequired = true; break; case Guarantee::PROBE_MISCONFIGURED: $output->write(" [<alert>-</alert>] "); $output->write(sprintf(" %s %s ", $name, $pendingAction)); $expectedResult = 'Fixed'; $changesRequired = true; break; case Guarantee::PROBE_OK: $output->write(" [<ok>X</ok>] "); $output->write(sprintf(" %s ", $name)); break; } if (!$dry) { try { $this->guaranteeManager->ensureQueue($queue); $output->write(sprintf("<ok>%s</ok>", $expectedResult)); } catch (RabbitMQManagementException $e) { $errors++; $output->write(sprintf("<error>failed</error> : %s", $e->getMessage())); } } } catch (RabbitMQManagementException $e) { } $output->writeln(""); } $output->writeln(""); $output->writeln("<title> Exchanges configuration </title>"); $exchanges = array(RabbitMQConf::EXCHANGE_DISPATCHER => 'direct', RabbitMQConf::EXCHANGE_MONITOR => 'fanout'); foreach ($exchanges as $name => $type) { try { $exchange = new RabbitMQExchange(); $exchange->vhost = $this->conf['server']['vhost']; $exchange->name = $name; $exchange->durable = true; $exchange->type = $type; $status = $this->guaranteeManager->probeExchange($exchange); $expectedResult = ''; switch ($status) { case Guarantee::PROBE_ABSENT: $output->write(" [ ] "); $output->write(sprintf(" %s %s ", $name, $pendingAction)); $expectedResult = 'Added'; $changesRequired = true; break; case Guarantee::PROBE_MISCONFIGURED: $output->write(" [<alert>-</alert>] "); $output->write(sprintf(" %s %s ", $name, $pendingAction)); $expectedResult = 'Fixed'; $changesRequired = true; break; case Guarantee::PROBE_OK: $output->write(" [<ok>X</ok>] "); $output->write(sprintf(" %s ", $name)); break; } if (!$dry) { try { $this->guaranteeManager->ensureExchange($exchange); $output->write(sprintf("<ok>%s</ok>", $expectedResult)); } catch (RabbitMQManagementException $e) { $errors++; $output->write(sprintf("<error>failed</error> : %s", $e->getMessage())); } } } catch (RabbitMQManagementException $e) { } $output->writeln(""); } $output->writeln(""); $output->writeln("<title> Routing configuration </title>"); $routing_keys = array(RabbitMQConf::ROUTINGKEY_ERROR => array(RabbitMQConf::EXCHANGE_DISPATCHER, RabbitMQConf::QUEUE_ERRORS), RabbitMQConf::ROUTINGKEY_IMAGE_PROCESSING => array(RabbitMQConf::EXCHANGE_DISPATCHER, RabbitMQConf::QUEUE_IMAGE_PROCESSING), RabbitMQConf::ROUTINGKEY_LOG => array(RabbitMQConf::EXCHANGE_DISPATCHER, RabbitMQConf::QUEUE_LOGS), RabbitMQConf::ROUTINGKEY_VIDEO_PROCESSING => array(RabbitMQConf::EXCHANGE_DISPATCHER, RabbitMQConf::QUEUE_VIDEO_PROCESSING)); foreach ($routing_keys as $routing => $queueData) { try { list($exchangeName, $queueName) = $queueData; $binding = new RabbitMQBinding(); $binding->routing_key = $routing; $binding->vhost = $this->conf['server']['vhost']; $binding->source = $exchangeName; $binding->destination = $queueName; $binding->routing_key = $binding->routing_key; $status = $this->guaranteeManager->probeBinding($binding); $expectedResult = ''; switch ($status) { case Guarantee::PROBE_ABSENT: $output->write(" [ ] "); $output->write(sprintf(" routing %s on %s => %s %s ", $this->setToLength($routing, 22), $exchangeName, $queueName, $pendingAction)); $expectedResult = 'Added'; $changesRequired = true; break; case Guarantee::PROBE_MISCONFIGURED: $output->write(" [<alert>-</alert>] "); $output->write(sprintf(" routing %s on %s => %s %s ", $this->setToLength($routing, 22), $exchangeName, $queueName, $pendingAction)); $expectedResult = 'Fixed'; $changesRequired = true; break; case Guarantee::PROBE_OK: $output->write(" [<ok>X</ok>] "); $output->write(sprintf(" routing %s on %s => %s ", $this->setToLength($routing, 22), $exchangeName, $queueName)); break; } if (!$dry) { try { $this->guaranteeManager->ensureBinding($binding); $output->write(sprintf("<ok>%s</ok>", $expectedResult)); } catch (RabbitMQManagementException $e) { $errors++; $output->write(sprintf("<error>failed</error> : %s", $e->getMessage())); } } } catch (RabbitMQManagementException $e) { } $output->writeln(""); } $output->writeln(""); if ($errors) { $output->writeln(sprintf("<error>%d error(s) occured</error>", $errors)); } else { if ($dry && $changesRequired) { $output->writeln("<alert>It was just a dry-run, remove the option to apply changes</alert>"); } else { $output->writeln("<ok>Everything seems OK !</ok>"); } } $output->writeln(""); }