8000 Merge branch 'hotfix/1.0.1' · CodingSeo/laravel-json-api@6724619 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6724619

Browse files
committed
Merge branch 'hotfix/1.0.1'
2 parents 5d27fb8 + 23593ed commit 6724619

File tree

5 files changed

+76
-3
lines changed

5 files changed

+76
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
All notable changes to this project will be documented in this file. This project adheres to
33
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
44

5+
## [1.0.1] - 2019-03-12
6+
7+
### Fixed
8+
- A `303 See Other` will now not be returned if a client job has failed. This is necessary because
9+
otherwise there is no way for an API client to determine that the job completed but failed.
10+
511
## [1.0.0] - 2019-02-28
612

713
### Added

docs/features/async.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,40 @@ class ProcessPodcast implements ShouldQueue
217217
}
218218
```
219219

220+
## Routing
221+
222+
The final step of setup is to enable asynchronous process routes on a resource. These
223+
routes allow a client to check the current status of a process.
224+
225+
For example, if our `podcasts` resource used asynchronous processes when a podcast is
226+
created, we would need to add the following to our [route definitions](../basics/routing.md):
227+
228+
```php
229+
JsonApi::register('default')->withNamespace('Api')->routes(function ($api) {
230+
$api->resource('podcasts')->async();
231+
});
232+
```
233+
234+
This enables the following routes:
235+
236+
- `GET /podcasts/queue-jobs`: this lists all `queue-jobs` resources for the `podcasts`
237+
resource type.
238+
- `GET /podcasts/queue-jobs/<UUID>`: this retrieves a specific `queue-jobs` resource
239+
for the `podcasts` resource type.
240+
241+
The resource type `queue-jobs` is the name used in the JSON API's recommendation for
242+
asynchronous processing. If you want to use a resource type, then you can change this
243+
by editing the `jobs.resource` config setting in your API's configuration file.
244+
245+
Note that we assume the resource id of a process is a valid UUID. If you use something
246+
different, then you can pass a constraint into the `async()` method, as follows:
247+
248+
```php
249+
JsonApi::register('default')->withNamespace('Api')->routes(function ($api) {
250+
$api->resource('podcasts')->async('^\d+$');
251+
});
252+
```
253+
220254
## HTTP Requests and Responses
221255

222256
Once you have followed the above instructions, you can now make HTTP requests and receive

src/Queue/ClientJob.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ public function getResourceType(): string
119119
*/
120120
public function getLocation(): ?string
121121
{
122+
if ($this->failed) {
123+
return null;
124+
}
125+
122126
$type = $this->resource_type;
123127
$id = $this->resource_id;
124128

tests/dummy/database/factories/ClientJobFactory.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
/** @var Factory $factory */
2424

25-
$factory->define(ClientJob::class, function (Faker $faker) {
25+
$factory->define(ClientJob::class, function () {
2626
return [
2727
'api' => 'v1',
2828
'failed' => false,
@@ -36,6 +36,20 @@
3636
'completed_at' => $faker->dateTimeBetween('-10 minutes', 'now'),
3737
'failed' => false,
3838
'attempts' => $faker->numberBetween(1, 3),
39+
];
40+
});
41+
42+
$factory->state(ClientJob::class, 'failed', function (Faker $faker) {
43+
return [
44+
'completed_at' => $faker->dateTimeBetween('-10 minutes', 'now'),
45+
'failed' => true,
46+
'attempts' => $faker->numberBetween(1, 3),
47+
];
48+
});
49+
50+
$factory->state(ClientJob::class, 'with_download', function () {
51+
return [
52+
'resource_type' => 'downloads',
3953
'resource_id' => factory(Download::class)->create()->getRouteKey(),
4054
];
4155
});

tests/lib/Integration/Queue/QueueJobsTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testReadPending()
5454
*/
5555
public function testReadNotPending()
5656
{
57-
$job = factory(ClientJob::class)->states('success')->create();
57+
$job = factory(ClientJob::class)->states('success', 'with_download')->create();
5858

5959
$response = $this
6060
->getJsonApi($this->jobUrl($job))
@@ -71,7 +71,22 @@ public function testReadNotPending()
7171
*/
7272
public function testReadNotPendingCannotSeeOther()
7373
{
74-
$job = factory(ClientJob::class)->states('success')->create(['resource_id' => null]);
74+
$job = factory(ClientJob::class)->states('success')->create();
75+
$expected = $this->serialize($job);
76+
77+
$this->getJsonApi($this->jobUrl($job))
78+
->assertFetchedOneExact($expected)
79+
->assertHeaderMissing('Location');
80+
}
81+
82+
/**
83+
* If the async process fails, we do not expect it to return a See Other even if
84+
* it has a resource id. This is because otherwise there is no way for the client
85+
* to know that it failed.
86+
*/
87+
public function testReadFailed()
88+
{
89+
$job = factory(ClientJob::class)->states('failed', 'with_download')->create();
7590
$expected = $this->serialize($job);
7691

7792
$this->getJsonApi($this->jobUrl($job))

0 commit comments

Comments
 (0)
0