output->error('The image processing queue is not enabled'); } $rabbitmq = new RabbitMQService(); $this->output->writeln('Monitoring queue'); $rabbitmq->waitOnQueue([$this, 'processQueueItem']); } /** * Processes a single item from the queue. * * @param $msg * @return void */ public function processQueueItem($msg) { $queueItemID = intval($msg->body); $this->output->writeln(sprintf('Processing queue item %d', $queueItemID)); /** @var QueueItem $queueItem */ $queueItem = QueueItem::where('id', $queueItemID)->first(); if (is_null($queueItem)) { $this->output->writeln('Queue item does not exist; skipping'); $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); return; } try { switch (strtolower($queueItem->action_type)) { case 'photo.analyse': $this->processPhotoAnalyseMessage($queueItem); break; default: $this->output->writeln(sprintf('Action %s is not recognised, skipping', $queueItem->action_type)); break; } } finally { $queueItem->completed_at = new \DateTime(); $queueItem->save(); $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); } } private function processPhotoAnalyseMessage(QueueItem $queueItem) { $this->output->writeln(sprintf('Analysing photo ID %l (batch: %s)', $queueItem->photo_id, $queueItem->batch_reference)); $photo = Photo::where('id', $queueItem->photo_id)->first(); if (is_null($photo)) { $this->output->writeln('Photo does not exist; skipping'); } $photoService = new PhotoService($photo); $photoService->analyse($queueItem->batch_reference); } }