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.
144 lines
8.0 KiB
PHP
144 lines
8.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudVpc;
|
|
use App\Models\CloudVpcOperation;
|
|
|
|
/**
|
|
* Independent consumer for cloud_vpc_operations.
|
|
*
|
|
* This lane never claims cloud_jobs and never dispatches public
|
|
* network_change. CloudVpcService performs the VPC root/operation lease CAS;
|
|
* this class only finds work and supplies the internal trusted actor context.
|
|
*/
|
|
final class CloudVpcWorkerService
|
|
{
|
|
public function tick(): bool
|
|
{
|
|
$candidate = (new CloudVpcOperation())->getConnection()->transaction(
|
|
static function (): ?array {
|
|
$now = time();
|
|
$allowDeferredRetry = false;
|
|
$operation = CloudVpcOperation::where(function ($query) use ($now): void {
|
|
$query->where(function ($queued) use ($now): void {
|
|
$queued->where('status', 'queued')
|
|
->where('updated_at', '<=', $now - 2);
|
|
})->orWhere(function ($uncertain) use ($now): void {
|
|
// Transport ambiguity is retryable.
|
|
$uncertain->where('status', 'uncertain')
|
|
->where('updated_at', '<=', $now - 30);
|
|
})->orWhere(function ($waiting) use ($now): void {
|
|
// The Fabric gate may complete before the host
|
|
// private0/route/nft readback. Retry this same
|
|
// VPC operation on its own lane after a quiet
|
|
// period; it never enters network_change.
|
|
$waiting->where('status', 'waiting_observed')
|
|
->where('updated_at', '<=', $now - 30);
|
|
})->orWhere(function ($running) use ($now): void {
|
|
$running->where('status', 'running')
|
|
->where('lease_until', '<=', $now);
|
|
})->orWhere(function ($legacy) use ($now): void {
|
|
// A rolling upgrade must not keep a previously
|
|
// `succeeded` host/libvirt-only receipt usable.
|
|
// Re-run only the root's current last operation;
|
|
// historical audit rows are never replayed.
|
|
$legacy->where('status', 'succeeded')
|
|
->where('kind', '!=', 'vpc_delete')
|
|
->where('evidence_version', '<',
|
|
CloudVpcService::OBSERVATION_EVIDENCE_VERSION)
|
|
->where('lease_until', '<=', $now)
|
|
->where('updated_at', '<=', $now - 2)
|
|
->whereExists(static function ($current): void {
|
|
$current->selectRaw('1')
|
|
->from('cloud_vpcs as current_vpc')
|
|
->whereColumn('current_vpc.id',
|
|
'cloud_vpc_operations.vpc_id')
|
|
->whereColumn('current_vpc.last_operation_id',
|
|
'cloud_vpc_operations.operation_id')
|
|
->whereColumn('current_vpc.desired_revision',
|
|
'cloud_vpc_operations.desired_revision')
|
|
->where('current_vpc.state', '!=', 'deleted');
|
|
});
|
|
})->orWhere(function ($runtime) use ($now): void {
|
|
// Configuration may already be committed while one
|
|
// or more live guests are not yet observable. Keep
|
|
// the mutation terminal, but idempotently re-apply
|
|
// only the current root target until runtime proof
|
|
// is exact.
|
|
$runtime->where('status', 'succeeded')
|
|
->where('kind', '!=', 'vpc_delete')
|
|
->where('evidence_version', '>=',
|
|
CloudVpcService::OBSERVATION_EVIDENCE_VERSION)
|
|
->where('lease_until', '<=', $now)
|
|
->where('updated_at', '<=', $now - 30)
|
|
->whereExists(static function ($current): void {
|
|
$current->selectRaw('1')
|
|
->from('cloud_vpcs as current_vpc')
|
|
->whereColumn('current_vpc.id',
|
|
'cloud_vpc_operations.vpc_id')
|
|
->whereColumn('current_vpc.last_operation_id',
|
|
'cloud_vpc_operations.operation_id')
|
|
->whereColumn('current_vpc.desired_revision',
|
|
'cloud_vpc_operations.desired_revision')
|
|
->whereColumn('current_vpc.observed_revision',
|
|
'current_vpc.desired_revision')
|
|
->whereColumn('current_vpc.observed_digest',
|
|
'current_vpc.desired_digest')
|
|
->where('current_vpc.state', 'degraded')
|
|
->where(static function ($runtimeState): void {
|
|
$runtimeState->whereIn(
|
|
'current_vpc.sync_state',
|
|
['waiting', 'uncertain']
|
|
)->orWhere(static function ($stopped): void {
|
|
$stopped->where(
|
|
'current_vpc.sync_state', 'applied'
|
|
)->whereIn(
|
|
'current_vpc.last_error_code', [
|
|
'vpc_private_guest_shutdown_pending',
|
|
'vpc_private_guest_stopped',
|
|
]
|
|
);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
})
|
|
->orderBy('id')->lockForUpdate()->first();
|
|
if ($operation === null) {
|
|
return null;
|
|
}
|
|
$allowDeferredRetry = (string) $operation->status === 'waiting_observed'
|
|
|| (string) $operation->status === 'succeeded';
|
|
$vpc = CloudVpc::where('id', (int) $operation->vpc_id)->first();
|
|
if ($vpc === null) {
|
|
$operation->status = 'failed';
|
|
$operation->error_code = 'vpc_not_found';
|
|
$operation->finished_at = $now;
|
|
$operation->updated_at = $now;
|
|
$operation->save();
|
|
return null;
|
|
}
|
|
return [(string) $vpc->vpc_key, (int) $operation->user_id,
|
|
$allowDeferredRetry];
|
|
}, 3
|
|
);
|
|
if ($candidate === null) {
|
|
return false;
|
|
}
|
|
[$vpcKey, $userId, $allowDeferredRetry] = $candidate;
|
|
$actor = [
|
|
'user_id' => $userId, 'is_admin' => false,
|
|
'is_super_admin' => false, 'can_manage' => true,
|
|
'session_id' => '',
|
|
];
|
|
try {
|
|
CloudVpcService::reconcile($vpcKey, $actor, $allowDeferredRetry);
|
|
} catch (CloudError) {
|
|
// The service records failed/uncertain status under the operation
|
|
// lease. No public network job is replayed by this worker.
|
|
}
|
|
return true;
|
|
}
|
|
}
|