Skip to content

Commit e19c57f

Browse files
authored
JobExecution id generation (#13)
* Centralize JobExecution default id generation * Use service to generate JobExecution id
1 parent 222d191 commit e19c57f

File tree

12 files changed

+64
-13
lines changed

12 files changed

+64
-13
lines changed

src/batch-symfony-console/src/RunCommandJobLauncher.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ public function __construct(
4949
*/
5050
public function launch(string $name, array $configuration = []): JobExecution
5151
{
52-
$configuration['_id'] = $configuration['_id'] ?? uniqid();
53-
5452
$jobExecution = $this->jobExecutionFactory->create($name, $configuration);
5553
$jobExecution->setStatus(BatchStatus::PENDING);
5654
$this->jobExecutionStorage->store($jobExecution);

src/batch-symfony-console/tests/RunCommandJobLauncherTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Yokai\Batch\Bridge\Symfony\Console\CommandRunner;
1313
use Yokai\Batch\Bridge\Symfony\Console\RunCommandJobLauncher;
1414
use Yokai\Batch\Factory\JobExecutionFactory;
15+
use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator;
1516
use Yokai\Batch\JobExecution;
1617
use Yokai\Batch\Storage\JobExecutionStorageInterface;
1718

@@ -44,7 +45,7 @@ function ($jobExecution): bool {
4445
$storage->store($jobExecutionAssertions)->shouldBeCalledTimes(1);
4546

4647
$launcher = new RunCommandJobLauncher(
47-
new JobExecutionFactory(),
48+
new JobExecutionFactory(new UniqidJobExecutionIdGenerator()),
4849
$commandRunner->reveal(),
4950
$storage->reveal(),
5051
'test.log'

src/batch-symfony-framework/src/Resources/services/global/alias.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@
1212

1313
<service id="Yokai\Batch\Registry\JobRegistry"
1414
alias="yokai_batch.job_registry"/>
15+
16+
<service id="Yokai\Batch\Factory\JobExecutionIdGeneratorInterface"
17+
alias="yokai_batch.job_execution_id_generator.uniqid"/>
1518
</services>
1619
</container>

src/batch-symfony-framework/src/Resources/services/global/factory.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
<defaults public="false"/>
99

1010
<service id="yokai_batch.job_execution_factory"
11-
class="Yokai\Batch\Factory\JobExecutionFactory"/>
11+
class="Yokai\Batch\Factory\JobExecutionFactory">
12+
<argument type="service" id="Yokai\Batch\Factory\JobExecutionIdGeneratorInterface"/>
13+
</service>
14+
15+
<service id="yokai_batch.job_execution_id_generator.uniqid"
16+
class="Yokai\Batch\Factory\UniqidJobExecutionIdGenerator"/>
1217
</services>
1318
</container>

src/batch-symfony-messenger/src/DispatchMessageJobLauncher.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ public function __construct(
4040

4141
public function launch(string $name, array $configuration = []): JobExecution
4242
{
43-
$configuration['_id'] = $configuration['_id'] ?? uniqid();
44-
4543
// create and store execution before dispatching message
4644
// guarantee job execution exists if message bus transport is asynchronous
4745
$jobExecution = $this->jobExecutionFactory->create($name, $configuration);

src/batch-symfony-messenger/tests/DispatchMessageJobLauncherTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Yokai\Batch\Bridge\Symfony\Messenger\DispatchMessageJobLauncher;
1515
use Yokai\Batch\Bridge\Symfony\Messenger\LaunchJobMessage;
1616
use Yokai\Batch\Factory\JobExecutionFactory;
17+
use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator;
1718
use Yokai\Batch\JobExecution;
1819
use Yokai\Batch\Storage\JobExecutionStorageInterface;
1920

@@ -52,7 +53,7 @@ function ($message): bool {
5253
->willReturn(new Envelope(new LaunchJobMessage('unused')));
5354

5455
$jobLauncher = new DispatchMessageJobLauncher(
55-
new JobExecutionFactory(),
56+
new JobExecutionFactory(new UniqidJobExecutionIdGenerator()),
5657
$storage->reveal(),
5758
$messageBus->reveal()
5859
);

src/batch/src/Factory/JobExecutionFactory.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@
99

1010
final class JobExecutionFactory
1111
{
12+
/**
13+
* @var JobExecutionIdGeneratorInterface
14+
*/
15+
private JobExecutionIdGeneratorInterface $idGenerator;
16+
17+
/**
18+
* @param JobExecutionIdGeneratorInterface $idGenerator
19+
*/
20+
public function __construct(JobExecutionIdGeneratorInterface $idGenerator)
21+
{
22+
$this->idGenerator = $idGenerator;
23+
}
24+
1225
/**
1326
* @param string $name
1427
* @param array $configuration
@@ -17,7 +30,7 @@ final class JobExecutionFactory
1730
*/
1831
public function create(string $name, array $configuration = []): JobExecution
1932
{
20-
$configuration['_id'] = $configuration['_id'] ?? uniqid();
33+
$configuration['_id'] = $configuration['_id'] ?? $this->idGenerator->generate();
2134

2235
return JobExecution::createRoot($configuration['_id'], $name, null, new JobParameters($configuration));
2336
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Factory;
6+
7+
interface JobExecutionIdGeneratorInterface
8+
{
9+
/**
10+
* @return string
11+
*/
12+
public function generate(): string;
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Factory;
6+
7+
final class UniqidJobExecutionIdGenerator implements JobExecutionIdGeneratorInterface
8+
{
9+
/**
10+
* @inheritdoc
11+
*/
12+
public function generate(): string
13+
{
14+
return \uniqid();
15+
}
16+
}

src/batch/tests/Factory/JobExecutionFactoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Yokai\Batch\Factory\JobExecutionFactory;
9+
use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator;
910
use Yokai\Batch\JobExecution;
1011

1112
class JobExecutionFactoryTest extends TestCase
1213
{
1314
public function testCreate(): void
1415
{
15-
$executionFactory = new JobExecutionFactory();
16+
$executionFactory = new JobExecutionFactory(new UniqidJobExecutionIdGenerator());
1617

1718
$executionWithoutConfig = $executionFactory->create('export');
1819
self::assertSame('export', $executionWithoutConfig->getJobName());

0 commit comments

Comments
 (0)