Skip to content

Commit

Permalink
ConfigLoader rework
Browse files Browse the repository at this point in the history
  • Loading branch information
luzrain committed Feb 7, 2024
1 parent 6470a29 commit 4ca4f7e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 102 deletions.
78 changes: 19 additions & 59 deletions src/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@
namespace Luzrain\PhpRunnerBundle;

use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class ConfigLoader implements CacheWarmerInterface
{
private array $config;
private array $config = [];
private ConfigCache $cache;
private string $yamlConfigFilePath;

public function __construct(string $projectDir, string $cacheDir, bool $isDebug)
public function __construct(private string $projectDir, string $cacheDir, bool $isDebug)
{
$this->yamlConfigFilePath = \sprintf('%s/config/packages/phprunner.yaml', $projectDir);
$cacheConfigFilePath = \sprintf('%s/phprunner_config.cache.php', $cacheDir);
$this->cache = new ConfigCache($cacheConfigFilePath, $isDebug);
$this->cache = new ConfigCache(\sprintf('%s/phprunner_config.cache.php', $cacheDir), $isDebug);
}

public function isOptional(): bool
Expand All @@ -31,64 +25,30 @@ public function isOptional(): bool

public function warmUp(string $cacheDir, string $buildDir = null): array
{
$resources = \is_file($this->yamlConfigFilePath) ? [new FileResource($this->yamlConfigFilePath)] : [];
$this->cache->write(\sprintf('<?php return %s;', \var_export($this->config, true)), $resources);
$metadata = new DirectoryResource(\sprintf('%s/config/packages', $this->projectDir), '/phprunner/');
$this->cache->write(\sprintf('<?php return %s;', \var_export($this->config, true)), [$metadata]);

return [];
}

public function warmUpInFork(KernelFactory $kernelFactory): void
{
if (\pcntl_fork() === 0) {
$kernelFactory->createKernel()->boot();
exit;
} else {
\pcntl_wait($status);
unset($status);
}
}

public function isFresh(): bool
{
return $this->cache->isFresh();
}

/**
* @psalm-suppress UnresolvableInclude
* @psalm-suppress RedundantPropertyInitializationCheck
*/
private function getConfigCache(): array
{
return $this->config ??= require $this->cache->getPath();
}

public function setConfig(array $config): void
{
$this->config[0] = $config;
}

public function setProcessConfig(array $config): void
{
$this->config[1] = $config;
}

public function setSchedulerConfig(array $config): void
{
$this->config[2] = $config;
$this->config = $config;
}

public function getConfig(): array
public function getConfig(KernelFactory|null $kernelFactory = null): array
{
return $this->getConfigCache()[0];
}

public function getProcessConfig(): array
{
return $this->getConfigCache()[1];
}
// Warm up cache if no fresh config found (do it in a forked process as the main process should not boot kernel)
if ($this->cache->isFresh() === false && $kernelFactory !== null) {
if (\pcntl_fork() === 0) {
$kernelFactory->createKernel()->boot();
exit;
} else {
\pcntl_wait($status);
unset($status);
}
}

public function getSchedulerConfig(): array
{
return $this->getConfigCache()[2];
return require $this->cache->getPath();
}
}
5 changes: 1 addition & 4 deletions src/KernelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

final readonly class KernelFactory
{
private string $projectDir;
private string $environment;
private bool $isDebug;
private string $projectDir;

/**
* @psalm-suppress InvalidPropertyAssignmentValue
*/
public function __construct(private \Closure $app, private array $args, array $options)
{
$this->projectDir = $options['project_dir'];
Expand Down
40 changes: 1 addition & 39 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@ public function run(): int
isDebug: $this->kernelFactory->isDebug(),
);

// Warm up cache if no phprunner fresh config found (do it in a forked process as the main process should not boot kernel)
if (!$configLoader->isFresh()) {
$configLoader->warmUpInFork($this->kernelFactory);
}

$config = $configLoader->getConfig();
//$schedulerConfig = $configLoader->getSchedulerConfig();
//$processConfig = $configLoader->getProcessConfig();
$config = $configLoader->getConfig($this->kernelFactory);

$phpRunner = new PhpRunner(
pidFile: $config['pid_file'],
Expand All @@ -52,36 +45,5 @@ public function run(): int
}

return $phpRunner->run();


if (!empty($schedulerConfig)) {
new SchedulerWorker(
kernelFactory: $this->kernelFactory,
user: $config['user'],
group: $config['group'],
schedulerConfig: $schedulerConfig,
);
}

if ($config['reload_strategy']['file_monitor']['active'] && $this->kernelFactory->isDebug()) {
new FileMonitorWorker(
user: $config['user'],
group: $config['group'],
sourceDir: $config['reload_strategy']['file_monitor']['source_dir'],
filePattern: $config['reload_strategy']['file_monitor']['file_pattern'],
);
}

if (!empty($processConfig)) {
new SupervisorWorker(
kernelFactory: $this->kernelFactory,
user: $config['user'],
group: $config['group'],
processConfig: $processConfig,
);
}


return 0;
}
}

0 comments on commit 4ca4f7e

Please sign in to comment.