| 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\Queues; |
||
| 13 | |||
| 14 | use Aws\Batch\BatchClient; |
||
| 15 | use Illuminate\Database\Connection; |
||
| 16 | use Illuminate\Queue\DatabaseQueue; |
||
| 17 | use Illuminate\Queue\Jobs\DatabaseJobRecord; |
||
| 18 | use DNXLabs\LaravelQueueAwsBatch\Exceptions\JobNotFoundException; |
||
| 19 | use DNXLabs\LaravelQueueAwsBatch\Exceptions\UnsupportedException; |
||
| 20 | use DNXLabs\LaravelQueueAwsBatch\Jobs\BatchJob; |
||
| 21 | |||
| 22 | class BatchQueue extends DatabaseQueue |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * The AWS Batch client. |
||
| 26 | * |
||
| 27 | * @var BatchClient |
||
| 28 | */ |
||
| 29 | protected $batch; |
||
| 30 | |||
| 31 | protected $jobDefinition; |
||
| 32 | |||
| 33 | 5 | public function __construct( |
|
| 34 | Connection $database, |
||
| 35 | $table, |
||
| 36 | $default, |
||
| 37 | $expire, |
||
| 38 | $jobDefinition, |
||
| 39 | BatchClient $batch |
||
| 40 | ) { |
||
| 41 | 5 | $this->jobDefinition = $jobDefinition; |
|
| 42 | 5 | $this->batch = $batch; |
|
| 43 | 5 | parent::__construct($database, $table, $default, $expire); |
|
| 44 | 5 | } |
|
| 45 | |||
| 46 | 2 | public function push($job, $data = '', $queue = null) |
|
| 47 | { |
||
| 48 | 2 | $queue = $this->getQueue($queue); |
|
| 49 | 2 | $payload = $this->createPayload($job, $queue, $data); |
|
| 50 | 2 | return $this->pushToBatch($queue, $payload, $this->getBatchDisplayName($job)); |
|
| 51 | } |
||
| 52 | |||
| 53 | public function pushRaw($payload, $queue = null, array $options = []) |
||
| 54 | { |
||
| 55 | return $this->pushToBatch($queue, $payload, 'raw-job'); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get the display name for the given job. |
||
| 60 | * |
||
| 61 | * @param mixed $job |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | 2 | protected function getBatchDisplayName($job) |
|
| 65 | { |
||
| 66 | 2 | if (is_object($job)) { |
|
| 67 | 1 | $jobName = method_exists($job, 'displayName') |
|
| 68 | 1 | ? $job->displayName() : str_replace('\\', '_', (string) get_class($job)); |
|
| 69 | } else { |
||
| 70 | 1 | $jobName = is_string($job) ? explode('@', $job)[0] : null; |
|
| 71 | } |
||
| 72 | |||
| 73 | return str_limit($jobName, 128); // Limit requested by AWS Batch SubmitJob API |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Push a raw payload to the database, then to AWS Batch, with a given delay. |
||
| 78 | * |
||
| 79 | * @param string|null $queue |
||
| 80 | * @param string $payload |
||
| 81 | * @param string $jobName |
||
| 82 | * |
||
| 83 | 2 | * @return int |
|
| 84 | */ |
||
| 85 | 2 | protected function pushToBatch($queue, $payload, $jobName) |
|
| 86 | { |
||
| 87 | 2 | $jobId = $this->pushToDatabase($queue, $payload); |
|
| 88 | 2 | ||
| 89 | 2 | $json = json_decode($payload); |
|
| 90 | 2 | $data = unserialize($json->data->command); |
|
| 91 | |||
| 92 | 2 | $this->batch->submitJob([ |
|
| 93 | 'jobDefinition' => $this->jobDefinition, |
||
| 94 | 'jobName' => $jobName, |
||
| 95 | 'jobQueue' => $this->getQueue($queue), |
||
| 96 | 2 | 'parameters' => [ |
|
| 97 | 'jobId' => $jobId, |
||
| 98 | 'connectionId' => $data->connection |
||
| 99 | ] |
||
| 100 | ]); |
||
| 101 | |||
| 102 | return $jobId; |
||
| 103 | } |
||
| 104 | |||
| 105 | public function getJobById($id) |
||
| 106 | { |
||
| 107 | $this->database->beginTransaction(); |
||
| 108 | |||
| 109 | $job = $this->database->table($this->table) |
||
| 110 | ->lockForUpdate() |
||
| 111 | ->where('id', $id) |
||
| 112 | ->first(); |
||
| 113 | |||
| 114 | |||
| 115 | if (!isset($job)) { |
||
| 116 | throw new JobNotFoundException('Could not find the job'); |
||
| 117 | } |
||
| 118 | |||
| 119 | $job = new DatabaseJobRecord($job); |
||
| 120 | |||
| 121 | return $this->marshalJob($job->queue, $job); |
||
| 122 | } |
||
| 123 | |||
| 124 | protected function marshalJob($queue, $job) |
||
| 125 | { |
||
| 126 | $job = $this->markJobAsReserved($job); |
||
| 127 | |||
| 128 | $this->database->commit(); |
||
| 129 | |||
| 130 | return new BatchJob( |
||
| 131 | $this->container, |
||
| 132 | $this, |
||
| 133 | $job, |
||
| 134 | $this->connectionName, |
||
| 135 | $queue |
||
| 136 | ); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Release the job, without deleting first from the Queue |
||
| 141 | * |
||
| 142 | * @param string $queue |
||
| 143 | 2 | * @param \StdClass $job |
|
| 144 | * @param int $delay |
||
| 145 | 2 | * |
|
| 146 | 1 | * @return int |
|
| 147 | * @throws UnsupportedException |
||
| 148 | */ |
||
| 149 | 1 | public function release($queue, $job, $delay) |
|
| 150 | 1 | { |
|
| 151 | if ($delay != 0) { |
||
| 152 | throw new UnsupportedException('The BatchJob does not support releasing back onto the queue with a delay'); |
||
| 153 | } |
||
| 154 | |||
| 155 | return $this->database->table($this->table)->where('id', $job->id)->update([ |
||
| 156 | 'attempts' => $job->attempts, |
||
| 157 | 'reserved_at' => null |
||
| 158 | ]); |
||
| 159 | } |
||
| 160 | |||
| 161 | public function pop($queue = null) |
||
| 162 | { |
||
| 163 | throw new UnsupportedException('The BatchQueue does not support running via a regular worker. ' . |
||
| 164 | 'Instead, you should use the queue:work-batch command with a job id.'); |
||
| 165 | } |
||
| 166 | |||
| 167 | public function bulk($jobs, $data = '', $queue = null) |
||
| 168 | { |
||
| 169 | // This could be implemented, but it's not in first pass. |
||
| 170 | 1 | throw new UnsupportedException('The BatchQueue does not currently support the bulk() operation.'); |
|
| 171 | } |
||
| 172 | 1 | ||
| 173 | public function later($delay, $job, $data = '', $queue = null) |
||
| 174 | { |
||
| 175 | throw new UnsupportedException('The BatchQueue does not support the later() operation.'); |
||
| 176 | } |
||
| 177 | } |
||
| 178 |