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.
125 lines
6.1 KiB
PHP
125 lines
6.1 KiB
PHP
<?php
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudJob;
|
|
|
|
final class CloudJobService
|
|
{
|
|
public static function enqueue(CloudInstance $instance, array $actor, string $kind, string $method,
|
|
string $suffix, array $body = [], ?array $credential = null): CloudJob
|
|
{
|
|
CloudAccess::vmActor($instance, $actor);
|
|
return $instance->getConnection()->transaction(function () use ($instance, $actor, $kind, $method, $suffix, $body, $credential) {
|
|
if ($kind === 'reinstall') {
|
|
CloudTemplateDeletionService::assertAvailable(
|
|
CloudInput::template($body['template_id'] ?? '')
|
|
);
|
|
}
|
|
$locked = CloudInstance::where('id', $instance->id)->lockForUpdate()->first();
|
|
if ($locked === null || (string) $locked->status === 'deleted') {
|
|
throw new CloudError('虚拟机不存在', 404);
|
|
}
|
|
if ((string) $locked->status !== 'active') {
|
|
throw new CloudError('实例正在执行到期销毁流程,不能提交其他操作', 409);
|
|
}
|
|
$policyService = ['suspending_expired', 'resume_pending', 'resuming',
|
|
'suspend_failed', 'resume_failed', 'expired_grace',
|
|
'purge_stopping', 'purge_stop_failed', 'purge_deleting',
|
|
'purge_failed'];
|
|
$policyNetwork = ['blocking', 'blocking_expired', 'blocked_expired',
|
|
'restore_pending', 'restoring', 'block_failed', 'restore_failed'];
|
|
$superAdmin = ($actor['is_super_admin'] ?? false)
|
|
&& ($actor['can_manage'] ?? false);
|
|
if ($kind !== 'delete' && !$superAdmin
|
|
&& (in_array((string) $locked->service_status, $policyService, true)
|
|
|| in_array((string) $locked->network_status, $policyNetwork, true)
|
|
|| ((string) $locked->service_status !== 'exempt'
|
|
&& ($locked->expires_at === null || (int) $locked->expires_at <= time())))) {
|
|
throw new CloudError('实例正在执行到期或流量策略,请稍后重试', 409);
|
|
}
|
|
if (CloudJob::where('instance_id', $locked->id)->whereIn('status', ['queued', 'running', 'uncertain'])->exists()) {
|
|
throw new CloudError('虚拟机已有任务,请等待完成', 409);
|
|
}
|
|
$job = new CloudJob();
|
|
$job->operation_id = bin2hex(random_bytes(16));
|
|
$job->instance_id = $locked->id;
|
|
$job->user_id = $actor['user_id'];
|
|
$job->host_id = $locked->host_id;
|
|
$job->kind = $kind;
|
|
$job->status = 'queued';
|
|
$job->payload_cipher = CloudSecrets::encrypt(['method' => $method,
|
|
'resource' => 'vms/' . rawurlencode($locked->uuid) . $suffix, 'body' => $body,
|
|
'credential' => $credential, 'owner_id' => (int) $locked->user_id,
|
|
'can_manage' => (bool) ($actor['can_manage'] ?? false)]);
|
|
$job->available_at = $job->created_at = $job->updated_at = time();
|
|
$job->save();
|
|
return $job;
|
|
}, 3);
|
|
}
|
|
|
|
/**
|
|
* Queue the irreversible second phase of expiry cleanup.
|
|
*
|
|
* Only the lifecycle state machine may reach this method. Unlike the
|
|
* four target-state policy jobs, expiry deletion is intentionally never
|
|
* replayed after dispatch because an interrupted volume deletion may be
|
|
* only partly complete.
|
|
*/
|
|
public static function enqueueExpiryDelete(CloudInstance $instance): CloudJob
|
|
{
|
|
return $instance->getConnection()->transaction(function () use ($instance) {
|
|
$locked = CloudInstance::where('id', $instance->id)->lockForUpdate()->first();
|
|
if ($locked === null || (string) $locked->status === 'deleted') {
|
|
throw new CloudError('虚拟机不存在', 404);
|
|
}
|
|
if ((string) $locked->status !== 'deleting'
|
|
|| (string) $locked->service_status !== 'purge_deleting') {
|
|
throw new CloudError('实例尚未完成销毁前停机,不能删除磁盘', 409);
|
|
}
|
|
$existing = CloudJob::where('instance_id', $locked->id)
|
|
->whereIn('status', ['queued', 'running', 'uncertain'])
|
|
->orderByDesc('id')->first();
|
|
if ($existing !== null) {
|
|
if ((string) $existing->kind === 'expiry_delete') {
|
|
return $existing;
|
|
}
|
|
throw new CloudError('虚拟机已有任务,请等待完成', 409);
|
|
}
|
|
|
|
$job = new CloudJob();
|
|
$job->operation_id = bin2hex(random_bytes(16));
|
|
$job->instance_id = $locked->id;
|
|
$job->user_id = $locked->user_id;
|
|
$job->host_id = $locked->host_id;
|
|
$job->kind = 'expiry_delete';
|
|
$job->status = 'queued';
|
|
$job->payload_cipher = CloudSecrets::encrypt([
|
|
'method' => 'DELETE',
|
|
'resource' => 'vms/' . rawurlencode($locked->uuid)
|
|
. '?delete_disks=true',
|
|
'body' => [],
|
|
'owner_id' => (int) $locked->user_id,
|
|
'system_cleanup' => true,
|
|
]);
|
|
$job->available_at = $job->created_at = $job->updated_at = time();
|
|
$job->save();
|
|
return $job;
|
|
}, 3);
|
|
}
|
|
|
|
public static function display(CloudJob $job): array
|
|
{
|
|
$result = $job->result_json === null ? null : json_decode($job->result_json, true);
|
|
$safeResult = is_array($result) ? CloudInput::safeResult($result) : null;
|
|
return ['id' => (string) $job->id, 'operation_id' => $job->operation_id, 'kind' => $job->kind,
|
|
'status' => $job->status, 'error' => $job->error_code,
|
|
'attempts' => max(0, (int) $job->attempts),
|
|
'created_at' => max(0, (int) $job->created_at),
|
|
'updated_at' => max(0, (int) $job->updated_at),
|
|
'phase' => is_string($safeResult['phase'] ?? null)
|
|
? mb_substr($safeResult['phase'], 0, 120) : null,
|
|
'result' => $safeResult];
|
|
}
|
|
}
|