Skip to content

Commit

Permalink
Merge pull request #1 from FluffyDiscord/#lazy-boot
Browse files Browse the repository at this point in the history
#lazy boot
  • Loading branch information
FluffyDiscord authored Feb 21, 2024
2 parents c7bf376 + ad7feb9 commit 186e6a3
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 24 deletions.
23 changes: 1 addition & 22 deletions .idea/phpspec.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ server:
APP_RUNTIME: FluffyDiscord\RoadRunnerBundle\Runtime\Runtime
```

## Configuration
fluffy_discord_road_runner.yaml
```yaml
fluffy_discord_road_runner:
http:
# This decides when to boot the Symfony kernel.
#
# false (default) - before first request (worker takes some time to be ready, but app has consistent response times)
# true - once first request arrives (worker is ready immediately, but inconsistent response times due to kernel boot time spikes)
#
# If you use large amount of workers, you might want to set this to true or else the RR boot up might
# take a lot of time or just boot up using only a few "emergency" workers
# and then use dynamic worker scaling as described here https://docs.roadrunner.dev/php-worker/scaling
lazy_boot: false

centrifugo:
# See http section
lazy_boot: false
```
## Running behind a load balancer or a proxy
If you want to use `REMOTE_ADDR` as trusted proxy, replace it with `0.0.0.0/0` instead
or else your trusted headers will not work.
Expand Down
2 changes: 2 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
->set(BundleHttpWorker::class)
->public()
->args([
false,
service(KernelInterface::class),
service(SentryHubInterface::class)->nullOnInvalid(),
service(HttpFoundationFactoryInterface::class)->nullOnInvalid(),
Expand Down Expand Up @@ -108,6 +109,7 @@
->set(CentrifugoWorker::class)
->public()
->args([
false,
service(KernelInterface::class),
service(CentrifugoWorkerInterface::class),
service(EventDispatcherInterface::class),
Expand Down
31 changes: 31 additions & 0 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace FluffyDiscord\RoadRunnerBundle\Configuration;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$builder = new TreeBuilder("fluffy_discord_road_runner");

$builder->getRootNode()
->children()
->arrayNode("http")
->children()
->booleanNode("lazy_boot")->defaultFalse()->end()
->end()
->end()
->arrayNode("centrifugo")
->children()
->booleanNode("lazy_boot")->defaultFalse()->end()
->end()
->end()
->end()
;

return $builder;
}
}
19 changes: 17 additions & 2 deletions src/DependencyInjection/FluffyDiscordRoadRunnerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace FluffyDiscord\RoadRunnerBundle\DependencyInjection;

use FluffyDiscord\RoadRunnerBundle\Configuration\Configuration;
use FluffyDiscord\RoadRunnerBundle\Worker\CentrifugoWorker;
use FluffyDiscord\RoadRunnerBundle\Worker\HttpWorker;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
Expand All @@ -11,7 +14,19 @@ class FluffyDiscordRoadRunnerExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new PhpFileLoader($container, new FileLocator(__DIR__."/../../config"));
$loader = new PhpFileLoader($container, new FileLocator(__DIR__ . "/../../config"));
$loader->load("services.php");

$config = $this->processConfiguration(new Configuration(), $configs);

if (isset($config["http"]["lazy_boot"]) && $container->hasDefinition(HttpWorker::class)) {
$definition = $container->getDefinition(HttpWorker::class);
$definition->replaceArgument(0, $config["http"]["lazy_boot"]);
}

if (isset($config["centrifugo"]["lazy_boot"]) && $container->hasDefinition(CentrifugoWorker::class)) {
$definition = $container->getDefinition(CentrifugoWorker::class);
$definition->replaceArgument(0, $config["centrifugo"]["lazy_boot"]);
}
}
}
}
5 changes: 5 additions & 0 deletions src/Worker/CentrifugoWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
readonly class CentrifugoWorker implements WorkerInterface
{
public function __construct(
private bool $lazyBoot,
private KernelInterface $kernel,
private RoadRunnerCentrifugoWorker $worker,
private EventDispatcherInterface $eventDispatcher,
Expand All @@ -38,6 +39,10 @@ public function __construct(

public function start(): void
{
if (!$this->lazyBoot) {
$this->kernel->boot();
}

while ($request = $this->worker->waitRequest()) {
$this->sentryHubInterface?->pushScope();

Expand Down
5 changes: 5 additions & 0 deletions src/Worker/HttpWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
private Psr7\Factory\Psr17Factory $psrFactory;

public function __construct(
private bool $lazyBoot,
private KernelInterface $kernel,
private ?SentryHubInterface $sentryHubInterface = null,
?HttpFoundationFactoryInterface $httpFoundationFactory = null,
Expand All @@ -41,6 +42,10 @@ public function start(): void
$this->psrFactory,
);

if (!$this->lazyBoot) {
$this->kernel->boot();
}

try {
while ($request = $worker->waitRequest()) {
$this->sentryHubInterface?->pushScope();
Expand Down

0 comments on commit 186e6a3

Please sign in to comment.