Baldinof / roadrunner-bundle

A RoadRunner worker integrated in your Symfony app
MIT License
270 stars 48 forks source link

Added WorkerRegistry and ability to add additional worker implementat… #98

Closed iborysenko closed 1 year ago

iborysenko commented 1 year ago

…ions (grpc, jobs, ...)

iborysenko commented 1 year ago

Hi @Baldinof This is preparation PR for future roadrunner/jobs integration

Now it will be possible to add something like


final class JobsWorker implements WorkerInterface
{
    public function __construct(
        private EventDispatcherInterface $eventDispatcher,
        private ConsumerInterface $consumer,
        private ProcessorRegistry $registry,
        private ChainExceptionHandler $exceptionHandler
    ) {
    }

    public function start(): void
    {
        $this->eventDispatcher->dispatch(new WorkerStartEvent());

        while ($task = $this->consumer->waitTask()) {
            $this->eventDispatcher->dispatch(new TaskReceivedEvent($task));
            $exception = null;
            try {
                $processor = $this->registry->getProcessor($task->getName());
                if (!$processor instanceof ProcessorInterface) {
                    throw new JobsException(\sprintf('missing processor for task "%s"', $task->getName()));
                }

                $task = $processor->process($task);
            } catch (\Throwable $e) {
                $exception = $e;
                $task = $this->exceptionHandler->handle($exception, $task);
            } finally {
                if (false === $task->isCompleted()) {
                    $task->complete();
                }
                $this->eventDispatcher->dispatch(new TaskCompletedEvent($task, $exception));
            }
        }

        $this->eventDispatcher->dispatch(new WorkerStopEvent());
    }
}
Baldinof commented 1 year ago

Hi!

Nice, I'll try to review it this week :)

FluffyDiscord commented 1 year ago

@iborysenko would you mind adding full usage example to README ?

iborysenko commented 1 year ago

FluffyDiscord

Hi, you meant full roadrunner-jobs configuration, or symfony flex setup ? My implementation of roadrunner-jobs will be in the next MR, this MR is just structure preparation

FluffyDiscord commented 1 year ago

FluffyDiscord

Hi, you meant full roadrunner-jobs configuration, or symfony flex setup ? My implementation of roadrunner-jobs will be in the next MR, this MR is just structure preparation

Aha, then I am looking forward to the next MR 😊

Baldinof commented 1 year ago

Thank you @iborysenko!

koekaverna commented 1 year ago

It's awesome! Waiting for release!