1 | <?php |
||
22 | class Db extends AbstractQueue |
||
23 | { |
||
24 | /** |
||
25 | * Status when the job is ready. |
||
26 | */ |
||
27 | const STATUS_READY = 0; |
||
28 | |||
29 | /** |
||
30 | * Status when the job is running by the worker. |
||
31 | */ |
||
32 | const STATUS_ACTIVE = 1; |
||
33 | |||
34 | /** |
||
35 | * Status when the job is deleted. |
||
36 | */ |
||
37 | const STATUS_DELETED = 2; |
||
38 | |||
39 | /** |
||
40 | * The database used for the queue. |
||
41 | * |
||
42 | * This will use default `db` component from Craft application. |
||
43 | * @var string|\yii\db\Connection |
||
44 | */ |
||
45 | public $db = 'db'; |
||
46 | |||
47 | /** |
||
48 | * The name of the table to store the queue. |
||
49 | * |
||
50 | * The table should be pre-created as follows for MySQL: |
||
51 | * |
||
52 | * ```php |
||
53 | * CREATE TABLE queue ( |
||
54 | * id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT, |
||
55 | * status TINYINT NOT NULL DEFAULT 0, |
||
56 | * timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, |
||
57 | * data LONGBLOB |
||
58 | * ); |
||
59 | * ``` |
||
60 | * @var string |
||
61 | */ |
||
62 | public $tableName = '{{%queue}}'; |
||
63 | |||
64 | /** |
||
65 | * Whether to do hard delete of the deleted job, instead of just flagging the |
||
66 | * status. |
||
67 | * @var boolean |
||
68 | */ |
||
69 | public $hardDelete = true; |
||
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | */ |
||
74 | public function init() |
||
79 | |||
80 | /** |
||
81 | * @inheritdoc |
||
82 | */ |
||
83 | protected function fetchJob() |
||
100 | |||
101 | /** |
||
102 | * Fetch latest ready job from the table. |
||
103 | * |
||
104 | * Due to the use of AUTO_INCREMENT ID, this will fetch the job with the |
||
105 | * largest ID. |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | protected function fetchLatestRow() |
||
119 | |||
120 | /** |
||
121 | * Flag a row as running. This will update the row ID and status if ready. |
||
122 | * |
||
123 | * @param array $row The row to update. |
||
124 | * @return boolean Whether successful or not. |
||
125 | */ |
||
126 | protected function flagRunningRow(array $row) |
||
139 | |||
140 | /** |
||
141 | * @inheritdoc |
||
142 | */ |
||
143 | protected function postJob(JobInterface $job, array $options = []): bool |
||
144 | { |
||
145 | return $this->db->createCommand()->insert( |
||
146 | $this->tableName, |
||
147 | [ |
||
148 | 'timestamp' => new Expression('NOW()'), |
||
149 | 'data' => $this->serialize($job), |
||
150 | ] |
||
151 | )->execute() == 1; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @inheritdoc |
||
156 | */ |
||
157 | public function deleteJob(JobInterface $job): bool |
||
172 | |||
173 | /** |
||
174 | * @inheritdoc |
||
175 | */ |
||
176 | public function releaseJob(JobInterface $job): bool |
||
184 | |||
185 | /** |
||
186 | * @inheritdoc |
||
187 | */ |
||
188 | public function getSize(): int |
||
196 | |||
197 | /** |
||
198 | * @inheritdoc |
||
199 | */ |
||
200 | public function purge(): bool |
||
204 | } |
||
205 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.