public function receive($message) { $data = null; try { $data = MessageFactory::fromJson($message); } catch (RuntimeException $e) { throw new RuntimeException(sprintf('Trying to sumbit a non-job message, ' . 'got error %s with message %s', $e->getMessage(), $message), $e->getCode(), $e); } if (!$data instanceof JobInterface) { throw new RuntimeException(sprintf('Trying to sumbit a non-job message : %s', $message)); } if (!$this->client->isConnected()) { throw new RuntimeException(sprintf('STOMP server is not connected')); } try { $data->isOk(true); } catch (RuntimeException $e) { throw new RuntimeException(sprintf('Submit an invalid message job : %s', $e->getMessage())); } switch (true) { case $data instanceof ImageJob: $routingKey = RabbitMQConf::ROUTINGKEY_IMAGE_PROCESSING; break; case $data instanceof VideoJob: $routingKey = RabbitMQConf::ROUTINGKEY_VIDEO_PROCESSING; break; default: $routingKey = RabbitMQConf::ROUTINGKEY_ERROR; break; } $this->client->send(sprintf('/exchange/%s/%s', RabbitMQConf::EXCHANGE_DISPATCHER, $routingKey), $data->toJson()); }
/** @test */ public function itMustBeSerializable() { $ack = new JobNotAcknowledgement(); $date = new \DateTime('+5days'); $ack->setCreatedOn($date); $ack->setReason('Man machine'); $data = json_decode($ack->toJson(), true); $this->assertEquals($date->format(DATE_ATOM), $data['createdOn']); $this->assertEquals('Man machine', $data['reason']); $unserialized = MessageFactory::fromJson($ack->toJson()); $this->assertEquals($ack->getCreatedOn()->format(DATE_ATOM), $unserialized->getCreatedOn()->format(DATE_ATOM)); }
/** * {@inheritdoc} */ public function register(GloubsterServerInterface $server) { $server['dispatcher']->on('stomp-connected', function (GloubsterServerInterface $server, Client $stomp) { $stomp->subscribe(sprintf('/exchange/%s', RabbitMQConf::EXCHANGE_MONITOR), function (Frame $frame) use($server) { try { $data = MessageFactory::fromJson($frame->body); $server['websocket-application']->onPresence($data); } catch (RuntimeException $e) { $server['monolog']->addError(sprintf('Receiving wrong monitor message : %s', $frame->body)); } }); }); }
public function handleLog(PredisClient $redis, Logger $logger, Frame $frame, AckResolver $resolver) { $logger->addInfo(sprintf('Processing job %s', $frame->getHeader('delivery_tag'))); $error = false; try { $job = Factory::fromJson($frame->body); if (!$job instanceof JobInterface) { $error = true; } } catch (RuntimeException $e) { $error = true; } if (!$error) { $promise = $this->saveJob($redis, $job); } else { $promise = $this->saveGarbage($redis, $frame->body); } $promise->then(function () use($resolver) { $resolver->ack(); }); return $promise; }
/** * @dataProvider getData * @covers Gloubster\Message\Factory::fromJson */ public function testFromJson($expected, $json) { $this->assertEquals($expected, Factory::fromJson($json)); }
/** @test */ public function requestsShouldTriggersGloubsterCallbacksWithAGoodJob() { $reactor = $this->getReactSocketServerMock(); $server = new HttpServer($reactor); $handler = $this->getMessageHandlerMock(); $handler->expects($this->once())->method('receive')->with($this->equalTo('GOOD MESSAGE'))->will($this->returnValue(true)); $httpListener = new HTTPListener($server, $reactor, $this->getLogger()); $httpListener->attach($handler); $httpListener->listen(); $request = new HttpRequest('GET', '/'); $response = $this->getReactHttpResponseMock(); $phpunit = $this; $catch = false; $response->expects($this->once())->method('write')->will($this->returnCallback(function ($json) use($phpunit, &$catch) { $catch = true; $ack = MessageFactory::fromJson($json); $phpunit->assertInstanceOf('Gloubster\\Message\\Acknowledgement\\JobAcknowledgement', $ack); })); $server->emit('request', array($request, $response)); $request->emit('data', array('GOOD ME')); $request->emit('data', array('SSAGE')); $request->emit('end', array()); }
public function assertWrongLocalLogJob(AMQPMessage $message) { $job = MessageFactory::fromJson($message->body); $this->assertEquals(array('format' => 'jpg'), $job->getParameters()); $this->assertTrue($job->isOnError()); }
public function assertGoodLogWrongJob(AMQPMessage $message) { $job = MessageFactory::fromJson($message->body); $this->assertInstanceOf('Gloubster\\Message\\Job\\JobInterface', $job); $this->assertGreaterThan(0, $job->getBeginning()); $this->assertGreaterThan(0, $job->getEnd()); $this->assertGreaterThan($job->getBeginning(), $job->getEnd()); $this->assertTrue($job->isOnError()); $this->assertInstanceOf('Gloubster\\Delivery\\DeliveryInterface', $job->getDelivery()); $this->assertEquals($this->getId(), $job->getWorkerId()); }
public final function process(AMQPMessage $message) { $this->logger->addInfo(sprintf('Processing job %s', $message->delivery_info['delivery_tag'])); $error = false; try { $job = MessageFactory::fromJson($message->body); if (!$job instanceof JobInterface) { $error = true; } } catch (RuntimeException $e) { $error = true; } if ($error) { $this->logger->addCritical(sprintf('Received a wrong job message : %s', $message->body)); $this->channel->basic_publish(new AMQPMessage($message->body), Configuration::EXCHANGE_DISPATCHER, Configuration::ROUTINGKEY_ERROR); $this->channel->basic_ack($message->delivery_info['delivery_tag']); throw new RuntimeException('Wrong job message'); } $error = null; try { $this->jobCounter->add(); $job->setWorkerId($this->id); assert($job->isOk(true)); $start = microtime(true); $this->logger->addInfo(sprintf('Computing job %s ...', $message->delivery_info['delivery_tag'])); $data = $this->compute($job); $this->logger->addInfo('Job computed.'); $job->setProcessDuration(microtime(true) - $start); $job->setResult($this->getTechnicalInformations($data)); $start = microtime(true); $this->logger->addInfo(sprintf('Delivering job %s ...', $message->delivery_info['delivery_tag'])); $this->deliver($job, $data); $this->logger->addInfo('Job delivered.'); $job->setDeliveryDuration(microtime(true) - $start); $this->jobCounter->addSuccess(); } catch (\Exception $e) { $error = $e; $job->setError(true); $job->setErrorMessage($e->getMessage()); } if ($job->requireReceipt()) { $this->sendReceipt($job); } $job->setEnd(microtime(true)); $this->log($job); $this->logger->addInfo(sprintf('Acknowledging %s', $message->delivery_info['delivery_tag'])); $this->channel->basic_ack($message->delivery_info['delivery_tag']); if ($error) { throw $error; } }