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')) {
|
|
|
|
|
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;
|
|
|
|
|
75
|
|
|
}
|
76
|
|
|
} |