|
|
|
|
@ -22,6 +22,7 @@ final class CloudServiceCompensationExecutionService
|
|
|
|
|
{
|
|
|
|
|
public const LEASE_SECONDS = 120;
|
|
|
|
|
public const MAX_ATTEMPTS = 8;
|
|
|
|
|
private const EXTERNAL_WAIT_SECONDS = 300;
|
|
|
|
|
|
|
|
|
|
private readonly \Closure $clock;
|
|
|
|
|
|
|
|
|
|
@ -287,7 +288,8 @@ final class CloudServiceCompensationExecutionService
|
|
|
|
|
$admission['state'],
|
|
|
|
|
$admission['code'],
|
|
|
|
|
$admission['message'],
|
|
|
|
|
$now
|
|
|
|
|
$now,
|
|
|
|
|
$admission['consume_retry_budget']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -313,7 +315,8 @@ final class CloudServiceCompensationExecutionService
|
|
|
|
|
'deferred',
|
|
|
|
|
'service_period_changed',
|
|
|
|
|
'实例服务周期已变化,系统将重新核对',
|
|
|
|
|
$now
|
|
|
|
|
$now,
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$oldExpiry = (int) $instance->expires_at;
|
|
|
|
|
@ -354,7 +357,8 @@ final class CloudServiceCompensationExecutionService
|
|
|
|
|
$financial['state'],
|
|
|
|
|
$financial['code'],
|
|
|
|
|
$financial['message'],
|
|
|
|
|
$now
|
|
|
|
|
$now,
|
|
|
|
|
$financial['consume_retry_budget']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$operationId = substr(hash(
|
|
|
|
|
@ -506,7 +510,10 @@ final class CloudServiceCompensationExecutionService
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return array{state:string,code:string,message:string}|null */
|
|
|
|
|
/**
|
|
|
|
|
* @return array{state:string,code:string,message:string,
|
|
|
|
|
* consume_retry_budget:bool}|null
|
|
|
|
|
*/
|
|
|
|
|
private function admissionLocked(CloudInstance $instance, int $now): ?array
|
|
|
|
|
{
|
|
|
|
|
if ((string) $instance->status === 'deleted') {
|
|
|
|
|
@ -516,7 +523,7 @@ final class CloudServiceCompensationExecutionService
|
|
|
|
|
}
|
|
|
|
|
if (CloudInvoiceService::instanceDeleting($instance)) {
|
|
|
|
|
return $this->decision(
|
|
|
|
|
'deferred', 'instance_deleting', '实例删除流程尚未收敛'
|
|
|
|
|
'deferred', 'instance_deleting', '实例删除流程尚未收敛', false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ((string) $instance->service_status !== 'active') {
|
|
|
|
|
@ -534,7 +541,7 @@ final class CloudServiceCompensationExecutionService
|
|
|
|
|
(int) $instance->id
|
|
|
|
|
)) {
|
|
|
|
|
return $this->decision(
|
|
|
|
|
'deferred', 'migration_open', '实例维护迁移尚未收敛'
|
|
|
|
|
'deferred', 'migration_open', '实例维护迁移尚未收敛', false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$jobs = CloudJobService::blockingQueryForInstance(
|
|
|
|
|
@ -545,7 +552,7 @@ final class CloudServiceCompensationExecutionService
|
|
|
|
|
if ($jobs->contains(static fn ($job): bool =>
|
|
|
|
|
(string) $job->phase !== 'terminal')) {
|
|
|
|
|
return $this->decision(
|
|
|
|
|
'deferred', 'instance_operation_open', '实例仍有操作正在执行'
|
|
|
|
|
'deferred', 'instance_operation_open', '实例仍有操作正在执行', false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (!$jobs->isEmpty()) {
|
|
|
|
|
@ -558,7 +565,10 @@ final class CloudServiceCompensationExecutionService
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return array{state:string,code:string,message:string}|null */
|
|
|
|
|
/**
|
|
|
|
|
* @return array{state:string,code:string,message:string,
|
|
|
|
|
* consume_retry_budget:bool}|null
|
|
|
|
|
*/
|
|
|
|
|
private function financialAdmissionLocked(
|
|
|
|
|
CloudInstance $instance,
|
|
|
|
|
int $oldExpiry
|
|
|
|
|
@ -603,7 +613,7 @@ final class CloudServiceCompensationExecutionService
|
|
|
|
|
(string) $job->phase !== 'terminal')) {
|
|
|
|
|
return $this->decision(
|
|
|
|
|
'deferred', 'renewal_payment_job_open',
|
|
|
|
|
'旧续费账单的支付任务仍在执行'
|
|
|
|
|
'旧续费账单的支付任务仍在执行', false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (!$jobs->isEmpty()) {
|
|
|
|
|
@ -622,7 +632,7 @@ final class CloudServiceCompensationExecutionService
|
|
|
|
|
))) {
|
|
|
|
|
return $this->decision(
|
|
|
|
|
'deferred', 'renewal_payment_open',
|
|
|
|
|
'旧续费账单仍有支付正在处理'
|
|
|
|
|
'旧续费账单仍有支付正在处理', false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ($payments->contains(static fn ($payment): bool => !in_array(
|
|
|
|
|
@ -693,8 +703,16 @@ final class CloudServiceCompensationExecutionService
|
|
|
|
|
string $state,
|
|
|
|
|
string $code,
|
|
|
|
|
string $message,
|
|
|
|
|
int $now
|
|
|
|
|
int $now,
|
|
|
|
|
bool $consumeRetryBudget = true
|
|
|
|
|
): array {
|
|
|
|
|
if ($state === 'deferred' && !$consumeRetryBudget) {
|
|
|
|
|
// Claiming increments attempts before the external admission facts
|
|
|
|
|
// are locked. Undo only that claim so long-running migrations,
|
|
|
|
|
// deletes and payments do not consume the technical failure
|
|
|
|
|
// budget. Prior real failures remain counted.
|
|
|
|
|
$item->attempts = max(0, (int) $item->attempts - 1);
|
|
|
|
|
}
|
|
|
|
|
if ($state === 'deferred'
|
|
|
|
|
&& (int) $item->attempts >= self::MAX_ATTEMPTS) {
|
|
|
|
|
$state = 'attention';
|
|
|
|
|
@ -708,7 +726,10 @@ final class CloudServiceCompensationExecutionService
|
|
|
|
|
$item->lease_token = null;
|
|
|
|
|
$item->lease_until = 0;
|
|
|
|
|
$item->available_at = $state === 'deferred'
|
|
|
|
|
? $now + $this->backoff((int) $item->attempts) : $now;
|
|
|
|
|
? $now + ($consumeRetryBudget
|
|
|
|
|
? $this->backoff((int) $item->attempts)
|
|
|
|
|
: self::EXTERNAL_WAIT_SECONDS)
|
|
|
|
|
: $now;
|
|
|
|
|
$item->terminal_at = $state === 'deferred' ? null : $now;
|
|
|
|
|
$item->last_error_code = substr($code, 0, 64);
|
|
|
|
|
$item->last_error = mb_substr($message, 0, 500);
|
|
|
|
|
@ -781,13 +802,22 @@ final class CloudServiceCompensationExecutionService
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return array{state:string,code:string,message:string} */
|
|
|
|
|
/**
|
|
|
|
|
* @return array{state:string,code:string,message:string,
|
|
|
|
|
* consume_retry_budget:bool}
|
|
|
|
|
*/
|
|
|
|
|
private function decision(
|
|
|
|
|
string $state,
|
|
|
|
|
string $code,
|
|
|
|
|
string $message
|
|
|
|
|
string $message,
|
|
|
|
|
bool $consumeRetryBudget = true
|
|
|
|
|
): array {
|
|
|
|
|
return compact('state', 'code', 'message');
|
|
|
|
|
return [
|
|
|
|
|
'state' => $state,
|
|
|
|
|
'code' => $code,
|
|
|
|
|
'message' => $message,
|
|
|
|
|
'consume_retry_budget' => $consumeRetryBudget,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function backoff(int $attempts): int
|
|
|
|
|
|