Skip to content

Commit e22e15b

Browse files
committed
Fix job dispatched before commit with afterCommit
1 parent 1c8ee02 commit e22e15b

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

src/CloudTasksQueue.php

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,15 @@ public function size($queue = null)
5252
*/
5353
public function push($job, $data = '', $queue = null)
5454
{
55-
$this->pushToCloudTasks($queue, $this->createPayload(
56-
$job, $this->getQueue($queue), $data
57-
));
55+
return $this->enqueueUsing(
56+
$job,
57+
$this->createPayload($job, $this->getQueue($queue), $data),
58+
$queue,
59+
null,
60+
function ($payload, $queue) {
61+
return $this->pushRaw($payload, $queue);
62+
}
63+
);
5864
}
5965

6066
/**
@@ -63,11 +69,11 @@ public function push($job, $data = '', $queue = null)
6369
* @param string $payload
6470
* @param string|null $queue
6571
* @param array $options
66-
* @return void
72+
* @return string
6773
*/
6874
public function pushRaw($payload, $queue = null, array $options = [])
6975
{
70-
$this->pushToCloudTasks($queue, $payload);
76+
return $this->pushToCloudTasks($queue, $payload);
7177
}
7278

7379
/**
@@ -81,9 +87,15 @@ public function pushRaw($payload, $queue = null, array $options = [])
8187
*/
8288
public function later($delay, $job, $data = '', $queue = null)
8389
{
84-
$this->pushToCloudTasks($queue, $this->createPayload(
85-
$job, $this->getQueue($queue), $data
86-
), $delay);
90+
return $this->enqueueUsing(
91+
$job,
92+
$this->createPayload($job, $this->getQueue($queue), $data),
93+
$queue,
94+
$delay,
95+
function ($payload, $queue, $delay) {
96+
return $this->pushToCloudTasks($queue, $payload, $delay);
97+
}
98+
);
8799
}
88100

89101
/**
@@ -92,7 +104,7 @@ public function later($delay, $job, $data = '', $queue = null)
92104
* @param string|null $queue
93105
* @param string $payload
94106
* @param \DateTimeInterface|\DateInterval|int $delay
95-
* @return void
107+
* @return string
96108
*/
97109
protected function pushToCloudTasks($queue, $payload, $delay = 0)
98110
{
@@ -103,12 +115,13 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
103115
$httpRequest = $this->createHttpRequest();
104116
$httpRequest->setUrl($this->getHandler());
105117
$httpRequest->setHttpMethod(HttpMethod::POST);
106-
$httpRequest->setBody(
107-
// Laravel 7+ jobs have a uuid, but Laravel 6 doesn't have it.
108-
// Since we are using and expecting the uuid in some places
109-
// we will add it manually here if it's not present yet.
110-
$this->withUuid($payload)
111-
);
118+
119+
// Laravel 7+ jobs have a uuid, but Laravel 6 doesn't have it.
120+
// Since we are using and expecting the uuid in some places
121+
// we will add it manually here if it's not present yet.
122+
[$payload, $uuid] = $this->withUuid($payload);
123+
124+
$httpRequest->setBody($payload);
112125

113126
$task = $this->createTask();
114127
$task->setHttpRequest($httpRequest);
@@ -128,9 +141,11 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
128141
$createdTask = CloudTasksApi::createTask($queueName, $task);
129142

130143
event((new TaskCreated)->queue($queue)->task($task));
144+
145+
return $uuid;
131146
}
132147

133-
private function withUuid(string $payload): string
148+
private function withUuid(string $payload): array
134149
{
135150
/** @var array $decoded */
136151
$decoded = json_decode($payload, true);
@@ -139,7 +154,10 @@ private function withUuid(string $payload): string
139154
$decoded['uuid'] = (string) Str::uuid();
140155
}
141156

142-
return json_encode($decoded);
157+
return [
158+
json_encode($decoded),
159+
$decoded['uuid'],
160+
];
143161
}
144162

145163
/**

tests/QueueTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
use Google\Cloud\Tasks\V2\HttpMethod;
88
use Google\Cloud\Tasks\V2\Task;
9+
use Illuminate\Queue\Events\JobQueued;
10+
use Illuminate\Support\Facades\DB;
11+
use Illuminate\Support\Facades\Event;
912
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
1013
use Stackkit\LaravelGoogleCloudTasksQueue\TaskHandler;
1114
use Tests\Support\FailingJob;
@@ -154,4 +157,24 @@ public function it_posts_the_task_the_correct_queue()
154157
&& $queueName === 'projects/my-test-project/locations/europe-west6/queues/my-special-queue';
155158
});
156159
}
160+
161+
/**
162+
* @test
163+
*/
164+
public function it_can_dispatch_after_commit()
165+
{
166+
// Arrange
167+
CloudTasksApi::fake();
168+
Event::fake();
169+
170+
// Act & Assert
171+
Event::assertNotDispatched(JobQueued::class);
172+
DB::beginTransaction();
173+
SimpleJob::dispatch()->afterCommit();
174+
Event::assertNotDispatched(JobQueued::class);
175+
DB::commit();
176+
Event::assertDispatched(JobQueued::class, function (JobQueued $event) {
177+
return $event->job instanceof SimpleJob;
178+
});
179+
}
157180
}

0 commit comments

Comments
 (0)