time(); $sleep ??= static function (int $microseconds): void { usleep($microseconds); }; self::assertWorker($ingest, ['tick', 'observationErrors']); self::assertWorker($projection, ['tick', 'observationErrors']); self::assertWorker($retention, ['prune']); $nextRetentionAt = 0; $unresolved = [ 'collection' => '', 'projection' => '', 'retention' => '', ]; try { do { $worked = false; $heartbeat->busy('abuse_monitor_ingest'); try { $worked = $ingest->tick() === true || $worked; $unresolved['collection'] = $ingest->observationErrors() === [] ? '' : 'abuse_monitor_collection_failed'; } catch (\Throwable $error) { $unresolved['collection'] = 'abuse_monitor_collection_failed'; self::writeFailure('collection', $error); } $heartbeat->busy('abuse_monitor_projection'); try { $projectionFailed = false; for ($projected = 0; $projected < self::PROJECTION_BURST; $projected++ ) { if ($projection->tick() !== true) { break; } $worked = true; $projectionFailed = self::hasProjectionFailure( $projection->observationErrors() ) || $projectionFailed; } $unresolved['projection'] = $projectionFailed ? 'abuse_monitor_projection_failed' : ''; } catch (\Throwable $error) { $unresolved['projection'] = 'abuse_monitor_projection_failed'; self::writeFailure('projection', $error); } $now = $clock(); if (!is_int($now) || $now < 1) { throw new \LogicException( 'Abuse monitor command clock is invalid' ); } if ($now >= $nextRetentionAt) { $heartbeat->busy('abuse_monitor_retention'); try { $pruned = $retention->prune(); if (!is_array($pruned)) { throw new \RuntimeException( 'Abuse retention returned an invalid result' ); } $worked = array_sum(array_map( static fn (mixed $value): int => is_int($value) && $value > 0 ? $value : 0, $pruned )) > 0 || $worked; $unresolved['retention'] = ''; } catch (\Throwable $error) { $unresolved['retention'] = 'abuse_monitor_retention_failed'; self::writeFailure('retention', $error); } $nextRetentionAt = $now + self::RETENTION_INTERVAL_SECONDS; } $errors = array_values(array_filter($unresolved)); if ($errors !== []) { $heartbeat->error($errors[0]); } else { $heartbeat->success(); } if ($once) { return $errors === [] ? 0 : 1; } if (!$worked) { $sleep(self::IDLE_SLEEP_MICROSECONDS); } } while (true); } catch (\Throwable $error) { $heartbeat->error('abuse_monitor_runtime_failed'); throw $error; } finally { $heartbeat->stopped(); } } /** @param list $methods */ private static function assertWorker(object $worker, array $methods): void { foreach ($methods as $method) { if (!is_callable([$worker, $method])) { throw new \LogicException( 'Abuse monitor dependency is invalid' ); } } } private static function writeFailure( string $stage, \Throwable $error ): void { fwrite( STDERR, 'abuse monitor ' . $stage . ' temporarily unavailable: ' . get_class($error) . PHP_EOL ); } /** @param list $diagnostics */ private static function hasProjectionFailure(array $diagnostics): bool { foreach ($diagnostics as $diagnostic) { if (!is_string($diagnostic) || !in_array( $diagnostic, self::SAFE_PROJECTION_DIAGNOSTICS, true )) { return true; } } return false; } }