Completed
Push — master ( c250f4...3517ac )
by
unknown
01:13
created

BatchQueue::pop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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/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
    public function __construct(
34
        Connection $database,
35
        $table,
36
        $default,
37
        $expire,
38
        $jobDefinition,
39
        BatchClient $batch
40
    ) {
41
        $this->jobDefinition = $jobDefinition;
42
        $this->batch = $batch;
43
        parent::__construct($database, $table, $default, $expire);
44
    }
45
46
    public function push($job, $data = '', $queue = null)
47
    {
48
        $queue = $this->getQueue($queue);
49
        $payload = $this->createPayload($job, $queue, $data);
50
        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
    protected function getBatchDisplayName($job)
65
    {
66
        if (is_object($job)) {
67
            return method_exists($job, 'displayName')
68
                ? $job->displayName() : str_replace('\\', '_', (string)get_class($job));
69
        } else {
70
            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
    protected function pushToBatch($queue, $payload, $jobName)
84
    {
85
        $jobId = $this->pushToDatabase($queue, $payload);
86
87
        $this->batch->submitJob([
88
            'jobDefinition' => $this->jobDefinition,
89
            'jobName'       => $jobName,
90
            'jobQueue'      => $this->getQueue($queue),
91
            'parameters'    => [
92
                'jobId' => $jobId,
93
            ]
94
        ]);
95
96
        return $jobId;
97
    }
98
99
    public function getJobById($id)
100
    {
101
        $this->database->beginTransaction();
102
103
        $job = $this->database->table($this->table)
104
            ->lockForUpdate()
105
            ->where('id', $id)
106
            ->first();
107
108
109
        if (!isset($job)) {
110
            throw new JobNotFoundException('Could not find the job');
111
        }
112
113
        $job = new DatabaseJobRecord($job);
114
115
        return $this->marshalJob($job->queue, $job);
116
    }
117
118
    protected function marshalJob($queue, $job)
119
    {
120
        $job = $this->markJobAsReserved($job);
121
122
        $this->database->commit();
123
124
        return new BatchJob(
125
            $this->container,
126
            $this,
127
            $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...
128
            $this->connectionName,
129
            $queue
130
        );
131
    }
132
133
    /**
134
     * Release the job, without deleting first from the Queue
135
     *
136
     * @param string $queue
137
     * @param \StdClass $job
138
     * @param int $delay
139
     *
140
     * @return int
141
     * @throws UnsupportedException
142
     */
143
    public function release($queue, $job, $delay)
144
    {
145
        if ($delay != 0) {
146
            throw new UnsupportedException('The BatchJob does not support releasing back onto the queue with a delay');
147
        }
148
149
        return $this->database->table($this->table)->where('id', $job->id)->update([
150
            'attempts'    => $job->attempts,
151
            'reserved_at' => null
152
        ]);
153
    }
154
155
    /*
156
     * Function pop() from parent class need to be executed on Laravel 6.x
157
     */
158
    // public function pop($queue = null)
159
    // {
160
    //     throw new UnsupportedException('The BatchQueue does not support running via a regular worker. ' .
161
    //         'Instead, you should use the queue:batch-work command with a job id.');
162
    // }
163
164
    public function bulk($jobs, $data = '', $queue = null)
165
    {
166
        // This could be implemented, but it's not in first pass.
167
        throw new UnsupportedException('The BatchQueue does not currently support the bulk() operation.');
168
    }
169
170
    public function later($delay, $job, $data = '', $queue = null)
171
    {
172
        throw new UnsupportedException('The BatchQueue does not support the later() operation.');
173
    }
174
}
175