|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OpenRuntimes\Executor\Runner; |
| 4 | + |
| 5 | +use OpenRuntimes\Executor\Runner\Repository\Runtimes; |
| 6 | +use Swoole\Timer; |
| 7 | +use Utopia\Console; |
| 8 | +use Utopia\Orchestration\Orchestration; |
| 9 | +use Utopia\Storage\Device\Local; |
| 10 | +use Utopia\System\System; |
| 11 | + |
| 12 | +use function Swoole\Coroutine\batch; |
| 13 | + |
| 14 | +/** |
| 15 | + * Handles periodic cleanup of inactive runtimes and orphaned temp directories. |
| 16 | + */ |
| 17 | +class Maintenance |
| 18 | +{ |
| 19 | + private int|false $timerId = false; |
| 20 | + |
| 21 | + public function __construct( |
| 22 | + private Orchestration $orchestration, |
| 23 | + private Runtimes $runtimes |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Starts the maintenance loop. No-op if already running. |
| 29 | + */ |
| 30 | + public function start(int $intervalSeconds, int $inactiveSeconds): void |
| 31 | + { |
| 32 | + if ($this->timerId !== false) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + $intervalMs = $intervalSeconds * 1000; |
| 37 | + $this->timerId = Timer::tick($intervalMs, fn () => $this->doMaintenance($inactiveSeconds)); |
| 38 | + Console::info("[Maintenance] Started task on interval $intervalSeconds seconds."); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Stops the maintenance loop. No-op if already stopped. |
| 43 | + */ |
| 44 | + public function stop(): void |
| 45 | + { |
| 46 | + if ($this->timerId === false) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + Timer::clear($this->timerId); |
| 51 | + $this->timerId = false; |
| 52 | + Console::info("[Maintenance] Stopped task."); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Removes runtimes inactive beyond the threshold and cleans up temporary files. |
| 57 | + */ |
| 58 | + private function doMaintenance(int $inactiveSeconds): void |
| 59 | + { |
| 60 | + Console::info("[Maintenance] Running task with threshold $inactiveSeconds seconds."); |
| 61 | + |
| 62 | + $threshold = \time() - $inactiveSeconds; |
| 63 | + $candidates = array_filter( |
| 64 | + $this->runtimes->list(), |
| 65 | + fn ($runtime) => $runtime->updated < $threshold |
| 66 | + ); |
| 67 | + |
| 68 | + // Remove from in-memory state before removing the container. |
| 69 | + // Ensures availability, otherwise we would route requests to terminating runtimes. |
| 70 | + $keys = array_keys($candidates); |
| 71 | + foreach ($keys as $key) { |
| 72 | + $this->runtimes->remove($key); |
| 73 | + } |
| 74 | + // Then, remove forcefully terminate the associated running container. |
| 75 | + $jobs = array_map( |
| 76 | + fn ($candidate) => fn () => $this->orchestration->remove($candidate->name, force: true), |
| 77 | + $candidates |
| 78 | + ); |
| 79 | + $results = batch($jobs); |
| 80 | + $removed = \count(array_filter($results)); |
| 81 | + |
| 82 | + Console::info("[Maintenance] Removed {$removed}/" . \count($candidates) . " inactive runtimes."); |
| 83 | + |
| 84 | + $this->cleanupTmp(); |
| 85 | + } |
| 86 | + |
| 87 | + private function cleanupTmp(): void |
| 88 | + { |
| 89 | + $localDevice = new Local(); |
| 90 | + $tmpPath = DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR; |
| 91 | + $prefix = $tmpPath . System::getHostname() . '-'; |
| 92 | + |
| 93 | + foreach ($localDevice->getFiles($tmpPath) as $entry) { |
| 94 | + if (!\str_starts_with($entry, $prefix)) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + $runtimeName = substr($entry, \strlen($prefix)); |
| 99 | + if ($this->runtimes->exists($runtimeName)) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + if ($localDevice->deletePath($entry)) { |
| 104 | + Console::info("[Maintenance] Removed {$entry}."); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments