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(""); }
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; }
protected function setUp() { $this->client = APIClient::factory(array('host' => 'localhost')); $this->object = new Guarantee($this->client); }