1
1
# Asynchronous Processing
2
2
3
- The JSON API specification
3
+ The JSON API specification
4
4
[ provides a recommendation] ( https://jsonapi.org/recommendations/#asynchronous-processing )
5
- for how APIs can implement long running processes. For example, if the operation to create a
6
- resource takes a long time, it is more appropriate to process the creation using
5
+ for how APIs can implement long running processes. For example, if the operation to create a
6
+ resource takes a long time, it is more appropriate to process the creation using
7
7
[ Laravel's queue system] ( https://laravel.com/docs/queues )
8
8
and return a ` 202 Accepted ` response to the client.
9
9
@@ -29,14 +29,14 @@ use Illuminate\Support\ServiceProvider;
29
29
30
30
class AppServiceProvider extends ServiceProvider
31
31
{
32
-
32
+
33
33
// ...
34
-
34
+
35
35
public function register()
36
36
{
37
37
LaravelJsonApi::runMigrations();
38
38
}
39
-
39
+
40
40
}
41
41
```
42
42
@@ -45,7 +45,7 @@ class AppServiceProvider extends ServiceProvider
45
45
If you want to customise the migrations, you can publish them as follows:
46
46
47
47
``` bash
48
- $ php artisan vendor:publish --tag=" json-api- migrations"
48
+ $ php artisan vendor:publish --tag=" json-api: migrations"
49
49
```
50
50
51
51
If you do this, you ** must not** call ` LaravelJsonApi::runMigrations() ` in your service provider.
@@ -76,7 +76,7 @@ use Neomerx\JsonApi\Schema\SchemaProvider;
76
76
class Schema extends SchemaProvider
77
77
{
78
78
use AsyncSchema;
79
-
79
+
80
80
// ...
81
81
}
82
82
```
@@ -112,7 +112,7 @@ class ProcessPodcast implements ShouldQueue
112
112
{
113
113
114
114
use ClientDispatchable;
115
-
115
+
116
116
// ...
117
117
}
118
118
@@ -130,7 +130,7 @@ means you can use any of the normal Laravel methods. The only difference is you
130
130
` dispatch ` method at the end of the chain so that you have access to the process that was stored
131
131
and can be serialized into JSON by your API.
132
132
133
- You can use this method of dispatching jobs in either
133
+ You can use this method of dispatching jobs in either
134
134
[ Controller Hooks] ( ../basics/controllers.md ) or within
135
135
[ Resource Adapters] ( ../basics/adapters.md ) , depending on your preference.
136
136
@@ -190,9 +190,9 @@ class Adapter extends AbstractAdapter
190
190
If a dispatched job creates a new resource (e.g. a new model), there is one additional step you will
191
191
need to follow in the job's ` handle ` method. This is to link the stored process to the resource that was
192
192
created as a result of the job completing successfully. The link must exist otherwise your API
193
- will not be able to inform a client of the location of the created resource once the job is complete.
193
+ will not be able to inform a client of the location of the created resource once the job is complete.
194
194
195
- You can easily create this link by calling the ` didCreate ` method that the ` ClientDispatchable `
195
+ You can easily create this link by calling the ` didCreate ` method that the ` ClientDispatchable `
196
196
trait adds to your job. For example:
197
197
198
198
``` php
@@ -205,13 +205,13 @@ class ProcessPodcast implements ShouldQueue
205
205
{
206
206
207
207
use ClientDispatchable;
208
-
208
+
209
209
// ...
210
-
210
+
211
211
public function handle()
212
212
{
213
213
// ...logic to process a podcast
214
-
214
+
215
215
$this->didCreate($podcast);
216
216
}
217
217
}
@@ -272,11 +272,11 @@ This enables the following routes:
272
272
273
273
- ` GET /podcasts/queue-jobs ` : this lists all ` queue-jobs ` resources for the ` podcasts `
274
274
resource type.
275
- - ` GET /podcasts/queue-jobs/<UUID> ` : this retrieves a specific ` queue-jobs ` resource
275
+ - ` GET /podcasts/queue-jobs/<UUID> ` : this retrieves a specific ` queue-jobs ` resource
276
276
for the ` podcasts ` resource type.
277
277
278
278
The resource type ` queue-jobs ` is the name used in the JSON API's recommendation for
279
- asynchronous processing. If you want to use a resource type, then you can change this
279
+ asynchronous processing. If you want to use a resource type, then you can change this
280
280
by editing the ` jobs.resource ` config setting in your API's configuration file.
281
281
282
282
Note that we assume the resource id of a process is a valid UUID. If you use something
@@ -291,7 +291,7 @@ JsonApi::register('default')->withNamespace('Api')->routes(function ($api) {
291
291
## HTTP Requests and Responses
292
292
293
293
Once you have followed the above instructions, you can now make HTTP requests and receive
294
- asynchronous process responses that following the
294
+ asynchronous process responses that following the
295
295
[ JSON API recommendation.] ( https://jsonapi.org/recommendations/#asynchronous-processing )
296
296
297
297
For example, a request to create a podcast would receive the following response:
0 commit comments