Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.67% covered (warning)
78.67%
118 / 150
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
OnContinuouslyCommand
78.67% covered (warning)
78.67%
118 / 150
50.00% covered (danger)
50.00%
2 / 4
9.79
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
75.41% covered (warning)
75.41%
92 / 122
0.00% covered (danger)
0.00%
0 / 1
1.01
 daily
88.24% covered (warning)
88.24%
15 / 17
0.00% covered (danger)
0.00%
0 / 1
6.06
 getTimeOnlyDiffSeconds
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
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        $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        // TODO: Remove this again!
72        $this->daily('01:25:00', 'send-test-email', function () use ($output) {
73            $this->symfonyUtils()->callCommand(
74                'olz:send-test-email',
75                new ArrayInput([]),
76                $output,
77            );
78        });
79
80        $this->daily('08:15:00', 'send-weekly-summary', function () use ($output) {
81            $this->symfonyUtils()->callCommand(
82                'olz:send-weekly-summary',
83                new ArrayInput([]),
84                $output,
85            );
86        });
87
88        $this->daily('14:30:00', 'send-monthly-preview', function () use ($output) {
89            $this->symfonyUtils()->callCommand(
90                'olz:send-monthly-preview',
91                new ArrayInput([]),
92                $output,
93            );
94        });
95
96        $this->daily('15:14:00', 'send-weekly-preview', function () use ($output) {
97            $this->symfonyUtils()->callCommand(
98                'olz:send-weekly-preview',
99                new ArrayInput([]),
100                $output,
101            );
102        });
103
104        $this->daily('16:27:00', 'send-deadline-warning', function () use ($output) {
105            $this->symfonyUtils()->callCommand(
106                'olz:send-deadline-warning',
107                new ArrayInput([]),
108                $output,
109            );
110        });
111
112        $this->daily('17:30:00', 'send-daily-summary', function () use ($output) {
113            $this->symfonyUtils()->callCommand(
114                'olz:send-daily-summary',
115                new ArrayInput([]),
116                $output,
117            );
118        });
119
120        $this->daily('18:30:00', 'send-reminders', function () use ($output) {
121            $this->symfonyUtils()->callCommand(
122                'olz:send-email-config-reminder',
123                new ArrayInput([]),
124                $output,
125            );
126            $this->symfonyUtils()->callCommand(
127                'olz:send-role-reminder',
128                new ArrayInput([]),
129                $output,
130            );
131            $this->symfonyUtils()->callCommand(
132                'olz:send-telegram-config-reminder',
133                new ArrayInput([]),
134                $output,
135            );
136        });
137
138        $this->logAndOutput("Stopping workers...", level: 'debug');
139        $this->symfonyUtils()->callCommand(
140            'messenger:stop-workers',
141            new ArrayInput([]),
142            $output,
143        );
144        $this->logAndOutput("Consume messages...", level: 'debug');
145        $this->symfonyUtils()->callCommand(
146            'messenger:consume',
147            new ArrayInput([
148                'receivers' => ['async'],
149                '--no-reset' => '--no-reset',
150            ]),
151            $output,
152        );
153
154        $this->logAndOutput("Ran continuously.", level: 'debug');
155        return Command::SUCCESS;
156    }
157
158    /** @param callable(): void $fn */
159    public function daily(string $time, string $ident, callable $fn): void {
160        $throttling_repo = $this->entityManager()->getRepository(Throttling::class);
161        $last_occurrence = $throttling_repo->getLastOccurrenceOf($ident);
162        $now = new \DateTime($this->dateUtils()->getIsoNow());
163        $is_too_soon = false;
164        if ($last_occurrence) {
165            // Consider daylight saving change date => not 23 hours!
166            $min_interval = \DateInterval::createFromDateString('+22 hours');
167            $min_now = $last_occurrence->add($min_interval);
168            $is_too_soon = $now < $min_now;
169        }
170        $time_diff = $this->getTimeOnlyDiffSeconds($now->format('H:i:s'), $time);
171        $is_right_time_of_day = $time_diff >= 0 && $time_diff < 7200; // 2h window
172        $should_execute_now = !$is_too_soon && $is_right_time_of_day;
173        if ($should_execute_now) {
174            try {
175                $this->logAndOutput("Executing daily ({$time}{$ident}...", level: 'info');
176                $fn();
177                $throttling_repo->recordOccurrenceOf($ident, $this->dateUtils()->getIsoNow());
178            } catch (\Throwable $th) {
179                $this->logAndOutput("Daily ({$time}{$ident} failed", level: 'error');
180            }
181        }
182    }
183
184    public function getTimeOnlyDiffSeconds(string $iso_value, string $iso_cmp): int {
185        date_default_timezone_set('Europe/Zurich');
186        $value = new \DateTime($iso_value);
187        $cmp = new \DateTime($iso_cmp);
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}