CommandTrait::actionProcess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace yiicod\jobqueue\commands;
4
5
use Illuminate\Queue\WorkerOptions;
6
use Yii;
7
use yiicod\jobqueue\failed\MongoFailedJobProvider;
8
use yiicod\jobqueue\handlers\ExceptionHandler;
9
use yiicod\jobqueue\Worker;
10
11
trait CommandTrait
12
{
13
    /**
14
     * Delay before getting jobs
15
     *
16
     * @var int
17
     */
18
    public $delay = 0;
19
20
    /**
21
     * Maximum memory usage
22
     *
23
     * @var int
24
     */
25
    public $memory = 128;
26
27
    /**
28
     * Sleep before getting new jobs
29
     *
30
     * @var int
31
     */
32
    public $sleep = 3;
33
34
    /**
35
     * Max tries to run job
36
     *
37
     * @var int
38
     */
39
    public $maxTries = 1;
40
41
    /**
42
     * Daemon timeout
43
     *
44
     * @var int
45
     */
46
    public $timeout = 60;
47
48
    /**
49
     * Queue name
50
     *
51
     * @var string
52
     */
53
    public $queue = 'default';
54
55
    /**
56
     * Connection name
57
     *
58
     * @var string
59
     */
60
    public $connection = 'default';
61
62
    /**
63
     * Available options
64
     *
65
     * @param string $actionId
66
     *
67
     * @return array
68
     */
69
    public function options($actionId)
70
    {
71
        return ['queue', 'connection'];
72
    }
73
74
    /**
75
     * Process job by id and connection
76
     */
77
    public function actionProcess($id)
78
    {
79
        $this->processJob(
80
            $this->connection, $id
81
        );
82
    }
83
84
    /**
85
     * Process the job
86
     *
87
     * @param $connectionName
88
     * @param $id
89
     *
90
     * @return array|null
91
     */
92
    protected function processJob($connectionName, $id)
93
    {
94
        // automatically send every new message to available log routes
95
        Yii::getLogger()->flushInterval = 1;
96
        //manager
97
        $queueManager = Yii::$app->jobqueue->getQueueManager();
98
        //worker
99
        $worker = new Worker($queueManager, new MongoFailedJobProvider(Yii::$app->mongodb, 'yii_jobs_failed'), new ExceptionHandler());
100
101
        $worker->runJobById($connectionName, $id, new WorkerOptions(
102
            $this->delay,
103
            $this->memory,
104
            $this->timeout,
105
            $this->sleep,
106
            $this->maxTries
107
        ));
108
    }
109
}
110