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\Console; |
13
|
|
|
|
14
|
|
|
use Illuminate\Console\Command; |
15
|
|
|
use Illuminate\Foundation\Exceptions\Handler; |
16
|
|
|
use Illuminate\Queue\Console\WorkCommand; |
17
|
|
|
use Illuminate\Queue\QueueManager; |
18
|
|
|
use Illuminate\Queue\Worker; |
19
|
|
|
use Illuminate\Queue\WorkerOptions; |
20
|
|
|
use LukeWaite\LaravelQueueAwsBatch\Exceptions\JobNotFoundException; |
21
|
|
|
use LukeWaite\LaravelQueueAwsBatch\Exceptions\UnsupportedException; |
22
|
|
|
use LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue; |
23
|
|
|
use Symfony\Component\Debug\Exception\FatalThrowableError; |
24
|
|
|
|
25
|
|
|
class QueueWorkBatchCommand extends WorkCommand |
26
|
|
|
{ |
27
|
|
|
protected $name = 'queue:work-batch'; |
28
|
|
|
|
29
|
|
|
protected $description = 'Run a Job for the AWS Batch queue'; |
30
|
|
|
|
31
|
|
|
// protected $signature = 'queue:work-batch {connection} {job_id} {--tries=}'; |
|
|
|
|
32
|
|
|
protected $signature = 'queue:work-batch |
33
|
|
|
{job_id : The job id in the database} |
34
|
|
|
{connection? : The name of the queue connection to work} |
35
|
|
|
{--memory=128 : The memory limit in megabytes} |
36
|
|
|
{--timeout=60 : The number of seconds a child process can run} |
37
|
|
|
{--tries=0 : Number of times to attempt a job before logging it failed}'; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
protected $manager; |
41
|
|
|
protected $exceptions; |
42
|
|
|
|
43
|
|
|
public function __construct(QueueManager $manager, Worker $worker, Handler $exceptions) |
44
|
|
|
{ |
45
|
|
|
parent::__construct($worker); |
46
|
|
|
$this->manager = $manager; |
47
|
|
|
$this->exceptions = $exceptions; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function fire() |
51
|
|
|
{ |
52
|
|
|
$this->listenForEvents(); |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
$this->runJob(); |
56
|
|
|
} catch (\Exception $e) { |
57
|
|
|
$this->exceptions->report($e); |
58
|
|
|
throw $e; |
59
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
60
|
|
|
$this->exceptions->report(new FatalThrowableError($e)); |
61
|
|
|
throw $e; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// TOOD: Refactor out the logic here into an extension of the Worker class |
66
|
|
|
protected function runJob() |
67
|
|
|
{ |
68
|
|
|
$connectionName = $this->argument('connection'); |
69
|
|
|
$jobId = $this->argument('job_id'); |
70
|
|
|
|
71
|
|
|
/** @var BatchQueue $connection */ |
72
|
|
|
$connection = $this->manager->connection($connectionName); |
|
|
|
|
73
|
|
|
|
74
|
|
|
if (!$connection instanceof BatchQueue) { |
75
|
|
|
throw new UnsupportedException('queue:work-batch can only be run on batch queues'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$job = $connection->getJobById($jobId, $connectionName); |
|
|
|
|
79
|
|
|
|
80
|
|
|
// If we're able to pull a job off of the stack, we will process it and |
81
|
|
|
// then immediately return back out. |
82
|
|
|
if (!is_null($job)) { |
83
|
|
|
return $this->worker->process( |
84
|
|
|
$this->manager->getName($connectionName), |
|
|
|
|
85
|
|
|
$job, |
86
|
|
|
$this->gatherWorkerOptions() |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// If we hit this point, we haven't processed our job |
91
|
|
|
throw new JobNotFoundException('No job was returned'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Gather all of the queue worker options as a single object. |
96
|
|
|
* |
97
|
|
|
* @return \Illuminate\Queue\WorkerOptions |
98
|
|
|
*/ |
99
|
|
|
protected function gatherWorkerOptions() |
100
|
|
|
{ |
101
|
|
|
return new WorkerOptions( |
102
|
|
|
0, $this->option('memory'), |
|
|
|
|
103
|
|
|
$this->option('timeout'), 0, |
|
|
|
|
104
|
|
|
$this->option('tries'), false |
|
|
|
|
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.