-
Notifications
You must be signed in to change notification settings - Fork 3
Dev/batched messeages #932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jvangestel
wants to merge
33
commits into
2.x
Choose a base branch
from
dev/batched-messeages
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
5aec4cf
Add Batchable messages
jvangestel 0c6cab4
Allow selects in insertIntoTable in resultFetcher
jvangestel 2f8261f
Add uid requirement
jvangestel 85fb345
Fixes in Database Batch Store
jvangestel 30c9660
Add table
jvangestel b6a6ac5
Multiple messages
jvangestel 1a743d9
Add results, info messages, fix adding batch stamp to chain messages,…
jvangestel 177ff2b
tinyint as bool should be 1 char
jvangestel e39314a
Add batch unit tests
jvangestel 0543997
finished can be null
jvangestel 2b51eb2
whole batch is readonly (except for messages)
jvangestel 329f28e
Add option to clear messages, after dispatch
jvangestel 425161a
fix isPending, isRunning and finished iterations
jvangestel dcb16e6
Clear current messages after batch
jvangestel 9d842a2
Add more unit tests
jvangestel 16fa129
Message bus middleware cannot inject messagebus directly on creation
jvangestel a10114f
Add batch middleware to messagebus
jvangestel 5d59a48
Add batch middleware test
jvangestel d56204a
Add Batch Store to aliases in config
jvangestel aaffa2b
Fix DI loop for BatchMiddleware
jvangestel 766a786
Save isChain
jvangestel 1af3b8a
BatchStore is an alias, not a factory setting
jvangestel 208ab2b
Fix middleware now only using container in tests
jvangestel 7fd2b89
phpstan fix
jvangestel a8b009b
fix mocked container
jvangestel 72d9b83
try to annotate captured messages in test
jvangestel 7ab7f92
Add time to the commjob batch name
jvangestel 70acede
track batch item start time as well
jvangestel f7362c1
Add progress items to batch, fail whole chain, and some other fixes
jvangestel 4969fc1
Add a command to clear messenger batches that have expired
jvangestel 0ab6393
Add clearOldBatches to batch store
jvangestel 62968ae
Add percent to batch
jvangestel 4775dc5
Add a batch runner for messenger batches
jvangestel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
|
||
| return 0; | ||
| } | ||
| return $this->success / $this->totalItems; | ||
| } | ||
|
|
||
| public function clearMessages(): void | ||
| { | ||
| $this->messages = []; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| $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; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.