connectholland / tactician-prioritycommand-plugin
Tactician plugin that allows adding a priority to a command which influences when and in what order commands will be executed
Installs: 13 329
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- league/tactician: ^0.6 | ^1.0
Requires (Dev)
- phpunit/phpunit: ^4.7
- satooshi/php-coveralls: ^0.6.1
- squizlabs/php_codesniffer: ^2.3
- symfony/event-dispatcher: ^2.7
This package is auto-updated.
Last update: 2023-05-29 00:49:41 UTC
README
Tactician plugin that allows adding a priority to a command which influences when and in what order commands will be executed
Concept
The plugin adds Middleware that allows you to prioritize your commands. To give priority to a command let it extend from one of the command implementations in this library:
Default supported command types:
- AbstractRequestCommand (bus, you may take a detour, but you may not start a new round)
- AbstractSequenceCommand (bus, you may take a detour, but nobody gets off before I do)
- AbstractUrgentCommand (take me to my destination asap)
You can create an interface to your event dispatcher (one for symfony ships with this library). You should attach the PriorityMiddleware::REQUEST queue to some event you always dispatch, preferably after output is sent to your users.
Suggested priorities
Obviously you're free to give your commands any priority you like, but these guidelines may help:
- Urgent: anything that affects the output you send to your user OR anything that affects the behavior of incoming request
- Request: anything that affects any subsequent requests from your user
- Sequence: anything that affects the behavior of following commands (i.e. because it sets an id in your database something else depends on)
Issues
Commands are not executed immediately which makes it impossible to return values. If you rely on the return value of a command, you can't make that command a priority command without breaking your application. The fix for this is to add an event listener before putting the command on the bus and dispatching an event from the handler which passes the return value. Example, code with return value
$result = $commandbus->handle($command);
echo "The result is.... {$result}";
Without the return value:
$eventdispatcher->addListener('when_my_commandhandler_is_done', function($event) {
echo "The result is.... {$event->getReturnValue()}";
});
$result = $commandbus->handle($priorityCommand);