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

BatchQueue::marshalJob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 9
nc 1
nop 2
crap 1
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 Illuminate\Queue\Jobs\DatabaseJobRecord;
18
use LukeWaite\LaravelQueueAwsBatch\Exceptions\JobNotFoundException;
19
use LukeWaite\LaravelQueueAwsBatch\Exceptions\UnsupportedException;
20
use LukeWaite\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 21
    public function __construct(
34
        Connection $database,
35
        $table,
36
        $default,
37
        $expire,
38
        $jobDefinition,
39
        BatchClient $batch
40
    ) {
41 21
        $this->jobDefinition = $jobDefinition;
42 21
        $this->batch = $batch;
43 21
        parent::__construct($database, $table, $default, $expire);
44 21
    }
45
46 6
    public function push($job, $data = '', $queue = null)
47
    {
48 6
        $payload = $this->createPayload($job, $data);
49 6
        return $this->pushToBatch($queue, $payload, $this->getBatchDisplayName($job));
50
    }
51
52
    public function pushRaw($payload, $queue = null, array $options = [])
53
    {
54
        return $this->pushToBatch($queue, $payload, 'raw-job');
55
    }
56
57
    /**
58
     * Get the display name for the given job.
59
     *
60
     * @param  mixed  $job
61
     * @return string
62
     */
63 6
    protected function getBatchDisplayName($job)
64
    {
65 6
        if (is_object($job)) {
66 3
            return method_exists($job, 'displayName')
67 3
                ? $job->displayName() : str_replace('\\', '_', (string)get_class($job));
68
        } else {
69 3
            return is_string($job) ? explode('@', $job)[0] : null;
70
        }
71
    }
72
73
    /**
74
     * Push a raw payload to the database, then to AWS Batch, with a given delay.
75
     *
76
     * @param string|null $queue
77
     * @param string      $payload
78
     * @param string      $jobName
79
     *
80
     * @return int
81
     */
82 6
    protected function pushToBatch($queue, $payload, $jobName)
83
    {
84 6
        $jobId = $this->pushToDatabase($queue, $payload);
85
86 6
        $this->batch->submitJob([
87 6
            'jobDefinition' => $this->jobDefinition,
88 6
            'jobName'       => $jobName,
89 6
            'jobQueue'      => $this->getQueue($queue),
90
            'parameters'    => [
91 6
                'jobId' => $jobId,
92
            ]
93 2
        ]);
94
95 6
        return $jobId;
96
    }
97
98 3
    public function getJobById($id)
99
    {
100 3
        $this->database->beginTransaction();
101
102 3
        $job = $this->database->table($this->table)
103 3
            ->lockForUpdate()
104 3
            ->where('id', $id)
105 3
            ->first();
106
107
108 3
        if (!isset($job)) {
109
            throw new JobNotFoundException('Could not find the job');
110
        }
111
112 3
        $job = new DatabaseJobRecord($job);
113
114 3
        return $this->marshalJob($job->queue, $job);
115
    }
116
117 3
    protected function marshalJob($queue, $job)
118
    {
119 3
        $job = $this->markJobAsReserved($job);
120
121 3
        $this->database->commit();
122
123 3
        return new BatchJob(
124 3
            $this->container,
125 1
            $this,
126 1
            $job,
0 ignored issues
show
Documentation introduced by
$job is of type object<Illuminate\Queue\Jobs\DatabaseJobRecord>, but the function expects a object<stdClass>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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