/** * {@inheritDoc} */ public function publish(Message $message, $key = null) { $properties = $message->getProperties(); if (isset($properties['application_headers'])) { if (!isset($properties['headers'])) { $properties['headers'] = array(); } foreach ($properties['application_headers'] as $header => $value) { if (!is_array($value) || 2 !== count($value)) { throw new \InvalidArgumentException('Unexpected value for application_headers "' . $header . '"'); } $properties['headers'][$header] = $value[1]; } unset($properties['application_headers']); } $this->exchange->publish($message->getBody(), $key, $this->flags, $properties); }
/** {@inheritDoc} */ public function process(Message $message, array $options) { $properties = $message->getProperties(); // check for invalid correlation_id properties (not set, or invalid) if (!isset($properties['correlation_id'])) { return; } if ($properties['correlation_id'] !== $options['rpc_client_correlation_id']) { return; } $result = null; $this->logger and $this->logger->info('Message received from the RPC Server ; terminating consumer', array('correlation_id' => $properties['correlation_id'])); $this->awoken = true; if (null !== $this->processor) { $this->logger and $this->logger->info('Sending message to sub-processor'); $result = $this->processor->process($message, $options); } return $result; }
/** * {@inheritdoc} */ public function publish(Message $message, $key = null) { $properties = $message->getProperties(); if (isset($properties['application_headers'])) { if (!isset($properties['headers'])) { $properties['headers'] = []; } foreach ($properties['application_headers'] as $header => $value) { if (!is_array($value) || 2 !== count($value)) { throw new \InvalidArgumentException('Unexpected value for application_headers "' . $header . '"'); } $properties['headers'][$header] = $value[1]; } unset($properties['application_headers']); } $body = $message->getBody(); if (empty($body)) { $this->logger->notice('Publishing empty message.', ['message' => $body, 'exchange' => $this->exchange->getName(), 'key' => $key]); } $this->exchange->publish($message->getBody(), $key, $this->flags, $this->sanitizeProperties($properties)); }
/** {@inheritdoc} */ public function publish(Message $message, $key = null) { $properties = $message->getProperties(); if (isset($properties['headers'])) { if (!isset($properties['application_headers'])) { $properties['application_headers'] = []; } foreach ($properties['headers'] as $header => $value) { if (is_array($value)) { $type = 'A'; } elseif (is_int($value)) { $type = 'I'; } else { $type = 'S'; } $properties['application_headers'][$header] = [$type, $value]; } } $amqpMessage = new AMQPMessage($message->getBody(), $properties); $this->channel->basic_publish($amqpMessage, $this->exchange, (string) $key); }
/** * @param \Exception|\Throwable $exception * @param Message $message * @param array $options */ private function handleException($exception, Message $message, array $options) { $properties = $message->getProperties(); $attempts = 0; if (isset($properties['headers']['swarrot_retry_attempts'])) { $attempts = $properties['headers']['swarrot_retry_attempts']; } ++$attempts; if ($attempts > $options['retry_attempts']) { $this->logger and $this->logger->warning(sprintf('[Retry] Stop attempting to process message after %d attempts', $attempts), ['swarrot_processor' => 'retry', 'exception' => $exception]); throw $exception; } if (!isset($properties['headers'])) { $properties['headers'] = array(); } $properties['headers']['swarrot_retry_attempts'] = $attempts; $message = new Message($message->getBody(), $properties); $key = str_replace('%attempt%', $attempts, $options['retry_key_pattern']); $this->logger and $this->logger->warning(sprintf('[Retry] An exception occurred. Republish message for the %d times (key: %s)', $attempts, $key), ['swarrot_processor' => 'retry', 'exception' => $exception]); $this->publisher->publish($message, $key); }
public function process(Message $message, array $options) { $properties = $message->getProperties(); $body = json_decode($message->getBody(), true); if (!isset($properties['wisembly_attempts'])) { $properties['wisembly_attempts'] = 0; } if (!isset($body['arguments'])) { $body['arguments'] = []; } // add environment $body['arguments'][] = '--env'; $body['arguments'][] = $this->environment; // add verbosity switch ($this->verbosity) { case OutputInterface::VERBOSITY_DEBUG: $body['arguments'][] = '-vvv'; break; case OutputInterface::VERBOSITY_VERY_VERBOSE: $body['arguments'][] = '-vv'; break; case OutputInterface::VERBOSITY_VERBOSE: $body['arguments'][] = '--verbose'; break; case OutputInterface::VERBOSITY_QUIET: $body['arguments'][] = '--quiet'; break; case OutputInterface::VERBOSITY_NORMAL: break; } ++$properties['wisembly_attempts']; $this->logger->info('Dispatching command', $body); // if no proper command given, log it and nack if (!isset($body['command'])) { $this->logger->critical('No proper command found in message', ['body' => $body]); $this->provider->nack($message, false); return; } $this->builder->setPrefix([PHP_BINARY, $this->commandPath, $body['command']]); $this->builder->setArguments($body['arguments']); $process = $this->builder->getProcess(); $process->run(function ($type, $data) { switch ($type) { case Process::OUT: $this->logger->info($data); break; case Process::ERR: $this->logger->error($data); break; } }); // reset the builder $this->builder->setArguments([]); $this->builder->setPrefix([]); if ($process->isSuccessful()) { $this->logger->info('The process was successful', $body); $this->provider->ack($message); return; } $code = $process->getExitCode(); $this->logger->error('The command failed ; aborting', ['body' => $body, 'code' => $code]); $this->provider->nack($message, false); // should we requeue it ? if (static::REQUEUE === $code && $properties['wisembly_attempts'] < static::MAX_ATTEMPTS) { $this->logger->notice('Retrying...', $body); $message = new Message($message->getBody(), $properties, $message->getId()); $this->publisher->publish($message); } }
/** * {@inheritDoc} */ public function publish(Message $message, $key = null) { $this->exchange->publish($message->getBody(), $key, $this->flags, $message->getProperties()); }
/** * {@inheritDoc} */ public function process(Message $message, array $options) { $properties = $message->getProperties(); $exchange = $this->exchange ?: $properties['exchange_name']; $routingKey = $this->routingKey ?: $properties['routing_key']; $this->getMessagePublisher($exchange)->publish(new Message($message->getBody()), $routingKey); }