-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from darkwood-com/batch-ip
✨ Add Batch IP
- Loading branch information
Showing
30 changed files
with
2,846 additions
and
1,788 deletions.
There are no files selected for viewing
This file contains 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 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 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,30 @@ | ||
--- | ||
title: "Async Handler" | ||
description: "Async Handler." | ||
lead: "Async Handler." | ||
date: 2020-10-13T15:21:01+02:00 | ||
lastmod: 2020-10-13T15:21:01+02:00 | ||
draft: false | ||
images: [] | ||
menu: | ||
docs: | ||
parent: "getting-started" | ||
weight: 35 | ||
toc: true | ||
--- | ||
|
||
# Async Handler | ||
|
||
When processing Flow at async step, you can choose a handler that will process asynchronously the Ip. | ||
|
||
## AsyncHandler | ||
|
||
This is the default one. Ip is async processed immediately. | ||
|
||
## BatchAsyncHandler | ||
|
||
This async process Ip as batch capability : the handler will wait for a certain amount of async messages ($batchSize) to be processed before pushing them. | ||
|
||
## Make your Async Handler | ||
|
||
You can make your custom Ip strategy by implementing `Flow\AsyncHandlerInterface` |
This file contains 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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\AsyncHandler; | ||
|
||
use Flow\AsyncHandlerInterface; | ||
use Flow\Event; | ||
use Flow\Event\AsyncEvent; | ||
|
||
use function call_user_func_array; | ||
|
||
final class AsyncHandler implements AsyncHandlerInterface | ||
{ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
Event::ASYNC => 'async', | ||
]; | ||
} | ||
|
||
public function async(AsyncEvent $event): void | ||
{ | ||
call_user_func_array($event->getAsync(), $event->getArgs()); | ||
} | ||
} |
This file contains 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,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\AsyncHandler; | ||
|
||
use Flow\AsyncHandlerInterface; | ||
use Flow\Event; | ||
use Flow\Event\AsyncEvent; | ||
use Symfony\Component\Messenger\Handler\Acknowledger; | ||
use Symfony\Component\Messenger\Handler\BatchHandlerInterface; | ||
use Symfony\Component\Messenger\Handler\BatchHandlerTrait; | ||
use Throwable; | ||
|
||
use function call_user_func_array; | ||
|
||
final class BatchAsyncHandler implements BatchHandlerInterface, AsyncHandlerInterface | ||
{ | ||
use BatchHandlerTrait; | ||
|
||
public function __construct( | ||
private int $batchSize = 10, | ||
) {} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
Event::ASYNC => 'async', | ||
]; | ||
} | ||
|
||
public function async(AsyncEvent $event): void | ||
{ | ||
$ack = new Acknowledger(get_debug_type($this), static function (?Throwable $e = null, $event = null) { | ||
call_user_func_array($event->getAsync(), $event->getArgs()); | ||
}); | ||
|
||
$this->handle($event, $ack); | ||
} | ||
|
||
/** | ||
* PHPStan should normaly pass for method.unused | ||
* https://github.com/phpstan/phpstan/issues/6039 | ||
* https://phpstan.org/r/8f7de023-9888-4dcb-b12c-e2fcf9547b6c. | ||
* | ||
* @param array{0: AsyncEvent, 1: Acknowledger}[] $jobs | ||
* | ||
* @phpstan-ignore method.unused | ||
*/ | ||
private function process(array $jobs): void | ||
{ | ||
foreach ($jobs as [$event, $ack]) { | ||
$ack->ack($event); | ||
} | ||
} | ||
|
||
/** | ||
* PHPStan should normaly pass for method.unused | ||
* https://github.com/phpstan/phpstan/issues/6039 | ||
* https://phpstan.org/r/8f7de023-9888-4dcb-b12c-e2fcf9547b6c. | ||
* | ||
* @phpstan-ignore method.unused | ||
*/ | ||
private function getBatchSize(): int | ||
{ | ||
return $this->batchSize; | ||
} | ||
} |
This file contains 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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
interface AsyncHandlerInterface extends EventSubscriberInterface {} |
This file contains 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 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.