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