QueueWorkBatchCommand::runJob()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 26
ccs 0
cts 12
cp 0
rs 9.8666
cc 4
nc 3
nop 0
crap 20
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\Console;
13
14
use Illuminate\Container\Container;
15
use Illuminate\Contracts\Cache\Repository;
16
use Illuminate\Foundation\Exceptions\Handler;
17
use Illuminate\Queue\Console\WorkCommand;
18
use Illuminate\Queue\QueueManager;
19
use Illuminate\Queue\Worker;
20
use Illuminate\Queue\WorkerOptions;
21
use DNXLabs\LaravelQueueAwsBatch\Exceptions\JobNotFoundException;
22
use DNXLabs\LaravelQueueAwsBatch\Exceptions\UnsupportedException;
23
use DNXLabs\LaravelQueueAwsBatch\Queues\BatchQueue;
24
use Symfony\Component\Debug\Exception\FatalThrowableError;
25
26
class QueueWorkBatchCommand extends WorkCommand
27
{
28
    protected $name = 'queue:work-batch';
29
30
    protected $description = 'Run a Job for the AWS Batch queue';
31
32
    protected $signature = 'queue:work-batch
33
                            {job_id : The job id in the database}
34
                            {connection? : The name of the queue connection to work}
35
                            {--memory=128 : The memory limit in megabytes}
36
                            {--timeout=60 : The number of seconds a child process can run}
37
                            {--tries=0 : Number of times to attempt a job before logging it failed}
38
                            {--force : Force the worker to run even in maintenance mode}
39
                            {--queue= : The names of the queues to work}
40
                            {--once : Only process the next job on the queue}
41
                            {--stop-when-empty : Stop when the queue is empty}';
42
43
44
    protected $manager;
45
    protected $exceptions;
46
47
    public function __construct(QueueManager $manager, Worker $worker, Handler $exceptions)
48
    {
49
        parent::__construct($worker, Container::getInstance()->make(Repository::class));
50
        $this->manager = $manager;
51
        $this->exceptions = $exceptions;
52
    }
53
54
    public function handle()
55
    {
56
        $this->listenForEvents();
57
58
        try {
59
            $this->runJob();
60
        } catch (\Exception $e) {
61
            $this->exceptions->report($e);
62
            throw $e;
63
        } catch (\Throwable $e) {
64
            $this->exceptions->report(new FatalThrowableError($e));
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Debug\...ion\FatalThrowableError has been deprecated: since Symfony 4.4 ( Ignorable by Annotation )

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

64
            $this->exceptions->report(/** @scrutinizer ignore-deprecated */ new FatalThrowableError($e));
Loading history...
65
            throw $e;
66
        }
67
    }
68
69
    // TOOD: Refactor out the logic here into an extension of the Worker class
70
    protected function runJob()
71
    {
72
        $connectionName = $connection = $this->argument('connection') ?: $this->laravel['config']['queue.default'];
0 ignored issues
show
Unused Code introduced by
The assignment to $connection is dead and can be removed.
Loading history...
73
        $jobId = $this->argument('job_id');
74
75
        /** @var BatchQueue $connection */
76
        $connection = $this->manager->connection($connectionName);
0 ignored issues
show
Bug introduced by
It seems like $connectionName can also be of type string[]; however, parameter $name of Illuminate\Queue\QueueManager::connection() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

76
        $connection = $this->manager->connection(/** @scrutinizer ignore-type */ $connectionName);
Loading history...
77
78
        if (!$connection instanceof BatchQueue) {
0 ignored issues
show
introduced by
$connection is always a sub-type of DNXLabs\LaravelQueueAwsBatch\Queues\BatchQueue.
Loading history...
79
            throw new UnsupportedException('queue:work-batch can only be run on batch queues');
80
        }
81
82
        $job = $connection->getJobById($jobId, $connectionName);
0 ignored issues
show
Unused Code introduced by
The call to DNXLabs\LaravelQueueAwsB...atchQueue::getJobById() has too many arguments starting with $connectionName. ( Ignorable by Annotation )

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

82
        /** @scrutinizer ignore-call */ 
83
        $job = $connection->getJobById($jobId, $connectionName);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
83
84
        // If we're able to pull a job off of the stack, we will process it and
85
        // then immediately return back out.
86
        if (!is_null($job)) {
87
            return $this->worker->process(
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->worker->process($...>gatherWorkerOptions()) targeting Illuminate\Queue\Worker::process() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
88
                $this->manager->getName($connectionName),
0 ignored issues
show
Bug introduced by
It seems like $connectionName can also be of type string[]; however, parameter $connection of Illuminate\Queue\QueueManager::getName() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

88
                $this->manager->getName(/** @scrutinizer ignore-type */ $connectionName),
Loading history...
89
                $job,
90
                $this->gatherWorkerOptions()
91
            );
92
        }
93
94
        // If we hit this point, we haven't processed our job
95
        throw new JobNotFoundException('No job was returned');
96
    }
97
98
    /**
99
     * Gather all of the queue worker options as a single object.
100
     *
101
     * @return \Illuminate\Queue\WorkerOptions
102
     */
103
    protected function gatherWorkerOptions()
104
    {
105
        return new WorkerOptions(
106
            0, // delay
107
            $this->option('memory'),
108
            $this->option('timeout'),
109
            0, // sleep
110
            $this->option('tries'),
111
            false, // force
112
            $this->option('stop-when-empty')
0 ignored issues
show
Bug introduced by
$this->option('stop-when-empty') of type string is incompatible with the type boolean expected by parameter $stopWhenEmpty of Illuminate\Queue\WorkerOptions::__construct(). ( Ignorable by Annotation )

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

112
            /** @scrutinizer ignore-type */ $this->option('stop-when-empty')
Loading history...
113
        );
114
    }
115
}
116