Skip to content

Commit

Permalink
Added WorkerRegistry and ability to add additional worker implementat…
Browse files Browse the repository at this point in the history
…ions (grpc, jobs, ...) (#98)
  • Loading branch information
iborysenko authored Mar 7, 2023
1 parent f0c0389 commit adf9501
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 73 deletions.
34 changes: 26 additions & 8 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
use Baldinof\RoadRunnerBundle\Reboot\KernelRebootStrategyInterface;
use Baldinof\RoadRunnerBundle\RoadRunnerBridge\HttpFoundationWorker;
use Baldinof\RoadRunnerBundle\RoadRunnerBridge\HttpFoundationWorkerInterface;
use Baldinof\RoadRunnerBundle\Worker\Dependencies;
use Baldinof\RoadRunnerBundle\Worker\GrpcWorker;
use Baldinof\RoadRunnerBundle\Worker\GrpcWorker as InternalGrpcWorker;
use Baldinof\RoadRunnerBundle\Worker\GrpcWorkerInterface;
use Baldinof\RoadRunnerBundle\Worker\Worker;
use Baldinof\RoadRunnerBundle\Worker\WorkerInterface;
use Baldinof\RoadRunnerBundle\Worker\HttpDependencies;
use Baldinof\RoadRunnerBundle\Worker\HttpWorker as InternalHttpWorker;
use Baldinof\RoadRunnerBundle\Worker\WorkerRegistry;
use Baldinof\RoadRunnerBundle\Worker\WorkerRegistryInterface;
use Psr\Log\LoggerInterface;
use Spiral\Goridge\RPC\RPCInterface;
use Spiral\RoadRunner\Environment;
Expand Down Expand Up @@ -73,7 +74,10 @@ function service(string $id): ReferenceConfigurator
$services->set(HttpFoundationWorkerInterface::class, HttpFoundationWorker::class)
->args([service(HttpWorkerInterface::class)]);

$services->set(WorkerInterface::class, Worker::class)
$services->set(WorkerRegistryInterface::class, WorkerRegistry::class)
->public();

$services->set(InternalHttpWorker::class)
->public() // Manually retrieved on the DIC in the Worker if the kernel has been rebooted
->tag('monolog.logger', ['channel' => BaldinofRoadRunnerExtension::MONOLOG_CHANNEL])
->args([
Expand All @@ -82,7 +86,14 @@ function service(string $id): ReferenceConfigurator
service(HttpFoundationWorkerInterface::class),
]);

$services->set(Dependencies::class)
$services
->get(WorkerRegistryInterface::class)
->call('registerWorker', [
Environment\Mode::MODE_HTTP,
service(InternalHttpWorker::class),
]);

$services->set(HttpDependencies::class)
->public() // Manually retrieved on the DIC in the Worker if the kernel has been rebooted
->args([
service(MiddlewareStack::class),
Expand All @@ -91,7 +102,7 @@ function service(string $id): ReferenceConfigurator
]);

$services->set(WorkerCommand::class)
->args([service(WorkerInterface::class)])
->args([service(InternalHttpWorker::class)])
->autoconfigure();

$services->set(KernelHandler::class)
Expand Down Expand Up @@ -125,7 +136,7 @@ function service(string $id): ReferenceConfigurator
service(GrpcInvoker::class),
]);

$services->set(GrpcWorkerInterface::class, GrpcWorker::class)
$services->set(GrpcWorkerInterface::class, InternalGrpcWorker::class)
->public() // Manually retrieved on the DIC in the Worker if the kernel has been rebooted
->tag('monolog.logger', ['channel' => BaldinofRoadRunnerExtension::MONOLOG_CHANNEL])
->args([
Expand All @@ -134,5 +145,12 @@ function service(string $id): ReferenceConfigurator
service(GrpcServiceProvider::class),
service(GrpcServer::class),
]);

$services
->get(WorkerRegistryInterface::class)
->call('registerWorker', [
Environment\Mode::MODE_GRPC,
service(GrpcWorkerInterface::class),
]);
}
};
38 changes: 0 additions & 38 deletions src/Runtime/GrpcRunner.php

This file was deleted.

17 changes: 13 additions & 4 deletions src/Runtime/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,34 @@

namespace Baldinof\RoadRunnerBundle\Runtime;

use Baldinof\RoadRunnerBundle\Worker\WorkerInterface;
use Baldinof\RoadRunnerBundle\Worker\WorkerRegistryInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Runtime\RunnerInterface;

