Skip to content

Commit b048895

Browse files
authored
Merge pull request #2 from intouchinsight/fix-log-failed-jobs
Failed Jobs & Container Overrides
2 parents 924434c + 4c334fe commit b048895

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

src/Console/QueueWorkBatchCommand.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Illuminate\Console\Command;
1616
use Illuminate\Contracts\Cache\Repository as Cache;
1717
use Illuminate\Foundation\Exceptions\Handler;
18+
use Illuminate\Queue\Events\JobFailed;
1819
use Illuminate\Queue\QueueManager;
1920
use Illuminate\Queue\Worker;
2021
use Illuminate\Queue\WorkerOptions;
@@ -56,6 +57,10 @@ public function __construct(QueueManager $manager, Worker $worker, Handler $exce
5657

5758
public function handle()
5859
{
60+
$this->laravel['events']->listen(JobFailed::class, function ($event) {
61+
$this->logFailedJob($event);
62+
});
63+
5964
try {
6065
$this->runJob();
6166
} catch (\Throwable $e) {
@@ -109,4 +114,20 @@ protected function gatherWorkerOptions()
109114
maxTries: $this->option('tries'),
110115
);
111116
}
117+
118+
/**
119+
* Store a failed job event.
120+
*
121+
* @param \Illuminate\Queue\Events\JobFailed $event
122+
* @return void
123+
*/
124+
protected function logFailedJob(JobFailed $event)
125+
{
126+
$this->laravel['queue.failer']->log(
127+
$event->connectionName,
128+
$event->job->getQueue(),
129+
$event->job->getRawBody(),
130+
$event->exception
131+
);
132+
}
112133
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Laravel Queue for AWS Batch.
4+
*
5+
* @author Luke Waite <[email protected]>
6+
* @copyright 2017 Luke Waite
7+
* @license http://www.opensource.org/licenses/mit-license.php MIT
8+
*
9+
* @link https://github.com/lukewaite/laravel-queue-aws-batch
10+
*/
11+
12+
namespace LukeWaite\LaravelQueueAwsBatch\Contracts;
13+
14+
/**
15+
* Should return an array representing the contents of the containerOverrides
16+
* property documented in the AWS Batch SubmitJob API reference.
17+
*
18+
* In the event of no overrides, should return null.
19+
*
20+
* https://docs.aws.amazon.com/batch/latest/APIReference/API_SubmitJob.html
21+
*
22+
* [
23+
* "command": ["string"],
24+
* "environment": [
25+
* [
26+
* "name": "string",
27+
* "value": "string"
28+
* ]
29+
* ],
30+
* "memory": number,
31+
* "vcpus": number
32+
* ]
33+
*
34+
* Interface JobContainerOverrides
35+
* @package LukeWaite\LaravelQueueAwsBatch\Contracts
36+
*/
37+
interface JobContainerOverrides
38+
{
39+
public function getBatchContainerOverrides();
40+
}

src/Queues/BatchQueue.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Illuminate\Database\Connection;
1717
use Illuminate\Queue\DatabaseQueue;
1818
use Illuminate\Queue\Jobs\DatabaseJobRecord;
19+
use LukeWaite\LaravelQueueAwsBatch\Contracts\JobContainerOverrides;
1920
use LukeWaite\LaravelQueueAwsBatch\Exceptions\JobNotFoundException;
2021
use LukeWaite\LaravelQueueAwsBatch\Exceptions\UnsupportedException;
2122
use LukeWaite\LaravelQueueAwsBatch\Jobs\BatchJob;
@@ -76,26 +77,42 @@ protected function getBatchDisplayName($job)
7677
* Push a raw payload to the database, then to AWS Batch, with a given delay.
7778
*
7879
* @param string|null $queue
79-
* @param string $payload
80+
* @param array $payload
8081
* @param string $jobName
8182
* @return int
8283
*/
8384
protected function pushToBatch($queue, $payload, $jobName)
8485
{
8586
$jobId = $this->pushToDatabase($queue, $payload);
8687

87-
$this->batch->submitJob([
88+
$payload = [
8889
'jobDefinition' => $this->jobDefinition,
8990
'jobName' => $jobName,
9091
'jobQueue' => $this->getQueue($queue),
9192
'parameters' => [
9293
'jobId' => $jobId,
93-
],
94-
]);
94+
]
95+
];
96+
97+
if (isset($job) && is_object($job) && $this->implementsJobContainerOverrides($job)) {
98+
/** @var JobContainerOverrides $job */
99+
$overrides = $job->getBatchContainerOverrides();
100+
101+
if (isset($overrides)) {
102+
$payload['containerOverrides'] = $overrides;
103+
}
104+
}
105+
106+
$this->batch->submitJob($payload);
95107

96108
return $jobId;
97109
}
98110

111+
private function implementsJobContainerOverrides($job)
112+
{
113+
return $job instanceof JobContainerOverrides;
114+
}
115+
99116
public function getJobById($id)
100117
{
101118
$this->database->beginTransaction();

0 commit comments

Comments
 (0)