Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | n/a |
0 / 0 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
|||
| Cron | n/a |
0 / 0 |
n/a |
0 / 0 |
30 | n/a |
0 / 0 |
|||
| create | n/a |
0 / 0 |
n/a |
0 / 0 |
3 | |||||
| update | n/a |
0 / 0 |
n/a |
0 / 0 |
6 | |||||
| delete | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| deleteByName | n/a |
0 / 0 |
n/a |
0 / 0 |
6 | |||||
| getAll | n/a |
0 / 0 |
n/a |
0 / 0 |
7 | |||||
| getAllByName | n/a |
0 / 0 |
n/a |
0 / 0 |
6 | |||||
| reload | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Utils\TaskSchedule |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Utils\TaskSchedule; |
| 16 | |
| 17 | use phpOMS\System\SystemUtils; |
| 18 | |
| 19 | /** |
| 20 | * Cron class. |
| 21 | * |
| 22 | * @package phpOMS\Utils\TaskSchedule |
| 23 | * @license OMS License 2.0 |
| 24 | * @link https://jingga.app |
| 25 | * @since 1.0.0 |
| 26 | * @codeCoverageIgnore |
| 27 | */ |
| 28 | class Cron extends SchedulerAbstract |
| 29 | { |
| 30 | /** |
| 31 | * {@inheritdoc} |
| 32 | * |
| 33 | * @throws \Exception |
| 34 | */ |
| 35 | public function create(TaskAbstract $task) : void |
| 36 | { |
| 37 | $path = \tempnam(\sys_get_temp_dir(), 'cron_'); |
| 38 | if ($path === false) { |
| 39 | throw new \Exception(); |
| 40 | } |
| 41 | |
| 42 | if (!empty($this->getAllByName($task->getId()))) { |
| 43 | \unlink($path); |
| 44 | |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | $this->run('-l > ' . $path); |
| 49 | \file_put_contents($path, $task->__toString() . "\n", \FILE_APPEND); |
| 50 | $this->run($path); |
| 51 | \unlink($path); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * {@inheritdoc} |
| 56 | * |
| 57 | * @throws \Exception |
| 58 | */ |
| 59 | public function update(TaskAbstract $task) : void |
| 60 | { |
| 61 | $path = \tempnam(\sys_get_temp_dir(), 'cron_'); |
| 62 | if ($path === false) { |
| 63 | throw new \Exception(); |
| 64 | } |
| 65 | |
| 66 | $this->run('-l > ' . $path); |
| 67 | |
| 68 | $new = ''; |
| 69 | $fp = \fopen($path, 'r+'); |
| 70 | |
| 71 | if ($fp) { |
| 72 | $line = \fgets($fp); |
| 73 | while ($line !== false) { |
| 74 | if ($line[0] !== '#' && \stripos($line, 'name="' . $task->getId()) !== false) { |
| 75 | $new .= $task->__toString() . "\n"; |
| 76 | } else { |
| 77 | $new .= $line; |
| 78 | } |
| 79 | |
| 80 | $line = \fgets($fp); |
| 81 | } |
| 82 | |
| 83 | \fclose($fp); |
| 84 | \file_put_contents($path, $new); |
| 85 | } |
| 86 | |
| 87 | $this->run($path); |
| 88 | \unlink($path); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * {@inheritdoc} |
| 93 | */ |
| 94 | public function delete(TaskAbstract $task) : void |
| 95 | { |
| 96 | $this->deleteByName($task->getId()); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * {@inheritdoc} |
| 101 | * |
| 102 | * @throws \Exception |
| 103 | */ |
| 104 | public function deleteByName(string $name) : void |
| 105 | { |
| 106 | $path = \tempnam(\sys_get_temp_dir(), 'cron_'); |
| 107 | if ($path === false) { |
| 108 | throw new \Exception(); |
| 109 | } |
| 110 | |
| 111 | $this->run('-l > ' . $path); |
| 112 | |
| 113 | $new = ''; |
| 114 | $fp = \fopen($path, 'r+'); |
| 115 | |
| 116 | if ($fp) { |
| 117 | $line = \fgets($fp); |
| 118 | while ($line !== false) { |
| 119 | if ($line[0] !== '#' && \stripos($line, 'name="' . $name) !== false) { |
| 120 | $line = \fgets($fp); |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | $new .= $line; |
| 125 | $line = \fgets($fp); |
| 126 | } |
| 127 | |
| 128 | \fclose($fp); |
| 129 | \file_put_contents($path, $new); |
| 130 | } |
| 131 | |
| 132 | $this->run($path); |
| 133 | \unlink($path); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * {@inheritdoc} |
| 138 | * |
| 139 | * @throws \Exception |
| 140 | */ |
| 141 | public function getAll() : array |
| 142 | { |
| 143 | $path = \tempnam(\sys_get_temp_dir(), 'cron_'); |
| 144 | if ($path === false) { |
| 145 | throw new \Exception(); |
| 146 | } |
| 147 | |
| 148 | $this->run('-l > ' . $path); |
| 149 | |
| 150 | $jobs = []; |
| 151 | $fp = \fopen($path, 'r+'); |
| 152 | |
| 153 | if ($fp) { |
| 154 | $line = \fgets($fp); |
| 155 | while ($line !== false) { |
| 156 | if ($line[0] !== '#') { |
| 157 | $elements = []; |
| 158 | $namePos = \stripos($line, 'name="'); |
| 159 | $nameEndPos = \stripos($line, '"', $namePos + 7); |
| 160 | |
| 161 | if ($namePos !== false && $nameEndPos !== false) { |
| 162 | $elements[] = \substr($line, $namePos + 6, $nameEndPos - 1); |
| 163 | } |
| 164 | |
| 165 | $elements = \array_merge($elements, \explode(' ', $line)); |
| 166 | $jobs[] = CronJob::createWith($elements); |
| 167 | } |
| 168 | |
| 169 | $line = \fgets($fp); |
| 170 | } |
| 171 | |
| 172 | \fclose($fp); |
| 173 | } |
| 174 | |
| 175 | $this->run($path); |
| 176 | \unlink($path); |
| 177 | |
| 178 | return $jobs; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * {@inheritdoc} |
| 183 | * |
| 184 | * @throws \Exception |
| 185 | */ |
| 186 | public function getAllByName(string $name, bool $exact = true) : array |
| 187 | { |
| 188 | $path = \tempnam(\sys_get_temp_dir(), 'cron_'); |
| 189 | if ($path === false) { |
| 190 | throw new \Exception(); |
| 191 | } |
| 192 | |
| 193 | $this->run('-l > ' . $path); |
| 194 | |
| 195 | $jobs = []; |
| 196 | $fp = \fopen($path, 'r+'); |
| 197 | |
| 198 | if ($fp) { |
| 199 | $line = \fgets($fp); |
| 200 | while ($line !== false) { |
| 201 | if (($comment = \stripos($line, '# name="' . $name)) !== false) { |
| 202 | $interval = \array_slice(\explode(' ', $line), 0, 5); |
| 203 | |
| 204 | $elements = []; |
| 205 | $elements[] = \trim(\substr($line, $comment + 8, \stripos($line, '"', $comment + 9) - $comment - 8)); |
| 206 | $elements = \array_merge($elements, $interval); |
| 207 | $elements[] = \trim(\substr($line, $len = (\strlen(\implode(' ', $interval)) + 1), $comment - $len - 1)); |
| 208 | |
| 209 | $jobs[] = $job = CronJob::createWith($elements); |
| 210 | $job->setStatus($line[0] === '#' ? TaskStatus::INACTIVE : TaskStatus::ACTIVE); |
| 211 | } |
| 212 | |
| 213 | $line = \fgets($fp); |
| 214 | } |
| 215 | |
| 216 | \fclose($fp); |
| 217 | } |
| 218 | |
| 219 | $this->run($path); |
| 220 | \unlink($path); |
| 221 | |
| 222 | return $jobs; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * {@inheritdoc} |
| 227 | */ |
| 228 | public function reload() : void |
| 229 | { |
| 230 | SystemUtils::runProc('service', 'cron reload'); |
| 231 | } |
| 232 | } |