Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5aec4cf
Add Batchable messages
jvangestel Sep 16, 2025
0c6cab4
Allow selects in insertIntoTable in resultFetcher
jvangestel Sep 16, 2025
2f8261f
Add uid requirement
jvangestel Sep 17, 2025
85fb345
Fixes in Database Batch Store
jvangestel Sep 17, 2025
30c9660
Add table
jvangestel Sep 29, 2025
b6a6ac5
Multiple messages
jvangestel Sep 29, 2025
1a743d9
Add results, info messages, fix adding batch stamp to chain messages,…
jvangestel Sep 29, 2025
177ff2b
tinyint as bool should be 1 char
jvangestel Sep 29, 2025
e39314a
Add batch unit tests
jvangestel Sep 29, 2025
0543997
finished can be null
jvangestel Sep 29, 2025
2b51eb2
whole batch is readonly (except for messages)
jvangestel Sep 29, 2025
329f28e
Add option to clear messages, after dispatch
jvangestel Sep 29, 2025
425161a
fix isPending, isRunning and finished iterations
jvangestel Sep 29, 2025
dcb16e6
Clear current messages after batch
jvangestel Sep 29, 2025
9d842a2
Add more unit tests
jvangestel Sep 29, 2025
16fa129
Message bus middleware cannot inject messagebus directly on creation
jvangestel Sep 29, 2025
a10114f
Add batch middleware to messagebus
jvangestel Sep 29, 2025
5d59a48
Add batch middleware test
jvangestel Sep 29, 2025
d56204a
Add Batch Store to aliases in config
jvangestel Sep 29, 2025
aaffa2b
Fix DI loop for BatchMiddleware
jvangestel Sep 29, 2025
766a786
Save isChain
jvangestel Sep 29, 2025
1af3b8a
BatchStore is an alias, not a factory setting
jvangestel Sep 29, 2025
208ab2b
Fix middleware now only using container in tests
jvangestel Oct 1, 2025
7fd2b89
phpstan fix
jvangestel Oct 1, 2025
a8b009b
fix mocked container
jvangestel Oct 1, 2025
72d9b83
try to annotate captured messages in test
jvangestel Oct 1, 2025
7ab7f92
Add time to the commjob batch name
jvangestel Oct 1, 2025
70acede
track batch item start time as well
jvangestel Oct 1, 2025
f7362c1
Add progress items to batch, fail whole chain, and some other fixes
jvangestel Nov 11, 2025
4969fc1
Add a command to clear messenger batches that have expired
jvangestel Dec 2, 2025
0ab6393
Add clearOldBatches to batch store
jvangestel Dec 2, 2025
62968ae
Add percent to batch
jvangestel Dec 2, 2025
4775dc5
Add a batch runner for messenger batches
jvangestel Dec 2, 2025
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"symfony/serializer": "^6.4",
"symfony/string": "^6.4",
"symfony/translation": "^6.4",
"symfony/uid": "^6.4",
"symfony/validator": "^6.4",
"symfony/yaml": "^6.4",
"twig/twig": "^3.9"
Expand Down
24 changes: 24 additions & 0 deletions configs/db/tables/gems__batch.20.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CREATE TABLE if not exists gems__batch (
gba_id char(36) not null,
gba_iteration int unsigned not null,

gba_name varchar(64) null,
gba_group varchar(64) null,
gba_status varchar(64) not null,
gba_synchronous tinyint(1) not null default 0,

gba_message JSON null,
gba_message_class varchar(128) null,

gba_info text null,

gba_started timestamp null,
gba_finished timestamp null,
gba_created timestamp not null default current_timestamp,
gba_changed timestamp not null default current_timestamp ON UPDATE current_timestamp,

PRIMARY KEY (gba_id, gba_iteration)
)
ENGINE=InnoDB
AUTO_INCREMENT = 1000
CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';
70 changes: 70 additions & 0 deletions src/Command/ClearMessengerBatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Gems\Command;

