/** * @param callable $errorCallback * @return bool * @throws RuntimeException */ public function process(Closure $errorCallback = null) { if (!$errorCallback) { $errorCallback = function () { }; } try { $message = $this->queue->dequeue(); } catch (RuntimeException $e) { $errorCallback($this, $e); return false; } if (!$message) { $this->eventDispatcher->dispatch(Events::QUEUE_EMPTY); return false; } $this->eventDispatcher->dispatch(Events::MESSAGE_RECEIVE, new MessageEvent($message)); $result = $this->handleMessage($message); if (!$result) { $this->eventDispatcher->dispatch(Events::MESSAGE_NOT_HANDLABLE, new MessageEvent($message)); throw new RuntimeException(sprintf("No handler will handle a message of topic '%s'", $message->getTopic())); } if ($result->isSuccess()) { $this->queue->ack($message); } return true; }
public function setUp() { if (!getenv("IRONIO_TOKEN") || !getenv("IRONIO_PROJECT")) { return $this->markTestSkipped('IronMQ not configured'); } $this->queue = new Queue(new IronMQAdapter(getenv("IRONIO_TOKEN"), getenv("IRONIO_PROJECT"), 'filelib_tests')); $this->queue->purge(); }
/** * @dataProvider provideMessages * @test */ public function messagesGoThroughThePipeUnchanged($topic, $data) { $this->assertFalse($this->queue->dequeue()); $message = $this->queue->enqueue($topic, $data); $dequeued = $this->queue->dequeue(); $this->assertInstanceOf('Pekkis\\Queue\\Message', $dequeued); $this->assertEquals($message->getTopic(), $dequeued->getTopic()); $this->assertEquals($message->getData(), $dequeued->getData()); $this->queue->ack($dequeued); $this->assertFalse($this->queue->dequeue()); }
/** * @test * @group lussi */ public function enqueueThrowsIfAdapterThrows() { $adapter = $this->prophesize(Adapter::class); $adapter->enqueue(Argument::type('string'), Argument::type('string'))->shouldBeCalled()->willThrow(new \RuntimeException('bibbidi bobbidi buu')); $queue = new Queue($adapter->reveal()); $this->setExpectedException(RuntimeException::class); $queue->enqueue('imaiseppa', 'mehewae'); }
<?php namespace Pekkis\Queue\Example; use Pekkis\Queue\Adapter\IronMQAdapter; use Pekkis\Queue\Message; use Pekkis\Queue\Queue; require_once is_file(__DIR__ . '/bootstrap.php') ? __DIR__ . '/bootstrap.php' : __DIR__ . '/bootstrap.dist.php'; // Create a new IronMQ backed queue $queue = new Queue(new IronMQAdapter(IRONMQ_TOKEN, IRONMQ_PROJECT_ID, 'pekkis-queue-example')); // Queues can be emptied. $queue->purge(); // A message consists of a topic and data. A message instance with an UUID you can use is returned. $message = $queue->enqueue('pekkis.queue.example', array('some' => 'random data')); // Dequeue and process a single message $received = $queue->dequeue(); $data = $received->getData(); var_dump($data); // Acknowledge the message (you're done with it) $queue->ack($received);
{ return $message->getType() == 'reservation.create'; } /** * @param Message $message * @return Result */ public function handle(Message $message, QueueInterface $queue) { /** @var ReservationRequest $reservation */ $reservation = $message->getData(); if (rand(1, 100) >= 75) { // If a result is not successful the message will stay on the queue. $result = new Result(false, 'Oh dear, the reservation could not be created. It will be retried... soon!'); } else { $msg = sprintf("Reservation created from %s to %s", $reservation->getFrom()->format('Y-m-d H:i:d'), $reservation->getTo()->format('Y-m-d H:i:d')); // If a result is successful, the message is acked (acknowledged to be processed, removed from queue) $result = new Result(true, $msg); } return $result; } } // Creates an IronMQ backed queue $innerQueue = new Queue(new IronMQAdapter(IRONMQ_TOKEN, IRONMQ_PROJECT_ID, 'pekkis-queue-example')); // Adds our own data serializer for reservation requests $innerQueue->addDataSerializer(new ReservationRequestDataSerializer()); // Wrap the queue with Symfony events $queue = new EventDispatchingQueue($innerQueue, new EventDispatcher()); // Create a console output and attach a queue subscriber to get queue event messages to console output. $output = new ConsoleOutput(); $queue->addSubscriber(new ConsoleOutputSubscriber($output));