tourze/json-rpc-async-bundle

JsonRPC异步实现

0.1.3 2025-04-28 17:25 UTC

This package is auto-updated.

Last update: 2025-05-09 16:08:27 UTC


README

English | 中文

Latest Version Build Status Quality Score Total Downloads

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

  1. Mark a JSON-RPC method as async using the AsyncExecute attribute.
  2. 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.
  3. Receive a taskId in the error response (code: -799), indicating the async task has started.
  4. Query the result by calling the GetAsyncRequestResult procedure with the returned taskId.

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.
  • Entities:
    • AsyncResult: Stores taskId, result, and creation time.
  • Configuration:
    • No special configuration required by default.
  • Error Codes:
    • -799: Task started, check taskId 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.