Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 143
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
OnContinuouslyCommand
0.00% covered (danger)
0.00%
0 / 143
0.00% covered (danger)
0.00%
0 / 4
90
0.00% covered (danger)
0.00%
0 / 1
 getAllowedAppEnvs
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 handle
0.00% covered (danger)
0.00%
0 / 115
0.00% covered (danger)
0.00%
0 / 1
2
 daily
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
42
 getTimeOnlyDiffSeconds
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
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        $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        }
173    }
174
175    public function getTimeOnlyDiffSeconds(string $iso_value, string $iso_cmp): int {
176        date_default_timezone_set('Europe/Zurich');
177        $value = new \DateTime($iso_value);
178        $cmp = new \DateTime($iso_cmp);
179
180        $seconds_diff = $value->getTimestamp() - $cmp->getTimestamp();
181        $time_only_diff = $seconds_diff % 86400;
182        return match (true) {
183            $time_only_diff < -43200 => $time_only_diff + 86400,
184            $time_only_diff >= 43200 => $time_only_diff - 86400,
185            default => $time_only_diff,
186        };
187    }
188}