-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
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,88 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger; | ||
|
||
use Amp\Cache\LocalCache; | ||
use Amp\Cancellation; | ||
use Amp\Parallel\Worker\Task; | ||
use Amp\Sync\Channel; | ||
use App\Kernel; | ||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\Dotenv\Dotenv; | ||
use Symfony\Component\Messenger\Stamp\AckStamp; | ||
|
||
class DispatchTask implements Task | ||
{ | ||
private static ?LocalCache $cache = null; | ||
|
||
public function __construct( | ||
private Envelope $envelope, | ||
private array $stamps, | ||
private readonly string $env, | ||
private readonly bool $isDebug, | ||
private readonly string $projectDir, | ||
) { | ||
if (!class_exists(LocalCache::class)) { | ||
throw new \LogicException(\sprintf('Package "amp/cache" is required to use the "%s". Try running "composer require amphp/cache".', LocalCache::class)); | ||
} | ||
} | ||
|
||
public function run(Channel $channel, Cancellation $cancellation): mixed | ||
{ | ||
$container = $this->getContainer(); | ||
$envelope = $this->dispatch($container, $channel); | ||
|
||
return $envelope->withoutStampsOfType(AckStamp::class); | ||
} | ||
|
||
private function dispatch(ContainerInterface $container, $channel) | ||
{ | ||
$messageBus = $container->get(MessageBusInterface::class); | ||
|
||
return $messageBus->dispatch($this->envelope, $this->stamps); | ||
} | ||
|
||
private function getContainer() | ||
{ | ||
$cache = self::$cache ??= new LocalCache(); | ||
$container = $cache->get('cache-container'); | ||
|
||
// if not in cache, create container | ||
if (!$container) { | ||
if (!method_exists(Dotenv::class, 'bootEnv')) { | ||
throw new \LogicException(\sprintf("Method bootEnv de \"%s\" doesn't exist.", Dotenv::class)); | ||
} | ||
|
||
(new Dotenv())->bootEnv($this->projectDir.'/.env'); | ||
|
||
if (!class_exists(Kernel::class) && !isset($_ENV['KERNEL_CLASS'])) { | ||
throw new \LogicException('Yo F438 u must set the KERNEL_CLASS environment variable to the fully-qualified class name of your Kernel in .env or have "%s" class.', Kernel::class); | ||
} elseif (isset($_ENV['KERNEL_CLASS'])) { | ||
$kernel = new $_ENV['KERNEL_CLASS']($this->env, $this->isDebug); | ||
} else { | ||
$kernel = new Kernel($this->env, $this->isDebug); | ||
} | ||
|
||
$kernel->boot(); | ||
|
||
$container = $kernel->getContainer(); | ||
$cache->set('cache-container', $container); | ||
} | ||
|
||
return $container; | ||
} | ||
|
||
public function getEnvelope(): Envelope | ||
{ | ||
return $this->envelope; | ||
} | ||
} |
156 changes: 156 additions & 0 deletions
156
src/Symfony/Component/Messenger/Handler/BatchAsyncHandlerTrait.php
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,156 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Handler; | ||
|
||
use Amp\Future; | ||
use Symfony\Component\Messenger\Stamp\FutureStamp; | ||
use Symfony\Component\Messenger\ParallelMessageBus; | ||
use Symfony\Component\Messenger\Envelope; | ||
|
||
/** | ||
* A batch handler trait designed for parallel execution using ParallelMessageBus. | ||
* | ||
* This trait collects jobs in worker-specific batches and processes them | ||
* in parallel by dispatching each job individually through ParallelMessageBus. | ||
*/ | ||
trait BatchAsyncHandlerTrait | ||
{ | ||
/** @var array<string,array> Map of worker IDs to their job batches */ | ||
private array $workerJobs = []; | ||
|
||
/** @var ParallelMessageBus|null */ | ||
private ?ParallelMessageBus $parallelBus = null; | ||
|
||
/** | ||
* Set the parallel message bus to use for dispatching jobs. | ||
*/ | ||
public function setParallelMessageBus(ParallelMessageBus $bus): void | ||
{ | ||
$this->parallelBus = $bus; | ||
} | ||
|
||
public function flush(bool $force): void | ||
{ | ||
$workerId = $this->getCurrentWorkerId(); | ||
|
||
if (isset($this->workerJobs[$workerId]) && $jobs = $this->workerJobs[$workerId]) { | ||
$this->workerJobs[$workerId] = []; | ||
|
||
if ($this->parallelBus) { | ||
// Process each job in parallel using ParallelMessageBus | ||
$futures = []; | ||
|
||
foreach ($jobs as [$message, $ack]) { | ||
// Dispatch each message individually | ||
$envelope = $this->parallelBus->dispatch($message); | ||
|
||
$futureStamp = $envelope->last(FutureStamp::class); | ||
if ($futureStamp) { | ||
/** @var Future $future */ | ||
$future = $futureStamp->getFuture(); | ||
$futures[] = [$future, $ack]; | ||
} | ||
} | ||
|
||
// If force is true, wait for all results | ||
if ($force && $futures) { | ||
foreach ($futures as [$future, $ack]) { | ||
try { | ||
$result = $future->await(); | ||
$ack->ack($result); | ||
} catch (\Throwable $e) { | ||
$ack->nack($e); | ||
10000 } | ||
} | ||
} | ||
} else { | ||
// Fallback to synchronous processing | ||
$this->process($jobs); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param Acknowledger|null $ack The function to call to ack/nack the $message. | ||
* | ||
* @return mixed The number of pending messages in the batch if $ack is not null, | ||
* the result from handling the message otherwise | ||
*/ | ||
private function handle(object $message, ?Acknowledger $ack): mixed | ||
{ | ||
$workerId = $this->getCurrentWorkerId(); | ||
|
||
if (!isset($this->workerJobs[$workerId])) { | ||
$this->workerJobs[$workerId] = []; | ||
} | ||
|
||
if (null === $ack) { | ||
$ack = new Acknowledger(get_debug_type($this)); | ||
$this->workerJobs[$workerId][] = [$message, $ack]; | ||
$this->flush(true); | ||
|
||
return $ack->getResult(); | ||
} | ||
|
||
$this->workerJobs[$workerId][] = [$message, $ack]; | ||
if (!$this->shouldFlush()) { | ||
return \count($this->workerJobs[$workerId]); | ||
} | ||
|
||
$this->flush(true); | ||
|
||
return 0; | ||
} | ||
|
||
private function shouldFlush(): bool | ||
{ | ||
$workerId = $this->getCurrentWorkerId(); | ||
return $this->getBatchSize() <= \count($this->workerJobs[$workerId] ?? []); | ||
} | ||
|
||
/** | ||
* Generates a unique identifier for the current worker context. | ||
*/ | ||
private function getCurrentWorkerId(): string | ||
{ | ||
// In a worker pool, each worker has a unique ID | ||
return getmypid() ?: 'default-worker'; | ||
} | ||
|
||
/** | ||
* Cleans up worker-specific resources when a worker completes its job. | ||
*/ | ||
public function cleanupWorker(): void | ||
{ | ||
$workerId = $this->getCurrentWorkerId(); | ||
|
||
// Flush any remaining jobs before cleaning up | ||
if (isset($this->workerJobs[$workerId]) && !empty($this->workerJobs[$workerId])) { | ||
$this->flush(true); | ||
} | ||
|
||
unset($this->workerJobs[$workerId]); | ||
} | ||
|
||
/** | ||
* Completes the jobs in the list. | ||
* This is used as a fallback when ParallelMessageBus is not available. | ||
* | ||
* @param list<array{0: object, 1: Acknowledger}> $jobs A list of pairs of messages and their corresponding acknowledgers | ||
*/ | ||
abstract private function process(array $jobs): void; | ||
|
||
private function getBatchSize(): int | ||
{ | ||
return 10; | ||
} | ||
} |
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
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.
[Messenger] Add BatchAsyncHandlerTrait for ParallelMessageBus #60080
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
base: 7.4
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
[Messenger] Add BatchAsyncHandlerTrait for ParallelMessageBus #60080
Changes from all commits
522a316
29b4e97
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.