I have PHP application that consumes Kafka messages. The problem is how to know that there are new messages in Kafka? First solution is to create consumer in PHP then run it in a loop to check new messages. Something like this
<?php
namespace MyAppBundle\Command;
use MyAppBundle\EventSourcing\EventSerializer\JSONEventSerializer;
use MyAppBundle\Service\EventProjectorService;
use MyAppBundle\Service\KafkaService;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\RuntimeException;
class EventCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('events:fetch');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var KafkaService $kafkaService */
$kafkaService = $this->getContainer()->get('store_locator.kafka_service');
/** @var EventProjectorService $eventProjector */
$eventProjector = $this->getContainer()->get('store_locator.event_projector');
while(1){
$messages = $kafkaService->fetchEvents();
foreach ($messages as $message) {
$eventProjector->aggregate($message);
}
}
$output->writeln("Finish");
}
}
But I don't like it... Is there any other way?
If no better way, how to keep it running? For instance when something fail.