Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
30 / 30 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
StuckTaskQuery | |
100.00% |
30 / 30 |
|
100.00% |
6 / 6 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
3 | |||
getQueryName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getWhitelist | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getStatuses | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAge | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAgeDateTime | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2021 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\taoTaskQueue\model\Repository; |
24 | |
25 | use DateTimeImmutable; |
26 | use DateTimeZone; |
27 | use Doctrine\Common\Cache\Psr6\InvalidArgument; |
28 | use oat\tao\model\taskQueue\TaskLog; |
29 | |
30 | class StuckTaskQuery |
31 | { |
32 | private const ALLOWED_STATUSES = [ |
33 | TaskLog::STATUS_ENQUEUED, |
34 | ]; |
35 | |
36 | /** @var string */ |
37 | private $queryName; |
38 | |
39 | /** @var array */ |
40 | private $whitelist; |
41 | |
42 | /** @var int */ |
43 | private $age; |
44 | |
45 | /** @var DateTimeImmutable */ |
46 | private $ageDateTime; |
47 | |
48 | /** @var array */ |
49 | private $statuses; |
50 | |
51 | public function __construct(string $queryName, array $whitelist, array $statuses, int $age) |
52 | { |
53 | array_walk( |
54 | $whitelist, |
55 | function (&$value) { |
56 | $value = trim($value); |
57 | } |
58 | ); |
59 | |
60 | $whitelist = array_filter($whitelist); |
61 | |
62 | if (empty($whitelist)) { |
63 | throw new InvalidArgument('Stuck tasks names white list cannot be empty'); |
64 | } |
65 | |
66 | if (count(array_intersect($statuses, self::ALLOWED_STATUSES)) !== count($statuses)) { |
67 | throw new InvalidArgument( |
68 | sprintf( |
69 | 'Only allowed statuses "%s" for stuck tasks. Provided: "%s"', |
70 | implode(',', self::ALLOWED_STATUSES), |
71 | implode(',', $statuses) |
72 | ) |
73 | ); |
74 | } |
75 | |
76 | $this->queryName = $queryName; |
77 | $this->whitelist = $whitelist; |
78 | $this->age = max($age, StuckTaskRepository::MIN_AGE); |
79 | $this->statuses = $statuses; |
80 | |
81 | /** |
82 | * Simulating approach provided at common_persistence_sql_Platform::getNowExpression() |
83 | * This is how we create new taskLogs in the system. |
84 | * |
85 | * @TODO Refactor this when we will have support for other Queue and TaskLog broker |
86 | * |
87 | * Task: https://oat-sa.atlassian.net/browse/ADF-556 |
88 | */ |
89 | $this->ageDateTime = new DateTimeImmutable( |
90 | sprintf('now -%s seconds', $this->age), |
91 | new DateTimeZone('UTC') |
92 | ); |
93 | } |
94 | |
95 | public function getQueryName(): string |
96 | { |
97 | return $this->queryName; |
98 | } |
99 | |
100 | public function getWhitelist(): array |
101 | { |
102 | return $this->whitelist; |
103 | } |
104 | |
105 | public function getStatuses(): array |
106 | { |
107 | return $this->statuses; |
108 | } |
109 | |
110 | public function getAge() |
111 | { |
112 | return $this->age; |
113 | } |
114 | |
115 | public function getAgeDateTime(): DateTimeImmutable |
116 | { |
117 | return $this->ageDateTime; |
118 | } |
119 | } |