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.
59 lines
2.3 KiB
PHP
59 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudBackupRestoreAttempt;
|
|
|
|
/** Bounded continuation for a restore admission committed before job publish. */
|
|
final class CloudPortableRestoreAdmissionRecoveryService
|
|
{
|
|
private const QUIET_SECONDS = 60;
|
|
private const RETRY_SECONDS = 300;
|
|
|
|
public static function recoverOne(): bool
|
|
{
|
|
$now = time();
|
|
$attempt = CloudBackupRestoreAttempt::where('state', 'reserved')
|
|
->whereNull('job_id')->where('active_attempt', 1)
|
|
->where(static function ($query) use ($now): void {
|
|
$query->where(static function ($fresh) use ($now): void {
|
|
$fresh->where('last_error_code', '')
|
|
->where('updated_at', '<=',
|
|
$now - self::QUIET_SECONDS);
|
|
})->orWhere(static function ($retry) use ($now): void {
|
|
$retry->whereIn('last_error_code', [
|
|
'portable_restore_admission_conflict',
|
|
'portable_restore_admission_unavailable',
|
|
])->where('updated_at', '<=',
|
|
$now - self::RETRY_SECONDS);
|
|
});
|
|
})
|
|
->orderBy('updated_at')->orderBy('id')->first();
|
|
if (!$attempt instanceof CloudBackupRestoreAttempt) {
|
|
return false;
|
|
}
|
|
try {
|
|
CloudPortableRestoreJobDispatcher::recoverUndispatched(
|
|
(string) $attempt->attempt_key
|
|
);
|
|
} catch (\Throwable $error) {
|
|
$code = $error instanceof CloudError
|
|
? 'portable_restore_admission_conflict'
|
|
: 'portable_restore_admission_unavailable';
|
|
CloudBackupRestoreAttempt::where('id', (int) $attempt->id)
|
|
->where('attempt_key', (string) $attempt->attempt_key)
|
|
->where('state', 'reserved')->whereNull('job_id')
|
|
->where('active_attempt', 1)->update([
|
|
'last_error_code' => $code,
|
|
'updated_at' => time(),
|
|
]);
|
|
error_log(sprintf(
|
|
'portable restore admission recovery pending attempt_id=%d error=%s',
|
|
(int) $attempt->id,
|
|
get_class($error)
|
|
));
|
|
}
|
|
return true;
|
|
}
|
|
}
|