You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Command/CloudAbuseMonitor.php

148 lines
5.1 KiB
PHP

<?php
namespace App\Command;
use App\Services\Cloud\CloudAbuseObservationIngestService;
use App\Services\Cloud\CloudAbuseRetentionService;
use App\Services\Cloud\CloudRuntimeHeartbeat;
/**
* Independent, observe-only network-abuse monitor.
*
* Collection deliberately stays outside cloud_jobs: every host call is
* read-only and every database write is an atomic telemetry/read-model
* update. No lifecycle, billing, firewall or task mutation belongs here.
*/
final class CloudAbuseMonitor
{
private const RETENTION_INTERVAL_SECONDS = 300;
private const IDLE_SLEEP_MICROSECONDS = 1_000_000;
/**
* Injectable objects and clock keep the command testable without turning
* the production services into interfaces solely for the command layer.
*
* @param null|object $ingest must expose tick() and observationErrors()
* @param null|object $retention must expose prune()
* @param null|callable():int $clock
* @param null|callable(int):void $sleep
*/
public static function run(
array $argv = [],
?CloudRuntimeHeartbeat $heartbeat = null,
?object $ingest = null,
?object $retention = null,
?callable $clock = null,
?callable $sleep = null
): int {
$once = in_array('--once', $argv, true);
$heartbeat ??= CloudRuntimeHeartbeat::start(
'abuse_monitor',
$once
);
$ingest ??= new CloudAbuseObservationIngestService();
$retention ??= new CloudAbuseRetentionService();
$clock ??= static fn (): int => time();
$sleep ??= static function (int $microseconds): void {
usleep($microseconds);
};
self::assertWorker($ingest, ['tick', 'observationErrors']);
self::assertWorker($retention, ['prune']);
$nextRetentionAt = 0;
$unresolved = [
'collection' => '',
'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);
}
$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<string> $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
);
}
}