Completed
Push — master ( 0fae22...156482 )
by Charles
03:07
created

WorkerController::options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace yrc\commands;
4
5
use Exception;
6
use yii\console\Controller;
7
use Yii;
8
9
class WorkerController extends Controller
10
{
11
    /**
12
     * @var string
13
     * The RPQ Job ID as passed by the RPQ CLI
14
     */
15
    public $jobId;
16
17
    /**
18
     * @var string
19
     * The name of the RPQ queue that this command should search for jobs in.
20
     */
21
    public $name = 'default';
22
23
    /**
24
     * Command line options
25
     * @param string $actionID
26
     * @return array
27
     */
28
    public function options($actionID)
29
    {
30
        return [
31
            'jobId',
32
            'name'
33
        ];
34
    }
35
36
    /**
37
     * Starts a worker in process mode
38
     * @return integer
39
     */
40
    public function actionProcess()
41
    {
42
        $hash = explode(':', $this->jobId);
43
        $jobId = $hash[count($hash) - 1];
44
45
        try {
46
            $job = Yii::$app->rpq->getQueue($this->name)->getJob($jobId);
47
            $class = $job->getWorkerClass();
48
49
            if (!\class_exists($class)) {
50
                throw new Exception("Unable to find worker class {$class}");
51
            }
52
53
            if (!\is_subclass_of($class, '\RPQ\Server\AbstractJob')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
54
                throw new Exception('Job does not implement RPQ\Server\AbstractJob');
55
            }
56
            
57
            $task = new $class(Yii::$app->rpq->getClient(), $job->getId());
58
            return $task->perform($job->getArgs());
59
        } catch (Exception $e) {
60
            // Log the error to the appropriate handler
61
            Yii::error([
62
                'message' => 'An error occured when executing the job',
63
                'jobId' => $job->getId(),
64
                'workerClass' => $job->getWorkerClass(),
65
                'queueName' => $this->name,
66
                'message' => $e->getMessage(),
67
                'code' => $e->getCode(),
68
                'file' => $e->getFile(),
69
                'line' => $e->getLine()
70
            ], 'rpq');
71
            return -1;
72
        }
73
74
        return 0;
0 ignored issues
show
Unused Code introduced by
return 0; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
75
    }
76
}