tourze / json-rpc-async-bundle
JsonRPC异步实现
Installs: 35
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/json-rpc-async-bundle
Requires
- php: ^8.1
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- doctrine/persistence: ^3.1 || ^4
- monolog/monolog: ^3.1
- psr/cache: ^2.0 || ^3.0
- psr/log: ^3|^2|^1
- psr/simple-cache: ^1.0|^2.0|^3.0
- symfony/cache: ^6.4
- symfony/cache-contracts: ^3
- symfony/config: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/doctrine-bridge: ^6.4
- symfony/event-dispatcher: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-kernel: ^6.4
- symfony/messenger: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/async-contracts: 0.0.*
- tourze/doctrine-helper: 0.0.*
- tourze/doctrine-indexed-bundle: 0.0.*
- tourze/doctrine-snowflake-bundle: 0.1.*
- tourze/doctrine-timestamp-bundle: 0.0.*
- tourze/json-rpc-core: 0.0.*
- tourze/json-rpc-endpoint-bundle: 0.1.*
- tourze/symfony-schedule-entity-clean-bundle: 0.1.*
- tourze/symfony-snowflake-bundle: 0.0.*
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
README
A Symfony bundle for handling asynchronous JSON-RPC requests and results, allowing heavy or long-running JSON-RPC calls to be processed asynchronously and their results to be queried later.
Table of Contents
Features
- Asynchronous execution for JSON-RPC methods
- Task ID based result query
- Result persistence with Doctrine ORM
- Cache-first result retrieval
- Integration with Symfony Messenger and Snowflake ID
- Automatic cleanup for expired results
Installation
Requirements
- PHP >= 8.1
- Symfony >= 6.4
- Doctrine ORM >= 3.0
- Symfony Messenger for async task processing
Install via Composer
composer require tourze/json-rpc-async-bundle
Enable the Bundle
Register the bundle in your config/bundles.php if not auto-registered:
return [ Tourze\JsonRPCAsyncBundle\JsonRPCAsyncBundle::class => ['all' => true], ];
Database Setup
Run the database migrations to create the required tables:
php bin/console doctrine:migrations:migrate
Configure Symfony Messenger
Ensure your config/packages/messenger.yaml has async transport configured:
framework: messenger: transports: async: '%env(MESSENGER_TRANSPORT_DSN)%' routing: 'Tourze\JsonRPCAsyncBundle\Message\AsyncProcedureMessage': async
Quick Start
- Mark a JSON-RPC method as async using the
AsyncExecuteattribute. - Call the method from client with a normal JSON-RPC request. If the environment is production and the request has a valid ID, the method is executed asynchronously.
- Receive a taskId in the error response (
code: -799), indicating the async task has started. - Query the result by calling the
GetAsyncRequestResultprocedure with the returnedtaskId.
Example
1. Marking a method async
use Tourze\JsonRPCAsyncBundle\Attribute\AsyncExecute; #[AsyncExecute] class MyAsyncProcedure extends BaseProcedure { ... }
2. Client requests async method
{
"jsonrpc": "2.0",
"method": "myAsyncMethod",
"params": { ... },
"id": "async_123456"
}
3. Receive async response
{
"jsonrpc": "2.0",
"id": "async_123456",
"error": {
"code": -799,
"message": "Async executing",
"data": { "taskId": "..." }
}
}
4. Query result
{
"jsonrpc": "2.0",
"method": "GetAsyncRequestResult",
"params": { "taskId": "..." },
"id": "query_1"
}
Advanced Usage
Custom Configuration
You can customize the async result persistence duration:
# config/packages/framework.yaml parameters: env(ASYNC_RESULT_PERSIST_DAY_NUM): 7 # Keep results for 7 days
Custom Error Handling
Handle async errors in your client application:
if ($response['error']['code'] === -799) { $taskId = $response['error']['data']['taskId']; // Store taskId for later query }
Cache Configuration
Optimize cache settings for better performance:
# config/packages/cache.yaml framework: cache: pools: cache.app: adapter: cache.adapter.redis default_lifetime: 3600
Documentation
Async Flow
- Method Interception: Methods with
AsyncExecuteattribute are intercepted byAsyncExecuteSubscriber - Task Dispatch: A unique task ID is generated using Snowflake ID generator
- Async Processing: The task is dispatched to Symfony Messenger for async processing
- Result Storage: Results are persisted in database using
AsyncResultentity - Result Caching: Results are cached for faster retrieval
- Query Results: Use
GetAsyncRequestResultprocedure to query task results
Entities
- AsyncResult: Stores taskId, result content, and creation time
- Uses Snowflake ID for unique identification
- Includes automatic cleanup via schedule
- Cached with configurable TTL
Configuration
- Environment Variables:
ASYNC_RESULT_PERSIST_DAY_NUM: How many days to keep async results (default: 1)MESSENGER_TRANSPORT_DSN: Symfony Messenger transport configuration
Error Codes
-799: Task started successfully, checktaskIdin error data-789: Task not finished yet, try again later
Performance Considerations
- Results are cached to reduce database queries
- Automatic cleanup prevents database bloat
- Uses Symfony Messenger for reliable async processing
Contributing
We welcome contributions! Please follow these guidelines:
- Issues: Report bugs or request features via GitHub Issues
- Pull Requests: Fork the repository and create a pull request
- Code Style: Follow PSR-12 coding standards
- Tests: Ensure all tests pass with
./vendor/bin/phpunit - Static Analysis: Run
./vendor/bin/phpstan analyseto check for issues
Running Tests
# Run all tests ./vendor/bin/phpunit packages/json-rpc-async-bundle/tests # Run static analysis php -d memory_limit=2G ./vendor/bin/phpstan analyse packages/json-rpc-async-bundle/
License
The MIT License (MIT). Please see LICENSE for more information.
Changelog
See CHANGELOG for version history and breaking changes.