Skip to content

Commit

Permalink
Disable background workers if they are not supported (see #6995)
Browse files Browse the repository at this point in the history
Description
-----------

Fixes #6907.

Ready for review but needs an update of the `composer.json` entries before merging. Requires Toflar/cronjob-supervisor#2 to be released as 2.0.0 which I'll only do once this PR is approved concept-wise.

Commits
-------

b63f645 Update to toflar/cronjob-supervisor v2 and disable workers if not sup…
7b81d99 Use static helpers
c1e55c0 Update to stable release
  • Loading branch information
Toflar committed Mar 11, 2024
1 parent 3344c5c commit 498049b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
"terminal42/escargot": "^1.6",
"terminal42/service-annotation-bundle": "^1.1",
"tijsverkoyen/css-to-inline-styles": "^2.0",
"toflar/cronjob-supervisor": "^1.0",
"toflar/cronjob-supervisor": "^2.0",
"toflar/psr6-symfony-http-cache-store": "^4.0",
"twig/extra-bundle": "^3.0",
"twig/string-extra": "^3.0",
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
"symfony/yaml": "^6.4",
"terminal42/escargot": "^1.6",
"terminal42/service-annotation-bundle": "^1.1",
"toflar/cronjob-supervisor": "^1.0",
"toflar/cronjob-supervisor": "^2.0",
"twig/string-extra": "^3.0",
"twig/twig": "^3.8",
"ua-parser/uap-php": "^3.9",
Expand Down
3 changes: 1 addition & 2 deletions core-bundle/config/commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,10 @@ services:

contao.command.supervise_workers:
class: Contao\CoreBundle\Command\SuperviseWorkersCommand
factory: [null, create]
arguments:
- '@messenger.receiver_locator'
- '@contao.process_util'
- '%kernel.cache_dir%/worker-supervisor'
- ~
- ~

contao.command.symlinks:
Expand Down
5 changes: 0 additions & 5 deletions core-bundle/src/Command/SuperviseWorkersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ public function __construct(
parent::__construct();
}

public static function create(ContainerInterface $messengerTransportLocator, ProcessUtil $processUtil, string $storageDirectory, array $workers): self
{
return new self($messengerTransportLocator, $processUtil, new Supervisor($storageDirectory), $workers);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
Expand Down
19 changes: 14 additions & 5 deletions core-bundle/src/DependencyInjection/ContaoCoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Toflar\CronjobSupervisor\Supervisor;

class ContaoCoreExtension extends Extension implements PrependExtensionInterface, ConfigureFilesystemInterface
{
Expand Down Expand Up @@ -253,16 +254,24 @@ private function handleMessengerConfig(array $config, ContainerBuilder $containe
return;
}

// Disable workers completely if supervision is not supported
if (!Supervisor::canSuperviseWithProviders(Supervisor::getDefaultProviders())) {
$config['messenger']['workers'] = [];
} else {
$supervisor = new Definition(Supervisor::class);
$supervisor->setFactory([Supervisor::class, 'withDefaultProviders']);
$supervisor->addArgument('%kernel.cache_dir%/worker-supervisor');

$command = $container->getDefinition('contao.command.supervise_workers');
$command->setArgument(2, $supervisor);
$command->setArgument(3, $config['messenger']['workers']);
}

// No workers defined -> remove our cron job and the command
if (0 === \count($config['messenger']['workers'])) {
$container->removeDefinition('contao.cron.supervise_workers');
$container->removeDefinition('contao.command.supervise_workers');

return;
}

$command = $container->getDefinition('contao.command.supervise_workers');
$command->setArgument(3, $config['messenger']['workers']);
}

private function handleSearchConfig(array $config, ContainerBuilder $container): void
Expand Down

0 comments on commit 498049b

Please sign in to comment.