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/dnxlabs/laravel-queue-aws-batch |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DNXLabs\LaravelQueueAwsBatch\Console; |
13
|
|
|
|
14
|
|
|
use Illuminate\Container\Container; |
15
|
|
|
use Illuminate\Contracts\Cache\Repository; |
16
|
|
|
use Illuminate\Foundation\Exceptions\Handler; |
17
|
|
|
use Illuminate\Queue\Console\WorkCommand; |
18
|
|
|
use Illuminate\Queue\QueueManager; |
19
|
|
|
use Illuminate\Queue\Worker; |
20
|
|
|
use Illuminate\Queue\WorkerOptions; |
21
|
|
|
use DNXLabs\LaravelQueueAwsBatch\Exceptions\JobNotFoundException; |
22
|
|
|
use DNXLabs\LaravelQueueAwsBatch\Exceptions\UnsupportedException; |
23
|
|
|
use DNXLabs\LaravelQueueAwsBatch\Queues\BatchQueue; |
24
|
|
|
use Symfony\Component\Debug\Exception\FatalThrowableError; |
25
|
|
|
|
26
|
|
|
class QueueWorkBatchCommand extends WorkCommand |
27
|
|
|
{ |
28
|
|
|
protected $name = 'queue:work-batch'; |
29
|
|
|
|
30
|
|
|
protected $description = 'Run a Job for the AWS Batch queue'; |
31
|
|
|
|
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
|
|
|
{--force : Force the worker to run even in maintenance mode} |
39
|
|
|
{--queue= : The names of the queues to work} |
40
|
|
|
{--once : Only process the next job on the queue} |
41
|
|
|
{--stop-when-empty : Stop when the queue is empty}'; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
protected $manager; |
45
|
|
|
protected $exceptions; |
46
|
|
|
|
47
|
|
|
public function __construct(QueueManager $manager, Worker $worker, Handler $exceptions) |
48
|
|
|
{ |
49
|
|
|
parent::__construct($worker, Container::getInstance()->make(Repository::class)); |
50
|
|
|
$this->manager = $manager; |
51
|
|
|
$this->exceptions = $exceptions; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function handle() |
55
|
|
|
{ |
56
|
|
|
$this->listenForEvents(); |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
$this->runJob(); |
60
|
|
|
} catch (\Exception $e) { |
61
|
|
|
$this->exceptions->report($e); |
62
|
|
|
throw $e; |
63
|
|
|
} catch (\Throwable $e) { |
64
|
|
|
$this->exceptions->report(new FatalThrowableError($e)); |
|
|
|
|
65
|
|
|
throw $e; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// TOOD: Refactor out the logic here into an extension of the Worker class |
70
|
|
|
protected function runJob() |
71
|
|
|
{ |
72
|
|
|
$connectionName = $connection = $this->argument('connection') ?: $this->laravel['config']['queue.default']; |
|
|
|
|
73
|
|
|
$jobId = $this->argument('job_id'); |
74
|
|
|
|
75
|
|
|
/** @var BatchQueue $connection */ |
76
|
|
|
$connection = $this->manager->connection($connectionName); |
|
|
|
|
77
|
|
|
|
78
|
|
|
if (!$connection instanceof BatchQueue) { |
|
|
|
|
79
|
|
|
throw new UnsupportedException('queue:work-batch can only be run on batch queues'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$job = $connection->getJobById($jobId, $connectionName); |
|
|
|
|
83
|
|
|
|
84
|
|
|
// If we're able to pull a job off of the stack, we will process it and |
85
|
|
|
// then immediately return back out. |
86
|
|
|
if (!is_null($job)) { |
87
|
|
|
return $this->worker->process( |
|
|
|
|
88
|
|
|
$this->manager->getName($connectionName), |
|
|
|
|
89
|
|
|
$job, |
90
|
|
|
$this->gatherWorkerOptions() |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// If we hit this point, we haven't processed our job |
95
|
|
|
throw new JobNotFoundException('No job was returned'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Gather all of the queue worker options as a single object. |
100
|
|
|
* |
101
|
|
|
* @return \Illuminate\Queue\WorkerOptions |
102
|
|
|
*/ |
103
|
|
|
protected function gatherWorkerOptions() |
104
|
|
|
{ |
105
|
|
|
return new WorkerOptions( |
106
|
|
|
0, // delay |
107
|
|
|
$this->option('memory'), |
108
|
|
|
$this->option('timeout'), |
109
|
|
|
0, // sleep |
110
|
|
|
$this->option('tries'), |
111
|
|
|
false, // force |
112
|
|
|
$this->option('stop-when-empty') |
|
|
|
|
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|