Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/batch-symfony-console/src/RunCommandJobLauncher.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public function __construct(
*/
public function launch(string $name, array $configuration = []): JobExecution
{
$configuration['_id'] = $configuration['_id'] ?? uniqid();

$jobExecution = $this->jobExecutionFactory->create($name, $configuration);
$jobExecution->setStatus(BatchStatus::PENDING);
$this->jobExecutionStorage->store($jobExecution);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Yokai\Batch\Bridge\Symfony\Console\CommandRunner;
use Yokai\Batch\Bridge\Symfony\Console\RunCommandJobLauncher;
use Yokai\Batch\Factory\JobExecutionFactory;
use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator;
use Yokai\Batch\JobExecution;
use Yokai\Batch\Storage\JobExecutionStorageInterface;

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

$launcher = new RunCommandJobLauncher(
new JobExecutionFactory(),
new JobExecutionFactory(new UniqidJobExecutionIdGenerator()),
$commandRunner->reveal(),
$storage->reveal(),
'test.log'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@

<service id="Yokai\Batch\Registry\JobRegistry"
alias="yokai_batch.job_registry"/>

<service id="Yokai\Batch\Factory\JobExecutionIdGeneratorInterface"
alias="yokai_batch.job_execution_id_generator.uniqid"/>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
<defaults public="false"/>

<service id="yokai_batch.job_execution_factory"
class="Yokai\Batch\Factory\JobExecutionFactory"/>
class="Yokai\Batch\Factory\JobExecutionFactory">
<argument type="service" id="Yokai\Batch\Factory\JobExecutionIdGeneratorInterface"/>
</service>

<service id="yokai_batch.job_execution_id_generator.uniqid"
class="Yokai\Batch\Factory\UniqidJobExecutionIdGenerator"/>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public function __construct(

public function launch(string $name, array $configuration = []): JobExecution
{
$configuration['_id'] = $configuration['_id'] ?? uniqid();

// create and store execution before dispatching message
// guarantee job execution exists if message bus transport is asynchronous
$jobExecution = $this->jobExecutionFactory->create($name, $configuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Yokai\Batch\Bridge\Symfony\Messenger\DispatchMessageJobLauncher;
use Yokai\Batch\Bridge\Symfony\Messenger\LaunchJobMessage;
use Yokai\Batch\Factory\JobExecutionFactory;
use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator;
use Yokai\Batch\JobExecution;
use Yokai\Batch\Storage\JobExecutionStorageInterface;

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

$jobLauncher = new DispatchMessageJobLauncher(
new JobExecutionFactory(),
new JobExecutionFactory(new UniqidJobExecutionIdGenerator()),
$storage->reveal(),
$messageBus->reveal()
);
Expand Down
15 changes: 14 additions & 1 deletion src/batch/src/Factory/JobExecutionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@

final class JobExecutionFactory
{
/**
* @var JobExecutionIdGeneratorInterface
*/
private JobExecutionIdGeneratorInterface $idGenerator;

/**
* @param JobExecutionIdGeneratorInterface $idGenerator
*/
public function __construct(JobExecutionIdGeneratorInterface $idGenerator)
{
$this->idGenerator = $idGenerator;
}

/**
* @param string $name
* @param array $configuration
Expand All @@ -17,7 +30,7 @@ final class JobExecutionFactory
*/
public function create(string $name, array $configuration = []): JobExecution
{
$configuration['_id'] = $configuration['_id'] ?? uniqid();
$configuration['_id'] = $configuration['_id'] ?? $this->idGenerator->generate();

return JobExecution::createRoot($configuration['_id'], $name, null, new JobParameters($configuration));
}
Expand Down
13 changes: 13 additions & 0 deletions src/batch/src/Factory/JobExecutionIdGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Yokai\Batch\Factory;

interface JobExecutionIdGeneratorInterface
{
/**
* @return string
*/
public function generate(): string;
}
16 changes: 16 additions & 0 deletions src/batch/src/Factory/UniqidJobExecutionIdGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Yokai\Batch\Factory;

final class UniqidJobExecutionIdGenerator implements JobExecutionIdGeneratorInterface
{
/**
* @inheritdoc
*/
public function generate(): string
{
return \uniqid();
}
}
3 changes: 2 additions & 1 deletion src/batch/tests/Factory/JobExecutionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

use PHPUnit\Framework\TestCase;
use Yokai\Batch\Factory\JobExecutionFactory;
use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator;
use Yokai\Batch\JobExecution;

class JobExecutionFactoryTest extends TestCase
{
public function testCreate(): void
{
$executionFactory = new JobExecutionFactory();
$executionFactory = new JobExecutionFactory(new UniqidJobExecutionIdGenerator());

$executionWithoutConfig = $executionFactory->create('export');
self::assertSame('export', $executionWithoutConfig->getJobName());
Expand Down
7 changes: 4 additions & 3 deletions src/batch/tests/Launcher/SimpleJobLauncherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Container\ContainerInterface;
use Yokai\Batch\Factory\JobExecutionFactory;
use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator;
use Yokai\Batch\Job\JobInterface;
use Yokai\Batch\JobExecution;
use Yokai\Batch\Launcher\SimpleJobLauncher;
Expand Down Expand Up @@ -44,7 +45,7 @@ public function testLaunch(): void
$container->get('export')->willReturn($job->reveal());

$jobRegistry = new JobRegistry($container->reveal());
$jobExecutionFactory = new JobExecutionFactory();
$jobExecutionFactory = new JobExecutionFactory(new UniqidJobExecutionIdGenerator());
$jobExecutionStorage = $this->prophesize(JobExecutionStorageInterface::class);

$launcher = new SimpleJobLauncher($jobRegistry, $jobExecutionFactory, $jobExecutionStorage->reveal(), null);
Expand Down Expand Up @@ -73,7 +74,7 @@ public function testLaunchJobCatchException(): void
$container->get('export')->willReturn($job->reveal());

$jobRegistry = new JobRegistry($container->reveal());
$jobExecutionFactory = new JobExecutionFactory();
$jobExecutionFactory = new JobExecutionFactory(new UniqidJobExecutionIdGenerator());
$jobExecutionStorage = $this->prophesize(JobExecutionStorageInterface::class);

$launcher = new SimpleJobLauncher($jobRegistry, $jobExecutionFactory, $jobExecutionStorage->reveal(), null);
Expand All @@ -100,7 +101,7 @@ public function testLaunchJobCatchFatal(): void
$container->get('export')->willReturn($job->reveal());

$jobRegistry = new JobRegistry($container->reveal());
$jobExecutionFactory = new JobExecutionFactory();
$jobExecutionFactory = new JobExecutionFactory(new UniqidJobExecutionIdGenerator());
$jobExecutionStorage = $this->prophesize(JobExecutionStorageInterface::class);

$launcher = new SimpleJobLauncher($jobRegistry, $jobExecutionFactory, $jobExecutionStorage->reveal(), null);
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/JobTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Psr\Container\ContainerInterface;
use Yokai\Batch\BatchStatus;
use Yokai\Batch\Factory\JobExecutionFactory;
use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator;
use Yokai\Batch\Failure;
use Yokai\Batch\Job\JobInterface;
use Yokai\Batch\JobExecution;
Expand Down Expand Up @@ -52,7 +53,7 @@ public function testLaunchJob(JobExecutionStorageInterface $jobExecutionStorage)

$launcher = new SimpleJobLauncher(
self::createJobRegistry([$jobName => $job]),
new JobExecutionFactory(),
new JobExecutionFactory(new UniqidJobExecutionIdGenerator()),
$jobExecutionStorage,
null
);
Expand Down