Completed
Pull Request — master (#26)
by Luke
02:36
created

BatchQueue::getBatchDisplayName()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

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