tourze / json-rpc-async-bundle
JsonRPC异步实现
Installs: 28
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.1
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^2.20 || ^3.0
- doctrine/persistence: ^3.1 || ^4
- psr/log: ^3|^2|^1
- psr/simple-cache: ^1.0|^2.0|^3.0
- symfony/cache-contracts: ^3
- symfony/config: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-kernel: ^6.4
- symfony/messenger: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/doctrine-helper: 0.0.*
- tourze/doctrine-indexed-bundle: 0.0.*
- tourze/doctrine-snowflake-bundle: 0.0.*
- tourze/doctrine-timestamp-bundle: 0.0.*
- tourze/json-rpc-core: ^0.0.6
- tourze/json-rpc-endpoint-bundle: 0.0.*
- tourze/symfony-async-bundle: 0.0.*
- 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.
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 >= 2.20
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], ];
Quick Start
- Mark a JSON-RPC method as async using the
AsyncExecute
attribute. - 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
GetAsyncRequestResult
procedure 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" }
Documentation
- Async Flow:
- Methods with
AsyncExecute
attribute are intercepted and dispatched as async tasks. - Results are persisted in DB and cached.
- Query via
GetAsyncRequestResult
procedure.
- Methods with
- Entities:
AsyncResult
: Stores taskId, result, and creation time.
- Configuration:
- No special configuration required by default.
- Error Codes:
-799
: Task started, checktaskId
in error data.-789
: Task not finished.
Contribution
Feel free to submit issues or pull requests. Please follow PSR code style and ensure tests pass.
License
MIT License. See LICENSE.
Changelog
See CHANGELOG for details.