Test Failed
Push — master ( 49af15...b4a160 )
by
unknown
04:48
created

BatchQueue::pop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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
            return method_exists($job, 'displayName')
68 1
                ? $job->displayName() : str_replace('\\', '_', (string) get_class($job));
69
        } else {
70 1
            return is_string($job) ? explode('@', $job)[0] : null;
71
        }
72
    }
73
74
    /**
75
     * Push a raw payload to the database, then to AWS Batch, with a given delay.
76
     *
77
     * @param string|null $queue
78
     * @param string      $payload
79
     * @param string      $jobName
80
     *
81
     * @return int
82
     */
83 2
    protected function pushToBatch($queue, $payload, $jobName)
84
    {
85 2
        $jobId = $this->pushToDatabase($queue, $payload);
86
87 2
        $json = json_decode($payload);
88 2
        $data = unserialize($json->data->command);
89 2
90 2
        $this->batch->submitJob([
91
            'jobDefinition' => $this->jobDefinition,
92 2
            'jobName'       => $jobName,
93
            'jobQueue'      => $this->getQueue($queue),
94
            'parameters'    => [
95
                'jobId'        => $jobId,
96 2
                'connectionId' => $data->connection
97
            ]
98
        ]);
99
100
        return $jobId;
101
    }
102
103
    public function getJobById($id)
104
    {
105
        $this->database->beginTransaction();
106
107
        $job = $this->database->table($this->table)
108
            ->lockForUpdate()
109
            ->where('id', $id)
110
            ->first();
111
112
113
        if (!isset($job)) {
114
            throw new JobNotFoundException('Could not find the job');
115
        }
116
117
        $job = new DatabaseJobRecord($job);
118
119
        return $this->marshalJob($job->queue, $job);
120
    }
121
122
    protected function marshalJob($queue, $job)
123
    {
124
        $job = $this->markJobAsReserved($job);
125
126
        $this->database->commit();
127
128
        return new BatchJob(
129
            $this->container,
130
            $this,
131
            $job,
0 ignored issues
show
Bug introduced by
$job of type Illuminate\Queue\Jobs\DatabaseJobRecord is incompatible with the type stdClass expected by parameter $job of DNXLabs\LaravelQueueAwsB...BatchJob::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
            /** @scrutinizer ignore-type */ $job,
Loading history...
132
            $this->connectionName,
133
            $queue
134
        );
135
    }
136
137
    /**
138
     * Release the job, without deleting first from the Queue
139
     *
140
     * @param string $queue
141
     * @param \StdClass $job
142
     * @param int $delay
143 2
     *
144
     * @return int
145 2
     * @throws UnsupportedException
146 1
     */
147
    public function release($queue, $job, $delay)
148
    {
149 1
        if ($delay != 0) {
150 1
            throw new UnsupportedException('The BatchJob does not support releasing back onto the queue with a delay');
151
        }
152
153
        return $this->database->table($this->table)->where('id', $job->id)->update([
154
            'attempts'    => $job->attempts,
155
            'reserved_at' => null
156
        ]);
157
    }
158
159
    public function pop($queue = null)
160
    {
161
        throw new UnsupportedException('The BatchQueue does not support running via a regular worker. ' .
162
            'Instead, you should use the queue:work-batch command with a job id.');
163
    }
164
165
    public function bulk($jobs, $data = '', $queue = null)
166
    {
167
        // This could be implemented, but it's not in first pass.
168
        throw new UnsupportedException('The BatchQueue does not currently support the bulk() operation.');
169
    }
170 1
171
    public function later($delay, $job, $data = '', $queue = null)
172 1
    {
173
        throw new UnsupportedException('The BatchQueue does not support the later() operation.');
174
    }
175
}
176