|
| 1 | +# Asynchronous Processing |
| 2 | + |
| 3 | +This library supports the |
| 4 | +[JSON:API Asynchronous Processing](https://jsonapi.org/recommendations/#asynchronous-processing) |
| 5 | +recommendation, which provides a standardized way to handle long-running |
| 6 | +resource creation operations. |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +The async processing pattern works as follows: |
| 11 | + |
| 12 | +1. **Async Creation** (202 Accepted): Client creates a resource that takes time |
| 13 | + to process. Server returns a `202 Accepted` status with a `Content-Location` |
| 14 | + or `Location` header pointing to a job resource. |
| 15 | +2. **Job Polling** (200 OK): Client polls the job resource to check status. |
| 16 | + Server returns `200 OK` with optional `Retry-After` header. |
| 17 | +3. **Completion** (303 See Other): When processing completes, job resource |
| 18 | + returns `303 See Other` with a `Location` header pointing to the created |
| 19 | + resource. |
| 20 | + |
| 21 | +## Async Creation |
| 22 | + |
| 23 | +To enable async processing for resource creation, use the `async` method on the |
| 24 | +`Create` endpoint: |
| 25 | + |
| 26 | +```php |
| 27 | +use Tobyz\JsonApiServer\Endpoint\Create; |
| 28 | + |
| 29 | +class PhotosResource extends Resource |
| 30 | +{ |
| 31 | + public function endpoints(): array |
| 32 | + { |
| 33 | + return [ |
| 34 | + Create::make()->async('jobs', function ($model, Context $context) { |
| 35 | + if ($this->requiresAsyncProcessing($model)) { |
| 36 | + return Job::create([ |
| 37 | + 'status' => 'pending', |
| 38 | + 'resource_type' => 'photos', |
| 39 | + 'resource_data' => $model, |
| 40 | + ]); |
| 41 | + } |
| 42 | + }), |
| 43 | + ]; |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +The `async` method takes two parameters: |
| 49 | + |
| 50 | +- **Collection name**: The name of the collection in which the job resource will |
| 51 | + be found (e.g. `jobs`) |
| 52 | +- **Callback**: A function that receives the model and context, and returns: |
| 53 | + - A **job model object**: Will return a `202 Accepted` response containing |
| 54 | + the job resource, and a `Content-Location` header pointing to the job |
| 55 | + resource |
| 56 | + - A **string path**: Will return a `202 Accepted` response with the string |
| 57 | + as the `Location` |
| 58 | + - **null**: Falls back to synchronous processing with a normal `201 Created` |
| 59 | + response |
| 60 | + |
| 61 | +The callback is invoked after the data pipeline has validated and filled the |
| 62 | +model, but before it's persisted to storage. |
| 63 | + |
| 64 | +## Job Polling |
| 65 | + |
| 66 | +Use custom headers on your job resource's `Show` endpoint to provide polling |
| 67 | +guidance: |
| 68 | + |
| 69 | +```php |
| 70 | +use Tobyz\JsonApiServer\Endpoint\Show; |
| 71 | +use Tobyz\JsonApiServer\Schema\Header; |
| 72 | +use Tobyz\JsonApiServer\Schema\Type\Integer; |
| 73 | + |
| 74 | +class JobsResource extends Resource |
| 75 | +{ |
| 76 | + public function endpoints(): array |
| 77 | + { |
| 78 | + return [ |
| 79 | + Show::make()->headers([ |
| 80 | + Header::make('Retry-After') |
| 81 | + ->type(Integer::make()) |
| 82 | + ->nullable() |
| 83 | + ->get( |
| 84 | + fn($model) => $model->status === 'pending' ? 60 : null, |
| 85 | + ), |
| 86 | + ]), |
| 87 | + ]; |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## Completion with See Other |
| 93 | + |
| 94 | +When a job completes, use the `seeOther` convenience method to redirect clients |
| 95 | +to the created resource: |
| 96 | + |
| 97 | +```php |
| 98 | +use Tobyz\JsonApiServer\Endpoint\Show; |
| 99 | + |
| 100 | +class JobsResource extends Resource |
| 101 | +{ |
| 102 | + public function endpoints(): array |
| 103 | + { |
| 104 | + return [ |
| 105 | + Show::make()->seeOther(function ($model, Context $context) { |
| 106 | + if ($model->status === 'completed') { |
| 107 | + return "photos/$model->result_id"; |
| 108 | + } |
| 109 | + }), |
| 110 | + ]; |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +The `seeOther` method automatically: |
| 116 | + |
| 117 | +- Returns a `303 See Other` response when the callback returns a value, with the |
| 118 | + returned string as the `Location` header |
| 119 | +- Adds OpenAPI schema for the 303 response |
0 commit comments