public function getTaskResult(Task $task) { $result = null; try { $taskId = str_replace('-', '', $task->getId()); $connection = $this->getConnection(); $connection->connect(); if (!$connection->isConnected()) { throw new Exception(); } $channel = new AMQPChannel($connection); $queue = new AMQPQueue($channel); $queue->setName($taskId); $queue->setFlags(AMQP_DURABLE | AMQP_AUTODELETE); $queue->setArgument('x-expires', 86400000); $queue->declareQueue(); if ($message = $queue->get()) { $messageBody = json_decode($message->getBody()); if (json_last_error()) { throw new InvalidJsonException('Serialization Error, result is not valid JSON'); } $queue->ack($message->getDeliveryTag()); $result = $messageBody; $queue->delete(AMQP_IFUNUSED | AMQP_IFEMPTY | AMQP_NOWAIT); $connection->disconnect(); } } catch (\AMQPChannelException $e) { } return $result; }
/** * @param \Rhubarb\Task $task * @return bool|mixed|string */ public function getTaskResult(\Rhubarb\Task $task) { $pubsub = $this->getConnection()->pubSub(); $pubsub->subscribe('celery-task-meta-' . $task->getId()); foreach ($pubsub as $message) { if ($message->kind == 'message') { $message = json_decode($message->payload); $pubsub->unsubscribe('celery-task-meta-' . $task->getId()); return $message; } } }
/** * @param \Rhubarb\Task $task * @throws \Rhubarb\Exception\Exception */ public function publishTask(\Rhubarb\Task $task) { $connection = $this->getConnection(); $connection->connect(); $channel = new AMQPChannel($connection); if (!$channel->isConnected()) { throw new Exception('AMQP Failed to Connect'); } $queue = new AMQPQueue($channel); $queue->setName($task->message->getQueue()); $queue->setFlags(AMQP_DURABLE); if ($this->options['options']) { $queue->setArguments($this->options['options']); } $queue->declareQueue(); $exchange = new AMQPExchange($channel); $exchange->setFlags(AMQP_PASSIVE | AMQP_DURABLE); $exchange->setType(AMQP_EX_TYPE_DIRECT); $exchange->setName($task->message->getPropExchange()); $exchange->declareExchange(); $queue->bind($task->message->getPropExchange(), $task->getId()); $msgProperties = array('content_type' => $task->getMessage()->getContentType(), 'content_encoding' => $task->getMessage()->getContentEncoding(), 'encoding' => $task->getMessage()->getContentEncoding()); if ($task->getPriority()) { $msgProperties['priority'] = $task->getPriority(); } $exchange->publish((string) $task, $task->getId(), AMQP_NOPARAM, $msgProperties); $this->getConnection()->disconnect(); }
/** * @param Task $task * @return bool|mixed|null|string * @throws InvalidJsonException * @throws CeleryConfigurationException */ public function getTaskResult(Task $task) { $result = null; try { $taskId = str_replace('-', '', $task->getId()); $channel = $this->getConnection()->channel(); if ($message = $channel->basicGet(array('queue' => $taskId))) { $content_type = $message->get('content_type'); if ($content_type !== 'application/json') { throw new CeleryConfigurationException("Response's content-type is not application/json. Got: {$content_type}. Make sure, that CELERY_RESULT_SERIALIZER is set to \"json\""); } $messageBody = json_decode($message->body); if (json_last_error()) { throw new InvalidJsonException('Serialization Error, result is not valid JSON'); } $channel->basicAck($message->delivery_info['delivery_tag']); $channel->queueDelete(array('queue' => $taskId, 'if_unused' => true, 'if_empty' => true, 'no_wait' => true)); $channel->close(); $result = $messageBody; } } catch (ChannelException $e) { } return $result; }
/** * @param \Rhubarb\Task $task */ public function publishTask(\Rhubarb\Task $task) { if (!$task->getMessage()->getPropRoutingKey()) { $task->getMessage()->setPropRoutingKey($task->getId()); } $channel = $this->getConnection()->channel(); $channel->queueDeclare(array('queue' => $task->getMessage()->getQueue(), 'durable' => $task->getMessage()->getPropDurable(), 'auto_delete' => $task->getMessage()->getPropAutoDelete(), 'arguments' => $task->getMessage()->getPropQueueArgs())); $channel->exchangeDeclare($task->getMessage()->getPropExchange(), 'direct', array('passive' => true, 'durable' => true)); $channel->queueBind($task->getMessage()->getQueue(), $task->getMessage()->getPropExchange(), array('routing_key' => $task->getMessage()->getPropRoutingKey())); $msgProperties = array('content_type' => Rhubarb::RHUBARB_CONTENT_TYPE, 'content_encoding' => $task->getMessage()->getContentEncoding(), 'priority' => $task->getMessage()->getPropPriority()); $channel->basicPublish(new AmqpMessage((string) $task, $msgProperties), array('exchange' => $task->getMessage()->getPropExchange(), 'routing_key' => $task->getMessage()->getPropRoutingKey())); $channel->close(); $channel = null; }
/** * @param \Rhubarb\Task $task */ public function publishTask(\Rhubarb\Task $task) { $taskArray = $task->toArray(); $this->published = json_encode($taskArray); }
/** * @param \Rhubarb\Task $task */ public function publishTask(\Rhubarb\Task $task) { $task->getMessage()->setContentEncoding(Rhubarb::CONTENT_ENCODING_UTF8); if (!$task->getMessage()->getPropRoutingKey()) { $task->getMessage()->setPropRoutingKey('celery'); } if (!$task->getMessage()->getCorrelationId()) { $task->getMessage()->setCorrelationId($task->getId()); } if (!$task->getMessage()->getReplyTo()) { $task->getMessage()->setReplyTo($task->getId()); } $task->getMessage()->setPropDeliveryMode(2)->setPropDeliveryTag(2); $task->getMessage()->setBodyEncoding(Rhubarb::CONTENT_ENCODING_BASE64); $task->toArray(); $this->getConnection()->lpush($task->getMessage()->getPropExchange(), (string) $task->getMessage()); }