MyThreadableRecord::setJob()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 14
rs 10
1
<?php
2
3
4
namespace andmemasin\myabstract;
5
6
7
use andmemasin\myabstract\interfaces\MultiThreadableInterface;
8
use yii\db\ActiveQuery;
9
use yii\db\Expression;
10
11
class MyThreadableRecord extends MyActiveRecord implements MultiThreadableInterface
12
{
13
    protected $processColumnName = 'processing_by';
14
    protected $jobColumnName = 'processing_job_id';
15
16
    /**
17
     * @param string $jobId
18
     * @return integer count of rows updated
19
     */
20
    public function setJob(string $jobId, string $processId, int $limit = 0, $conditions = []): int
21
    {
22
23
        $db = \Yii::$app->db;
24
25
        $setValues = [
26
            $this->processColumnName => $processId,
27
            $this->jobColumnName => $jobId,
28
        ];
29
30
        $query = $db->createCommand()
31
            ->update(static::tableName(), $setValues, $conditions);
32
        $sql = $query->rawSql . " " . new Expression(" LIMIT $limit");
33
        return $db->createCommand($sql)->execute();
34
    }
35
36
37
    /**
38
     * @param string $jobId
39
     * @return integer count of rows updated
40
     */
41
    public function clearJob(string $jobId): int
42
    {
43
        $db = \Yii::$app->db;
44
        $conditions = [
45
            $this->jobColumnName => $jobId,
46
        ];
47
        $setValues = [
48
            $this->processColumnName => new Expression('NULL'),
49
            $this->jobColumnName => new Expression('NULL'),
50
        ];
51
52
        $query = $db->createCommand()
53
            ->update(static::tableName(), $setValues, $conditions);
54
        return $query->execute();
55
    }
56
57
    /**
58
     * Find all records being locked for this job
59
     * @param string $jobId
60
     * @return ActiveQuery
61
     */
62
    public function findJobRows(string $jobId): ActiveQuery
63
    {
64
        return static::find()->andWhere([$this->jobColumnName => $jobId]);
65
    }
66
67
    /**
68
     * Find all records being locked for this process
69
     * @param string $processId
70
     * @return ActiveQuery
71
     */
72
    public function findProcessRows(string $processId): ActiveQuery
73
    {
74
        return static::find()->andWhere([$this->processColumnName => $processId]);
75
    }
76
77
    /**
78
     * Clear all records of any processing status
79
     * @return int
80
     */
81
    public function clearAllProcesses(): int
82
    {
83
        $db = \Yii::$app->db;
84
        $setValues = [
85
            $this->processColumnName => new Expression('NULL'),
86
            $this->jobColumnName => new Expression('NULL'),
87
        ];
88
89
        $query = $db->createCommand()
90
            ->update(static::tableName(), $setValues);
91
        return $query->execute();
92
    }
93
}
94