tourze / async-command-bundle
异步执行Command
Installs: 7 004
Dependents: 5
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/async-command-bundle
Requires
- monolog/monolog: ^3.1
- psr/log: ^3|^2|^1
- symfony/config: ^7.3
- symfony/console: ^7.3
- symfony/dependency-injection: ^7.3
- symfony/framework-bundle: ^7.3
- symfony/http-kernel: ^7.3
- symfony/messenger: ^7.3
- symfony/property-access: ^7.3
- symfony/yaml: ^7.3
- tourze/async-contracts: 0.0.*
- tourze/bundle-dependency: 1.*
- tourze/symfony-dependency-service-loader: 1.*
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
- symfony/phpunit-bridge: ^7.3
- tourze/phpunit-symfony-kernel-test: 1.0.*
- tourze/phpunit-symfony-unit-test: 1.*
This package is auto-updated.
Last update: 2025-11-01 19:09:47 UTC
README
A Symfony bundle for executing console commands asynchronously using Symfony Messenger.
Features
- Execute Symfony console commands asynchronously
- Built-in error handling and logging
- Support for command options and arguments
- Seamless integration with Symfony Messenger
Dependencies
- PHP 8.1 or higher
- Symfony 6.4 or higher
- Symfony Messenger component
- Monolog for logging
Installation
Add this bundle to your Symfony project:
composer require tourze/async-command-bundle
Quick Start
1. Enable the bundle
Add the bundle to your config/bundles.php:
return [ // ... other bundles Tourze\AsyncCommandBundle\AsyncCommandBundle::class => ['all' => true], ];
2. Configure Messenger
Configure Symfony Messenger to handle the async command messages:
# config/packages/messenger.yaml framework: messenger: transports: async: dsn: 'doctrine://default' routing: 'Tourze\AsyncCommandBundle\Message\RunCommandMessage': async
3. Dispatch commands asynchronously
use Symfony\Component\Messenger\MessageBusInterface; use Tourze\AsyncCommandBundle\Message\RunCommandMessage; class SomeService { public function __construct( private MessageBusInterface $messageBus ) { } public function scheduleCommand(): void { $message = new RunCommandMessage(); $message->setCommand('app:some-command'); $message->setOptions([ '--env' => 'prod', '--force' => true, 'argument1' => 'value1' ]); $this->messageBus->dispatch($message); } }
Configuration
The bundle works out of the box with minimal configuration. The command handler will:
- Log command execution start and completion
- Handle exceptions gracefully
- Support command retries through Symfony Messenger's retry mechanism
Advanced Usage
Using the AsyncCommandService
For a more convenient way to dispatch commands, you can use the AsyncCommandService:
use Tourze\AsyncCommandBundle\Service\AsyncCommandService; class MyService { public function __construct( private AsyncCommandService $asyncCommandService ) { } public function scheduleCommand(): void { $this->asyncCommandService->runCommand('app:some-command', [ '--env' => 'prod', '--force' => true, 'argument1' => 'value1' ]); } }
Error Handling and Retries
Configure retry behavior in your messenger configuration:
# config/packages/messenger.yaml framework: messenger: transports: async: dsn: 'doctrine://default' retry_strategy: max_retries: 3 delay: 1000 multiplier: 2 routing: 'Tourze\AsyncCommandBundle\Message\RunCommandMessage': async
Logging Configuration
The bundle uses Monolog for logging. You can configure the logger channel:
# config/packages/monolog.yaml monolog: channels: ['async_command'] handlers: async_command: type: stream path: "%kernel.logs_dir%/async_command.log" level: info channels: ['async_command']
API Reference
RunCommandMessage
The message class used to dispatch command execution:
class RunCommandMessage implements AsyncMessageInterface { public function getCommand(): string; public function setCommand(string $command): void; public function getOptions(): array; public function setOptions(array $options): void; }
RunCommandHandler
The message handler that executes commands:
- Decorated with
#[AsMessageHandler]attribute - Accepts optional
LoggerInterfacefor logging - Handles both recoverable and non-recoverable exceptions
Error Handling
The bundle provides robust error handling:
- Recoverable exceptions: Re-thrown to allow Messenger retry mechanism
- Non-recoverable exceptions: Logged as errors but do not cause message failure
- Logging: All command executions are logged with context information
Testing
Run the test suite:
./vendor/bin/phpunit packages/async-command-bundle/tests
Run PHPStan static analysis:
./vendor/bin/phpstan analyse packages/async-command-bundle
Contributing
Please see CONTRIBUTING.md for details on how to contribute to this project.
Development Guidelines
- Follow PSR-12 coding standards
- Write unit tests for new features
- Ensure all tests pass
- Update documentation as needed
Changelog
Please see CHANGELOG.md for details on version history and changes.
License
The MIT License (MIT). Please see License File for more information.