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.
96 lines
4.2 KiB
PHP
96 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudServicePeriod;
|
|
use App\Models\CloudTrafficEntitlement;
|
|
use App\Models\CloudTrafficInstanceCursor;
|
|
use App\Models\CloudTrafficPool;
|
|
use App\Models\CloudTrafficPoolGroup;
|
|
use App\Models\CloudTrafficScanFailure;
|
|
|
|
/** Retire a particular scan failure only after its subject is provably settled. */
|
|
final class CloudTrafficScanRecovery
|
|
{
|
|
public static function generation(CloudTrafficScanFailure $failure): array
|
|
{
|
|
return $failure->only(['id', 'scan_kind', 'subject_id', 'attempt_count', 'last_failed_at',
|
|
'first_failed_at', 'last_error_code', 'retry_at']);
|
|
}
|
|
|
|
public static function generations(string $kind, array $ids): array
|
|
{
|
|
return CloudTrafficScanFailure::where('scan_kind', $kind)->whereIn('subject_id', $ids)
|
|
->get()->mapWithKeys(static fn ($row): array => [$row->subject_id => self::generation($row)])->all();
|
|
}
|
|
|
|
public static function resolved(array $generation, int $now): bool
|
|
{
|
|
$id = $generation['subject_id'];
|
|
if ($generation['scan_kind'] === 'instance') {
|
|
$instance = CloudInstance::find($id);
|
|
$cursor = CloudTrafficInstanceCursor::find($id);
|
|
if (!$instance || !$cursor || $cursor->state !== 'ready' || (int) $instance->traffic_synced_at <= 0
|
|
|| $cursor->accounted_download_bytes !== (int) $instance->lifetime_download_bytes
|
|
|| $cursor->accounted_upload_bytes !== (int) $instance->lifetime_upload_bytes) {
|
|
return false;
|
|
}
|
|
$period = CloudTrafficPoolAccounting::periodForObservation($id, $instance->traffic_synced_at);
|
|
return $period && $period->user_id === $instance->user_id && $cursor->user_id === $instance->user_id
|
|
&& self::healthyPool($cursor->pool_id, $period);
|
|
}
|
|
if ($generation['scan_kind'] !== 'period') {
|
|
return false;
|
|
}
|
|
$period = CloudServicePeriod::find($id);
|
|
$entitlement = CloudTrafficEntitlement::where('service_period_id', $id)->first();
|
|
if (!$period || !$entitlement || $entitlement->starts_at !== $period->starts_at
|
|
|| $entitlement->ends_at !== $period->ends_at || $entitlement->instance_id !== $period->instance_id
|
|
|| $entitlement->user_id !== $period->user_id || $entitlement->quota_mode !== $period->traffic_quota_mode
|
|
|| $entitlement->granted_bytes !== $period->traffic_quota_bytes
|
|
|| !self::healthyPool($entitlement->pool_id, $period)) {
|
|
return false;
|
|
}
|
|
return match ($entitlement->state) {
|
|
'scheduled' => $now < $period->starts_at,
|
|
'active', 'exhausted' => $period->starts_at <= $now && $now < $period->ends_at,
|
|
'closing' => $period->ends_at <= $now && $now < $entitlement->closed_at,
|
|
'expired' => $period->ends_at <= $now,
|
|
default => false,
|
|
};
|
|
}
|
|
|
|
private static function healthyPool(int $poolId, CloudServicePeriod $period): bool
|
|
{
|
|
return CloudTrafficPool::whereKey($poolId)->where('user_id', $period->user_id)
|
|
->where('traffic_pool_group_id', $period->traffic_pool_group_id)
|
|
->where('state', '<>', 'attention')->exists()
|
|
&& CloudTrafficPoolGroup::whereKey($period->traffic_pool_group_id)
|
|
->whereIn('mode', ['shadow', 'enforced'])->exists();
|
|
}
|
|
|
|
public static function clearIfResolved(?array $generation, int $now): bool
|
|
{
|
|
if (!$generation) {
|
|
return false;
|
|
}
|
|
$connection = (new CloudTrafficScanFailure())->getConnection();
|
|
$connection->beginTransaction();
|
|
try {
|
|
// Claim this exact generation before taking a fresh view of its subject.
|
|
// An unresolved sample rolls the deletion back before it becomes visible.
|
|
if (CloudTrafficScanFailure::where($generation)->delete() !== 1
|
|
|| !self::resolved($generation, $now)) {
|
|
$connection->rollBack();
|
|
return false;
|
|
}
|
|
$connection->commit();
|
|
return true;
|
|
} catch (\Throwable $error) {
|
|
$connection->rollBack();
|
|
throw $error;
|
|
}
|
|
}
|
|
}
|