class Runner implements RunnerInterface
{
private KernelInterface $kernel;
private string $mode;

public function __construct(KernelInterface $kernel)
public function __construct(KernelInterface $kernel, string $mode)
{
$this->kernel = $kernel;
$this->mode = $mode;
}

public function run(): int
{
$this->kernel->boot();

/** @var WorkerInterface */
$worker = $this->kernel->getContainer()->get(WorkerInterface::class);
/** @var WorkerRegistryInterface $registry */
$registry = $this->kernel->getContainer()->get(WorkerRegistryInterface::class);
$worker = $registry->getWorker($this->mode);

if (null === $worker) {
error_log(sprintf('Missing RR worker implementation for %s mode', $this->mode));

return 1;
}

$worker->start();

Expand Down
9 changes: 2 additions & 7 deletions src/Runtime/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Baldinof\RoadRunnerBundle\Runtime;

use Spiral\RoadRunner\Environment\Mode;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Runtime\RunnerInterface;
use Symfony\Component\Runtime\SymfonyRuntime;
Expand All @@ -13,12 +12,8 @@ class Runtime extends SymfonyRuntime
{
public function getRunner(?object $application): RunnerInterface
{
if ($application instanceof KernelInterface && getenv('RR_MODE') === Mode::MODE_HTTP) {
return new Runner($application);
}

if ($application instanceof KernelInterface && getenv('RR_MODE') === Mode::MODE_GRPC) {
return new GrpcRunner($application);
if ($application instanceof KernelInterface && false !== getenv('RR_MODE')) {
return new Runner($application, getenv('RR_MODE'));
}

return parent::getRunner($application);
Expand Down
6 changes: 4 additions & 2 deletions src/Worker/GrpcWorkerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Baldinof\RoadRunnerBundle\Worker;

interface GrpcWorkerInterface
/**
* @deprecated and will be removed at 3.0, use Baldinof\RoadRunnerBundle\Worker\WorkerInterface instead
*/
interface GrpcWorkerInterface extends WorkerInterface
{
public function start(): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* A simple container class holding services needed by the Worker.
* A simple container class holding services needed by the Http Worker.
*
* It's used to ease worker dependencies retrieval when the kernel
* has been rebooted.
*
* @internal
*/
final class Dependencies
final class HttpDependencies
{
private MiddlewareStack $requestHandler;
private KernelRebootStrategyInterface $kernelRebootStrategy;
Expand Down
12 changes: 6 additions & 6 deletions src/Worker/Worker.php → src/Worker/HttpWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
/**
* @internal
*/
final class Worker implements WorkerInterface
final class HttpWorker implements WorkerInterface
{
private KernelInterface $kernel;
private LoggerInterface $logger;
private Dependencies $dependencies;
private HttpDependencies $dependencies;
private HttpFoundationWorkerInterface $httpFoundationWorker;

private array $trustedProxies = [];
Expand All @@ -49,8 +49,8 @@ public function __construct(

$container = $kernel->getContainer();

/** @var Dependencies */
$dependencies = $container->get(Dependencies::class);
/** @var HttpDependencies */
$dependencies = $container->get(HttpDependencies::class);
$this->dependencies = $dependencies;

if ($container->hasParameter('kernel.trusted_proxies') && $container->hasParameter('kernel.trusted_headers')) {
Expand Down Expand Up @@ -130,8 +130,8 @@ public function start(): void
} finally {
if ($this->kernel instanceof RebootableInterface && $this->dependencies->getKernelRebootStrategy()->shouldReboot()) {
$this->kernel->reboot(null);
/** @var Dependencies */
$deps = $this->kernel->getContainer()->get(Dependencies::class);
/** @var HttpDependencies */
$deps = $this->kernel->getContainer()->get(HttpDependencies::class);

$this->dependencies = $deps;
$this->dependencies->getEventDispatcher()->dispatch(new WorkerKernelRebootedEvent());
Expand Down
23 changes: 23 additions & 0 deletions src/Worker/WorkerRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Baldinof\RoadRunnerBundle\Worker;

/**
* @interal
*/
final class WorkerRegistry implements WorkerRegistryInterface
{
private array $workers = [];

public function registerWorker(string $mode, WorkerInterface $worker): void
{
$this->workers[$mode] = $worker;
}

public function getWorker(string $mode): ?WorkerInterface
{
return $this->workers[$mode] ?? null;
}
}
12 changes: 12 additions & 0 deletions src/Worker/WorkerRegistryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Baldinof\RoadRunnerBundle\Worker;

interface WorkerRegistryInterface
{
public function registerWorker(string $mode, WorkerInterface $worker): void;

public function getWorker(string $mode): ?WorkerInterface;
}
12 changes: 6 additions & 6 deletions tests/Worker/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
use Baldinof\RoadRunnerBundle\Http\RequestHandlerInterface;
use Baldinof\RoadRunnerBundle\Reboot\KernelRebootStrategyInterface;
use Baldinof\RoadRunnerBundle\RoadRunnerBridge\HttpFoundationWorkerInterface;
use Baldinof\RoadRunnerBundle\Worker\Dependencies;
use Baldinof\RoadRunnerBundle\Worker\Worker;
use Baldinof\RoadRunnerBundle\Worker\HttpDependencies;
use Baldinof\RoadRunnerBundle\Worker\HttpWorker;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
Expand All @@ -35,7 +35,7 @@ class WorkerTest extends TestCase

public static $rebootStrategyReturns = false;

private Worker $worker;
private HttpWorker $worker;
private \SplStack $requests;
private \Closure $responder;

Expand Down Expand Up @@ -101,9 +101,9 @@ public function clear(): void

$this->container = $c;

$c->set(Dependencies::class, new Dependencies(new MiddlewareStack($this->handler), $kernelBootStrategyClass, $this->eventDispatcher));
$c->set(HttpDependencies::class, new HttpDependencies(new MiddlewareStack($this->handler), $kernelBootStrategyClass, $this->eventDispatcher));

$this->worker = new Worker(
$this->worker = new HttpWorker(
$this->kernel->reveal(),
new NullLogger(),
$this->httpFoundationWorker->reveal()
Expand All @@ -117,7 +117,7 @@ public function test_it_setup_trusted_proxies_and_hosts()
$this->container->setParameter('kernel.trusted_proxies', '10.0.0.1,REMOTE_ADDR');
$this->container->setParameter('kernel.trusted_headers', Request::HEADER_FORWARDED);

$worker = new Worker(
$worker = new HttpWorker(
$this->kernel->reveal(),
new NullLogger(),
$this->httpFoundationWorker->reveal()
Expand Down

0 comments on commit adf9501

Please sign in to comment.