-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow configuration of job launcher with Symfony configuration (#121)
* Allow configuration of job launcher with Symfony configuration * Fixed code style * Fixed static analysis and extract job launcher definition factory * Add symfony/messenger as suggested dependency of yokai/batch-symfony-framework * Added documentation about job launcher DSN
- Loading branch information
1 parent
b333150
commit 00ea597
Showing
13 changed files
with
326 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/batch-symfony-framework/src/DependencyInjection/JobLauncherDefinitionFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yokai\Batch\Bridge\Symfony\Framework\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Yokai\Batch\Bridge\Symfony\Console\CommandRunner; | ||
use Yokai\Batch\Bridge\Symfony\Console\RunCommandJobLauncher; | ||
use Yokai\Batch\Bridge\Symfony\Messenger\DispatchMessageJobLauncher; | ||
use Yokai\Batch\Launcher\JobLauncherInterface; | ||
use Yokai\Batch\Launcher\SimpleJobLauncher; | ||
use Yokai\Batch\Storage\JobExecutionStorageInterface; | ||
|
||
/** | ||
* This is a helper for building services definitions of {@see JobLauncherInterface}. | ||
*/ | ||
final class JobLauncherDefinitionFactory | ||
{ | ||
/** | ||
* Build a service definition from DSN string. | ||
*/ | ||
public static function fromDsn(ContainerBuilder $container, string $dsn): Definition | ||
{ | ||
$dsnParts = \parse_url($dsn); | ||
$launcherType = $dsnParts['scheme'] ?? null; | ||
\parse_str($dsnParts['query'] ?? '', $launcherConfig); | ||
/** @var array<string, string> $launcherConfig */ | ||
|
||
return match ($launcherType) { | ||
'simple' => self::simple(), | ||
'console' => self::console($launcherConfig), | ||
'messenger' => self::messenger(), | ||
'service' => self::service($container, $launcherConfig), | ||
default => throw new LogicException('Unsupported job launcher type "' . $launcherType . '".'), | ||
}; | ||
} | ||
|
||
private static function simple(): Definition | ||
{ | ||
return new Definition(SimpleJobLauncher::class, [ | ||
'$jobExecutionAccessor' => new Reference('yokai_batch.job_execution_accessor'), | ||
'$jobExecutor' => new Reference('yokai_batch.job_executor'), | ||
]); | ||
} | ||
|
||
/** | ||
* @param array<string, string> $config | ||
*/ | ||
private static function console(array $config): Definition | ||
{ | ||
$log = $config['log'] ?? 'batch_execute.log'; | ||
|
||
return new Definition(RunCommandJobLauncher::class, [ | ||
'$jobExecutionFactory' => new Reference('yokai_batch.job_execution_factory'), | ||
'$commandRunner' => new Definition(CommandRunner::class, [ | ||
'$binDir' => '%kernel.project_dir%/bin', | ||
'$logDir' => '%kernel.logs_dir%', | ||
]), | ||
'$jobExecutionStorage' => new Reference(JobExecutionStorageInterface::class), | ||
'$logFilename' => $log, | ||
]); | ||
} | ||
|
||
private static function messenger(): Definition | ||
{ | ||
return new Definition(DispatchMessageJobLauncher::class, [ | ||
'$jobExecutionFactory' => new Reference('yokai_batch.job_execution_factory'), | ||
'$jobExecutionStorage' => new Reference(JobExecutionStorageInterface::class), | ||
'$messageBus' => new Reference(MessageBusInterface::class), | ||
]); | ||
} | ||
|
||
/** | ||
* @param array<string, string> $config | ||
*/ | ||
private static function service(ContainerBuilder $container, array $config): Definition | ||
{ | ||
$service = $config['service'] ?? throw new LogicException( | ||
'Missing "service" parameter to configure the job launcher.', | ||
); | ||
|
||
return $container->getDefinition($service); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
src/batch-symfony-framework/src/Resources/services/global/launcher.xml
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
src/batch-symfony-framework/src/Resources/services/symfony/console/launcher.xml
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
src/batch-symfony-framework/src/Resources/services/symfony/console/util.xml
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.