Skip to content

Commit

Permalink
Adds detection for application starting mode, to skip some initializa…
Browse files Browse the repository at this point in the history
…tion parts.
  • Loading branch information
butschster committed Apr 29, 2024
1 parent d95f6b0 commit 4e28645
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 13 deletions.
8 changes: 5 additions & 3 deletions .rr-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ rpc:
server:
command: 'php app.php'
relay: pipes
env:
- MODE: 'roadrunner'
on_init:
command: "php app.php configure"

Expand Down Expand Up @@ -42,13 +44,13 @@ tcp:
kv:
events:
driver: memory
config: {}
config: { }
local:
driver: memory
config: {}
config: { }

jobs:
consume: []
consume: [ ]

service:
nginx:
Expand Down
2 changes: 2 additions & 0 deletions .rr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ rpc:
server:
command: 'php app.php'
relay: pipes
env:
- MODE: 'roadrunner'
on_init:
command: "php app.php configure"

Expand Down
13 changes: 8 additions & 5 deletions app/modules/Projects/Application/ProjectBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Modules\Projects\Application;

use App\Application\Mode;
use Modules\Projects\Domain\ProjectFactoryInterface;
use Modules\Projects\Domain\ProjectLocatorInterface;
use Spiral\Boot\Bootloader\Bootloader;
Expand Down Expand Up @@ -39,11 +40,13 @@ public function defineSingletons(): array
];
}

public function init(ConsoleBootloader $console): void
public function init(ConsoleBootloader $console, Mode $mode): void
{
$console->addConfigureSequence(
sequence: 'projects:register',
header: 'Register all projects in the system',
);
if ($mode->insideRoadRunner()) {
$console->addConfigureSequence(
sequence: 'projects:register',
header: 'Register all projects in the system',
);
}
}
}
13 changes: 8 additions & 5 deletions app/modules/Webhooks/Application/WebhooksBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Modules\Webhooks\Application;

use App\Application\Mode;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use Modules\Webhooks\Domain\DeliveryFactoryInterface;
Expand Down Expand Up @@ -86,11 +87,13 @@ public function defineSingletons(): array
];
}

public function init(ConsoleBootloader $console): void
public function init(ConsoleBootloader $console, Mode $mode): void
{
$console->addConfigureSequence(
sequence: 'webhooks:register',
header: 'Register webhooks from configuration',
);
if ($mode->insideRoadRunner()) {
$console->addConfigureSequence(
sequence: 'webhooks:register',
header: 'Register webhooks from configuration',
);
}
}
}
31 changes: 31 additions & 0 deletions app/src/Application/Mode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Application;

use Spiral\Boot\EnvironmentInterface;
use Spiral\Boot\Injector\InjectableEnumInterface;
use Spiral\Boot\Injector\ProvideFrom;

#[ProvideFrom(method: 'detect')]
enum Mode implements InjectableEnumInterface
{
case Cli;
case RoadRunner;

public function insideRoadRunner(): bool
{
return $this === self::RoadRunner;
}

public static function detect(EnvironmentInterface $environment): self
{
$value = $environment->get('MODE');

return match ($value) {
'roadrunner', 'rr' => self::RoadRunner,
default => self::Cli,
};
}
}

0 comments on commit 4e28645

Please sign in to comment.