-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRunCommandJobLauncher.php
49 lines (42 loc) · 1.57 KB
/
RunCommandJobLauncher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
declare(strict_types=1);
namespace Yokai\Batch\Bridge\Symfony\Console;
use Yokai\Batch\BatchStatus;
use Yokai\Batch\Factory\JobExecutionFactory;
use Yokai\Batch\JobExecution;
use Yokai\Batch\Launcher\JobLauncherInterface;
use Yokai\Batch\Storage\JobExecutionStorageInterface;
/**
* This {@see JobLauncherInterface} will execute job via an asynchronous symfony command.
*
* Example, if you call RunCommandJobLauncher::launch('import', ['foo'=>'bar']),
* this command will run (with absolute pathes) :
*
* php bin/console yokai:batch:run import '{"foo":"bar"}' >> var/log/batch_execute.log 2>&1 &
*/
final class RunCommandJobLauncher implements JobLauncherInterface
{
public function __construct(
private JobExecutionFactory $jobExecutionFactory,
private CommandRunner $commandRunner,
private JobExecutionStorageInterface $jobExecutionStorage,
private string $logFilename = 'batch_execute.log',
) {
}
public function launch(string $name, array $configuration = []): JobExecution
{
$jobExecution = $this->jobExecutionFactory->create($name, $configuration);
$configuration['_id'] ??= $jobExecution->getId();
$jobExecution->setStatus(BatchStatus::PENDING);
$this->jobExecutionStorage->store($jobExecution);
$this->commandRunner->runAsync(
'yokai:batch:run',
$this->logFilename,
[
'job' => $name,
'configuration' => \json_encode($configuration, JSON_THROW_ON_ERROR),
],
);
return $jobExecution;
}
}