Skip to content

Commit c0bfc89

Browse files
committed
Add initial draft of handling failed jobs
1 parent 7b050e2 commit c0bfc89

File tree

5 files changed

+90
-4
lines changed

5 files changed

+90
-4
lines changed

src/CloudTasksJob.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ class CloudTasksJob extends LaravelJob implements JobContract
1010
{
1111
private $job;
1212
private $attempts;
13+
private $maxTries;
1314

14-
public function __construct($job, $attempts)
15+
public function __construct($job)
1516
{
1617
$this->job = $job;
17-
$this->attempts = $attempts;
1818
$this->container = Container::getInstance();
1919
}
2020

@@ -32,4 +32,24 @@ public function attempts()
3232
{
3333
return $this->attempts;
3434
}
35+
36+
public function setAttempts($attempts)
37+
{
38+
$this->attempts = $attempts;
39+
}
40+
41+
public function setMaxTries($maxTries)
42+
{
43+
$this->maxTries = $maxTries;
44+
}
45+
46+
public function maxTries()
47+
{
48+
return $this->maxTries;
49+
}
50+
51+
public function setQueue($queue)
52+
{
53+
$this->queue = $queue;
54+
}
3555
}

src/CloudTasksQueue.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0, $attempts = 0)
6060
$httpRequest->setHttpMethod(HttpMethod::POST);
6161
$httpRequest->setBody($payload);
6262

63+
$httpRequest->setHeaders([
64+
'X-Stackkit-Max-Attempts' => $this->getMaxAttempts($queueName),
65+
]);
66+
6367
$task = $this->createTask();
6468
$task->setHttpRequest($httpRequest);
6569

@@ -99,4 +103,9 @@ private function createTask()
99103
{
100104
return app(Task::class);
101105
}
106+
107+
private function getMaxAttempts($queue)
108+
{
109+
return $this->client->getQueue($queue)->getRetryConfig()->getMaxAttempts();
110+
}
102111
}

src/TaskHandler.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Google\Cloud\Tasks\V2\CloudTasksClient;
66
use Illuminate\Http\Request;
7+
use Illuminate\Queue\Events\JobFailed;
78
use Illuminate\Queue\Worker;
89
use Illuminate\Queue\WorkerOptions;
910
use Firebase\JWT\JWT;
@@ -33,6 +34,8 @@ public function handle($task = null)
3334

3435
$task = $task ?: $this->captureTask();
3536

37+
$this->listenForEvents();
38+
3639
$this->handleTask($task);
3740
}
3841

@@ -81,7 +84,11 @@ private function captureTask()
8184
{
8285
$input = file_get_contents('php://input');
8386

84-
if ($input === false) {
87+
if (!$input) {
88+
$input = request('input') ?: false;
89+
}
90+
91+
if (!$input) {
8592
throw new CloudTasksException('Could not read incoming task');
8693
}
8794

@@ -94,13 +101,27 @@ private function captureTask()
94101
return $task;
95102
}
96103

104+
private function listenForEvents()
105+
{
106+
app('events')->listen(JobFailed::class, function ($event) {
107+
app('queue.failer')->log(
108+
'cloudtasks', $event->job->getQueue(),
109+
$event->job->getRawBody(), $event->exception
110+
);
111+
});
112+
}
113+
97114
/**
98115
* @param $task
99116
* @throws CloudTasksException
100117
*/
101118
private function handleTask($task)
102119
{
103-
$job = new CloudTasksJob($task, request()->header('X-CloudTasks-TaskRetryCount'));
120+
$job = new CloudTasksJob($task);
121+
122+
$job->setAttempts(request()->header('X-CloudTasks-TaskRetryCount') + 1);
123+
$job->setQueue(request()->header('X-Cloudtasks-Queuename'));
124+
$job->setMaxTries(request()->header('X-Stackkit-Max-Attempts'));
104125

105126
$worker = $this->getQueueWorker();
106127

tests/Support/FailingJob.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Tests\Support;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Foundation\Bus\Dispatchable;
8+
use Illuminate\Queue\InteractsWithQueue;
9+
use Illuminate\Queue\SerializesModels;
10+
use Illuminate\Support\Facades\Mail;
11+
12+
class FailingJob implements ShouldQueue
13+
{
14+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
15+
16+
/**
17+
* Create a new job instance.
18+
*
19+
* @return void
20+
*/
21+
public function __construct()
22+
{
23+
//
24+
}
25+
26+
/**
27+
* Execute the job.
28+
*
29+
* @return void
30+
*/
31+
public function handle()
32+
{
33+
throw new \Error('simulating a failing job');
34+
}
35+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"uuid":"f4e1ea03-3ab9-45f8-a4a1-50218169472e","displayName":"Tests\\Support\\FailingJob","job":"Illuminate\\Queue\\CallQueuedHandler@call","maxTries":null,"maxExceptions":null,"delay":null,"timeout":null,"timeoutAt":null,"data":{"commandName":"Tests\\Support\\FailingJob","command":"O:24:\"Tests\\Support\\FailingJob\":8:{s:3:\"job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:5:\"delay\";N;s:10:\"middleware\";a:0:{}s:7:\"chained\";a:0:{}}"}}

0 commit comments

Comments
 (0)