Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 149 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| OnContinuouslyCommand | |
0.00% |
0 / 149 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
| getAllowedAppEnvs | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handle | |
0.00% |
0 / 115 |
|
0.00% |
0 / 1 |
2 | |||
| daily | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
72 | |||
| getTimeOnlyDiffSeconds | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Command; |
| 4 | |
| 5 | use Olz\Command\Common\OlzCommand; |
| 6 | use Olz\Entity\Throttling; |
| 7 | use Symfony\Component\Console\Attribute\AsCommand; |
| 8 | use Symfony\Component\Console\Command\Command; |
| 9 | use Symfony\Component\Console\Input\ArrayInput; |
| 10 | use Symfony\Component\Console\Input\InputInterface; |
| 11 | use Symfony\Component\Console\Output\OutputInterface; |
| 12 | |
| 13 | #[AsCommand(name: 'olz:on-continuously')] |
| 14 | class OnContinuouslyCommand extends OlzCommand { |
| 15 | /** @return array<string> */ |
| 16 | protected function getAllowedAppEnvs(): array { |
| 17 | return ['dev', 'test', 'staging', 'prod']; |
| 18 | } |
| 19 | |
| 20 | protected function handle(InputInterface $input, OutputInterface $output): int { |
| 21 | set_time_limit(4000); |
| 22 | ignore_user_abort(true); |
| 23 | |
| 24 | $this->logAndOutput("Running continuously...", level: 'debug'); |
| 25 | $throttling_repo = $this->entityManager()->getRepository(Throttling::class); |
| 26 | $throttling_repo->recordOccurrenceOf('on_continuously', $this->dateUtils()->getIsoNow()); |
| 27 | |
| 28 | $this->logAndOutput("Continuously processing email...", level: 'debug'); |
| 29 | $this->symfonyUtils()->callCommand( |
| 30 | 'olz:process-email', |
| 31 | new ArrayInput([]), |
| 32 | $output, |
| 33 | ); |
| 34 | |
| 35 | $this->daily('01:00:00', 'clean-temp-directory', function () use ($output) { |
| 36 | $this->symfonyUtils()->callCommand( |
| 37 | 'olz:clean-temp-directory', |
| 38 | new ArrayInput([]), |
| 39 | $output, |
| 40 | ); |
| 41 | }); |
| 42 | $this->daily('01:05:00', 'clean-temp-database', function () use ($output) { |
| 43 | $this->symfonyUtils()->callCommand( |
| 44 | 'olz:clean-temp-database', |
| 45 | new ArrayInput([]), |
| 46 | $output, |
| 47 | ); |
| 48 | }); |
| 49 | $this->daily('01:10:00', 'clean-logs', function () use ($output) { |
| 50 | $this->symfonyUtils()->callCommand( |
| 51 | 'olz:clean-logs', |
| 52 | new ArrayInput([]), |
| 53 | $output, |
| 54 | ); |
| 55 | }); |
| 56 | $this->daily('01:15:00', 'send-telegram-configuration', function () use ($output) { |
| 57 | $this->symfonyUtils()->callCommand( |
| 58 | 'olz:send-telegram-configuration', |
| 59 | new ArrayInput([]), |
| 60 | $output, |
| 61 | ); |
| 62 | }); |
| 63 | $this->daily('01:20:00', 'sync-solv', function () use ($output) { |
| 64 | $this->symfonyUtils()->callCommand( |
| 65 | 'olz:sync-solv', |
| 66 | new ArrayInput([]), |
| 67 | $output, |
| 68 | ); |
| 69 | }); |
| 70 | |
| 71 | $this->daily('08:15:00', 'send-weekly-summary', function () use ($output) { |
| 72 | $this->symfonyUtils()->callCommand( |
| 73 | 'olz:send-weekly-summary', |
| 74 | new ArrayInput([]), |
| 75 | $output, |
| 76 | ); |
| 77 | }); |
| 78 | |
| 79 | $this->daily('14:30:00', 'send-monthly-preview', function () use ($output) { |
| 80 | $this->symfonyUtils()->callCommand( |
| 81 | 'olz:send-monthly-preview', |
| 82 | new ArrayInput([]), |
| 83 | $output, |
| 84 | ); |
| 85 | }); |
| 86 | |
| 87 | $this->daily('15:14:00', 'send-weekly-preview', function () use ($output) { |
| 88 | $this->symfonyUtils()->callCommand( |
| 89 | 'olz:send-weekly-preview', |
| 90 | new ArrayInput([]), |
| 91 | $output, |
| 92 | ); |
| 93 | }); |
| 94 | |
| 95 | $this->daily('16:27:00', 'send-deadline-warning', function () use ($output) { |
| 96 | $this->symfonyUtils()->callCommand( |
| 97 | 'olz:send-deadline-warning', |
| 98 | new ArrayInput([]), |
| 99 | $output, |
| 100 | ); |
| 101 | }); |
| 102 | |
| 103 | $this->daily('17:30:00', 'send-daily-summary', function () use ($output) { |
| 104 | $this->symfonyUtils()->callCommand( |
| 105 | 'olz:send-daily-summary', |
| 106 | new ArrayInput([]), |
| 107 | $output, |
| 108 | ); |
| 109 | }); |
| 110 | |
| 111 | $this->daily('18:30:00', 'send-reminders', function () use ($output) { |
| 112 | $this->symfonyUtils()->callCommand( |
| 113 | 'olz:send-email-config-reminder', |
| 114 | new ArrayInput([]), |
| 115 | $output, |
| 116 | ); |
| 117 | $this->symfonyUtils()->callCommand( |
| 118 | 'olz:send-role-reminder', |
| 119 | new ArrayInput([]), |
| 120 | $output, |
| 121 | ); |
| 122 | $this->symfonyUtils()->callCommand( |
| 123 | 'olz:send-telegram-config-reminder', |
| 124 | new ArrayInput([]), |
| 125 | $output, |
| 126 | ); |
| 127 | }); |
| 128 | |
| 129 | $this->logAndOutput("Stopping workers...", level: 'debug'); |
| 130 | $this->symfonyUtils()->callCommand( |
| 131 | 'messenger:stop-workers', |
| 132 | new ArrayInput([]), |
| 133 | $output, |
| 134 | ); |
| 135 | $this->logAndOutput("Consume messages...", level: 'debug'); |
| 136 | $this->symfonyUtils()->callCommand( |
| 137 | 'messenger:consume', |
| 138 | new ArrayInput([ |
| 139 | 'receivers' => ['async'], |
| 140 | '--no-reset' => '--no-reset', |
| 141 | ]), |
| 142 | $output, |
| 143 | ); |
| 144 | |
| 145 | $this->logAndOutput("Ran continuously.", level: 'debug'); |
| 146 | return Command::SUCCESS; |
| 147 | } |
| 148 | |
| 149 | /** @param callable(): void $fn */ |
| 150 | public function daily(string $time, string $ident, callable $fn): void { |
| 151 | $throttling_repo = $this->entityManager()->getRepository(Throttling::class); |
| 152 | $last_occurrence = $throttling_repo->getLastOccurrenceOf($ident); |
| 153 | $now = new \DateTime($this->dateUtils()->getIsoNow()); |
| 154 | $is_too_soon = false; |
| 155 | if ($last_occurrence) { |
| 156 | // Consider daylight saving change date => not 23 hours! |
| 157 | $min_interval = \DateInterval::createFromDateString('+22 hours'); |
| 158 | $min_now = $last_occurrence->add($min_interval); |
| 159 | $is_too_soon = $now < $min_now; |
| 160 | } |
| 161 | $time_diff = $this->getTimeOnlyDiffSeconds($now->format('H:i:s'), $time); |
| 162 | $is_right_time_of_day = $time_diff >= 0 && $time_diff < 7200; // 2h window |
| 163 | $should_execute_now = !$is_too_soon && $is_right_time_of_day; |
| 164 | if ($should_execute_now) { |
| 165 | try { |
| 166 | $this->logAndOutput("Executing daily ({$time}) {$ident}...", level: 'info'); |
| 167 | $fn(); |
| 168 | $throttling_repo->recordOccurrenceOf($ident, $this->dateUtils()->getIsoNow()); |
| 169 | } catch (\Throwable $th) { |
| 170 | $this->logAndOutput("Daily ({$time}) {$ident} failed", level: 'error'); |
| 171 | } |
| 172 | } else { |
| 173 | $pretty_reasons = []; |
| 174 | if ($is_too_soon) { |
| 175 | $pretty_reasons[] = 'too soon'; |
| 176 | } |
| 177 | if (!$is_right_time_of_day) { |
| 178 | $pretty_reasons[] = "not the right time (diff: {$time_diff})"; |
| 179 | } |
| 180 | $pretty_reason = implode(", ", $pretty_reasons); |
| 181 | $this->log()->debug("Not executing daily ({$time}) {$ident}: {$pretty_reason}"); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | public function getTimeOnlyDiffSeconds(string $iso_value, string $iso_cmp): int { |
| 186 | $value = new \DateTime($iso_value, new \DateTimeZone('UTC')); |
| 187 | $cmp = new \DateTime($iso_cmp, new \DateTimeZone('UTC')); |
| 188 | |
| 189 | $seconds_diff = $value->getTimestamp() - $cmp->getTimestamp(); |
| 190 | $time_only_diff = $seconds_diff % 86400; |
| 191 | return match (true) { |
| 192 | $time_only_diff < -43200 => $time_only_diff + 86400, |
| 193 | $time_only_diff >= 43200 => $time_only_diff - 86400, |
| 194 | default => $time_only_diff, |
| 195 | }; |
| 196 | } |
| 197 | } |