Skip to content

Commit

Permalink
Refactored & factorized the way jobs are executed (#59)
Browse files Browse the repository at this point in the history
* Refactored & factorized the way jobs are executed

* Fixed PHP 8.0 array with keys unpack

* Fixed checkstyle

* Add missing JobContainer class comment

* Add test case to prove status can be changed by event listeners

* Fixed summary log array_merge

* Simplified integration tests jobs building

* Use PHP 8 promoted properties in JobContainer

* Fixed docs after refactoring
  • Loading branch information
yann-eugone authored Apr 12, 2022
1 parent 8372b79 commit 9460b82
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/LaunchJobMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
namespace Yokai\Batch\Bridge\Symfony\Messenger;

use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Yokai\Batch\Launcher\JobLauncherInterface;
use Yokai\Batch\Job\JobExecutionAccessor;
use Yokai\Batch\Job\JobExecutor;

/**
* Answer to {@see LaunchJobMessage} and launch requested job.
*/
final class LaunchJobMessageHandler implements MessageHandlerInterface
{
public function __construct(
private JobLauncherInterface $jobLauncher,
private JobExecutionAccessor $jobExecutionAccessor,
private JobExecutor $jobExecutor,
) {
}

public function __invoke(LaunchJobMessage $message): void
{
$this->jobLauncher->launch($message->getJobName(), $message->getConfiguration());
$execution = $this->jobExecutionAccessor->get($message->getJobName(), $message->getConfiguration());
$this->jobExecutor->execute($execution);
}
}
39 changes: 32 additions & 7 deletions tests/LaunchJobMessageHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,51 @@
namespace Yokai\Batch\Tests\Bridge\Symfony\Messenger;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Yokai\Batch\Bridge\Symfony\Messenger\LaunchJobMessage;
use Yokai\Batch\Bridge\Symfony\Messenger\LaunchJobMessageHandler;
use Yokai\Batch\Factory\JobExecutionFactory;
use Yokai\Batch\Job\JobExecutionAccessor;
use Yokai\Batch\Job\JobExecutor;
use Yokai\Batch\Job\JobInterface;
use Yokai\Batch\JobExecution;
use Yokai\Batch\Registry\JobRegistry;
use Yokai\Batch\Test\Factory\SequenceJobExecutionIdGenerator;
use Yokai\Batch\Test\Launcher\BufferingJobLauncher;
use Yokai\Batch\Test\Storage\InMemoryJobExecutionStorage;

final class LaunchJobMessageHandlerTest extends TestCase
{
use ProphecyTrait;

public function testInvoke(): void
{
$launcher = new BufferingJobLauncher(new SequenceJobExecutionIdGenerator(['123456']));
$job = new class implements JobInterface {
public JobExecution $execution;
public function execute(JobExecution $jobExecution): void
{
$this->execution = $jobExecution;
}
};

$handler = new LaunchJobMessageHandler($launcher);
$jobExecutionStorage = new InMemoryJobExecutionStorage();
$handler = new LaunchJobMessageHandler(
new JobExecutionAccessor(
new JobExecutionFactory(new SequenceJobExecutionIdGenerator(['123456'])),
$jobExecutionStorage,
),
new JobExecutor(
JobRegistry::fromJobArray(['foo' => $job]),
$jobExecutionStorage,
null
)
);
$handler->__invoke(new LaunchJobMessage('foo', ['bar' => 'BAR']));

self::assertCount(1, $launcher->getExecutions());
self::assertSame('foo', $launcher->getExecutions()[0]->getJobName());
self::assertSame('123456', $launcher->getExecutions()[0]->getId());
self::assertSame('foo', $job->execution->getJobName());
self::assertSame('123456', $job->execution->getId());
self::assertSame(
['bar' => 'BAR', '_id' => '123456'],
\iterator_to_array($launcher->getExecutions()[0]->getParameters()->getIterator())
$job->execution->getParameters()->all()
);
}
}

0 comments on commit 9460b82

Please sign in to comment.