use DateInterval;
use Exception;
use Gems\Messenger\Batch\BatchStoreInterface;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'app:clear-messenger-batch', description: 'Clear old messenger batch data')]
class ClearMessengerBatch extends Command
{

public function __construct(
private readonly ContainerInterface $container,
)
{
parent::__construct();
}

protected function configure()
{
$this->addArgument(
name: 'days',
mode: InputArgument::OPTIONAL,
description: 'Age in days of items to remove. Can also be a DateInterval notation (e.g. P7D)',
default: '7',
);

$this->addArgument(
name: 'group',
mode: InputArgument::OPTIONAL,
description: 'Optional Group to clear',
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$removeInterval = $this->getInterval($input);

/** @var BatchStoreInterface $batchStore */
$batchStore = $this->container->get(BatchStoreInterface::class);

$batchStore->clearOldBatches($removeInterval);

return Command::SUCCESS;
}

protected function getInterval(InputInterface $input): DateInterval
{
$days = $input->getArgument('days');
if (ctype_digit($days)) {
return new DateInterval('P' . (int)$days . 'D');
}

try {
$interval = new DateInterval($days);
return $interval;
} catch (Exception $e) {
throw new Exception(sprintf('Input days (%s) is not an int or a DateInterval string', $days));
}
}
}
60 changes: 41 additions & 19 deletions src/Command/RunCommJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Gems\Command;

use Gems\Console\ConsoleSettings;
use Gems\Messenger\Batch\MessengerBatchRepository;
use Gems\Messenger\Message\CommJob;
use Gems\Repository\CommJobRepository;
use Gems\Util\Lock\CommJobLock;
use Gems\Util\Lock\MaintenanceLock;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -19,12 +21,7 @@
class RunCommJob extends Command
{
public function __construct(
protected CommJobLock $commJobLock,
protected MaintenanceLock $maintenanceLock,
protected MessageBusInterface $messageBus,
protected CommJobRepository $commJobRepository,
protected ConsoleSettings $consoleSettings,
protected array $config,
private readonly ContainerInterface $container,
)
{
parent::__construct();
Expand All @@ -37,22 +34,38 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output)
{
if ($this->maintenanceLock->isLocked()) {
/**
* @var MaintenanceLock $maintenanceLock
*/
$maintenanceLock = $this->container->get(MaintenanceLock::class);

if ($maintenanceLock->isLocked()) {
$output->writeln('<error>Cannot run cron job in maintenance mode.</error>');
return static::FAILURE;
}
if ($this->commJobLock->isLocked()) {

/**
* @var CommJobLock $commJobLock
*/
$commJobLock = $this->container->get(CommJobLock::class);
if ($commJobLock->isLocked()) {
$output->writeln('<error>Cron jobs turned off.</error>');
return static::FAILURE;
}

$this->consoleSettings->setConsoleUser();
/**
* @var ConsoleSettings $consoleSettings
*/
$consoleSettings = $this->container->get(ConsoleSettings::class);
$consoleSettings->setConsoleUser();

$jobs = $this->commJobRepository->getAutomaticJobs();
/** @var CommJobRepository $commJobRepository */
$commJobRepository = $this->container->get(CommJobRepository::class);
$jobs = $commJobRepository->getAutomaticJobs();

$id = $input->getArgument('id');
if ($id !== null) {
$jobs = $this->commJobRepository->getActiveJobs();
$jobs = $commJobRepository->getActiveJobs();
$jobs = array_filter($jobs, function ($job) use ($id) {
return $job['gcj_id_job'] == $id;
});
Expand All @@ -61,18 +74,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

$this->commJobRepository->clearTokenQueue();
$commJobRepository->clearTokenQueue();

/** @var MessengerBatchRepository $messengerBatchRepository */
$messengerBatchRepository = $this->container->get(MessengerBatchRepository::class);
$now = new \DateTimeImmutable();
$batch = $messengerBatchRepository->createBatch('CommJob ' . $now->format('Y-m-d H:i:s'), 'CommJob', true);
foreach($jobs as $jobData) {
$commJobMessage = new CommJob($jobData);
$envelope = $this->messageBus->dispatch($commJobMessage);
/**
* @var HandledStamp $stamp
*/
$stamp = $envelope->last(HandledStamp::class);
$output->writeln($stamp->getResult());
$batch->addMessage($commJobMessage);
}

$output->writeln(sprintf('<info>Executed %d jobs</info>', count($jobs)));
$messengerBatchRepository->dispatch($batch);

$output->writeln(sprintf('<info>Dispatched %d jobs</info>', count($jobs)));

/** @var MessengerBatchRepository $messengerBatchRepository */
$messengerBatchRepository = $this->container->get(MessengerBatchRepository::class);
$messages = $messengerBatchRepository->getBatchInfoList($batch->batchId);
foreach($messages as $message) {
$output->writeln(sprintf('<info>%s</info>', $message));
}

return static::SUCCESS;
}
Expand Down
3 changes: 3 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
use Gems\Menu\RouteHelperFactory;
use Gems\Messenger\MessengerFactory;
use Gems\Messenger\TransportFactory;
use Gems\Messenger\Batch\BatchStoreInterface;
use Gems\Messenger\Batch\DatabaseBatchStore;
use Gems\Middleware\FlashMessageMiddleware;
use Gems\Model\Bridge\GemsFormBridge;
use Gems\Model\Bridge\GemsValidatorBridge;
Expand Down Expand Up @@ -485,6 +487,7 @@ public function getDependencies(): array

// Messenger
'messenger.bus.default' => MessageBusInterface::class,
BatchStoreInterface::class => DatabaseBatchStore::class,

// Session
//SessionPersistenceInterface::class => CacheSessionPersistence::class,
Expand Down
2 changes: 1 addition & 1 deletion src/Db/ResultFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected function do_query(Select|string $select, ?array $params = null)
return $this->db->query($select, $params, $resultSet);
}

public function insertIntoTable(string $tableName, array $values): int
public function insertIntoTable(string $tableName, array|Select $values): int
{
$table = new TableGateway($tableName, $this->getAdapter());
$table->insert($values);
Expand Down
74 changes: 74 additions & 0 deletions src/Messenger/Batch/Batch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Gems\Messenger\Batch;

use DateTimeInterface;

class Batch
{
/*
* @var object[]
*/
private array $messages = [];

public function __construct(
public readonly string $batchId,
public readonly DateTimeInterface $created,
public readonly string|null $name = null,
public readonly string|null $group = null,
public readonly bool $isChain = false,
public readonly DateTimeInterface|null $finished = null,
public readonly int|null $totalItems = null,
public readonly int|null $pending = null,
public readonly int|null $running = null,
public readonly int|null $success = null,
public readonly int|null $failed = null,



)
{
}

public function addMessage(object $message): void
{
$this->messages[] = $message;
}

/**
* @param object[] $messages
* @return void
*/
public function addMessages(array $messages): void
{
foreach ($messages as $message) {
$this->addMessage($message);
}
}

/**
* @return object[]
*/
public function getCurrentMessages(): array
{
return $this->messages;
}

public function getPercent(): float
{
if (!$this->totalItems) {
return 0;
}
if (!$this->success || $this->success === 0) {

Check failure on line 64 in src/Messenger/Batch/Batch.php

View workflow job for this annotation

GitHub Actions / phpstan (prefer-stable)

Strict comparison using === between int<min, -1>|int<1, max> and 0 will always evaluate to false.

Check failure on line 64 in src/Messenger/Batch/Batch.php

View workflow job for this annotation

GitHub Actions / phpstan (prefer-lowest)

Strict comparison using === between int<min, -1>|int<1, max> and 0 will always evaluate to false.

Check failure on line 64 in src/Messenger/Batch/Batch.php

View workflow job for this annotation

GitHub Actions / phpstan (prefer-stable)

Strict comparison using === between int<min, -1>|int<1, max> and 0 will always evaluate to false.

Check failure on line 64 in src/Messenger/Batch/Batch.php

View workflow job for this annotation

GitHub Actions / phpstan (prefer-lowest)

Strict comparison using === between int<min, -1>|int<1, max> and 0 will always evaluate to false.
return 0;
}
return $this->success / $this->totalItems;
}

public function clearMessages(): void
{
$this->messages = [];
}
}
79 changes: 79 additions & 0 deletions src/Messenger/Batch/BatchMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Gems\Messenger\Batch;

use Psr\Container\ContainerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Stamp\HandledStamp;

class BatchMiddleware implements MiddlewareInterface
{
public function __construct(
private readonly ContainerInterface $container,
)
{
}

public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
/** @var BatchStamp|null $stamp */
$stamp = $envelope->last(BatchStamp::class);
if (!$stamp) {
return $stack->next()->handle($envelope, $stack);
}

/** @var MessengerBatchRepository $batchRepository */
$batchRepository = $this->container->get(MessengerBatchRepository::class);

try {
$batchRepository->setIterationStatus($stamp->batchId, $stamp->iteration, BatchStatus::RUNNING);

$envelope = $stack->next()->handle($envelope, $stack);
$handledStamp =$envelope->last(HandledStamp::class);
$result = $handledStamp?->getResult();

$message = null;
if (is_string($result)) {
$message = $result;
}

$batch = $batchRepository->getBatch($stamp->batchId);
$batchRepository->setIterationStatus($stamp->batchId, $stamp->iteration, BatchStatus::SUCCESS, $message);

if ($batch->isChain) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jvangestel Wat gebeurt er met een chained batch als de eerste uit de chain faalt? De eerste krijgt dan status failed, maar alle die daar na komen blijven staan op pending? Komen die later nog door? Worden ze ooit opgeruimd?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rvm-peercode Wanneer het gaat om een chain (een voor een) wordt de rest van de chain nu ook op failed gezet.

$nextMessage = $batchRepository->getBatchIterationMessage($stamp->batchId, $stamp->iteration + 1);
if ($nextMessage) {
/**
* @var MessageBusInterface $messageBus
*/
$messageBus = $this->container->get(MessageBusInterface::class);
$messageBus->dispatch($nextMessage, [
new BatchStamp($stamp->batchId, $stamp->iteration + 1),
]);
}
}


} catch (\Throwable $e) {
$batchRepository->setIterationStatus(
$stamp->batchId,
$stamp->iteration,
BatchStatus::FAILED,
$e->getMessage()
);
$batch = $batchRepository->getBatch($stamp->batchId);
if ($batch->isChain) {
$batchRepository->failChain($stamp->batchId, $stamp->iteration);
}

throw $e;
}

return $envelope;
}
}
Loading
Loading