|
|
|
|
@ -84,9 +84,19 @@ final class CloudTemplateImportPublication extends CloudTemplateImportModule
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$connection = (new CloudTemplateImportIntent())->getConnection();
|
|
|
|
|
$prepared = $connection->transaction(static function () use (
|
|
|
|
|
$id
|
|
|
|
|
// Keep storage finalization, catalog adoption, distribution job
|
|
|
|
|
// reservation and the owner pointer in one database commit. Direct
|
|
|
|
|
// HTTP recovery and the publication outbox therefore have the same
|
|
|
|
|
// crash boundary: a process loss can commit all of them or none.
|
|
|
|
|
return $connection->transaction(function () use (
|
|
|
|
|
$connection,
|
|
|
|
|
$id,
|
|
|
|
|
$actor,
|
|
|
|
|
$client
|
|
|
|
|
): array {
|
|
|
|
|
$prepared = $connection->transaction(static function () use (
|
|
|
|
|
$id
|
|
|
|
|
): array {
|
|
|
|
|
$intent = self::lockIntent($id);
|
|
|
|
|
if (CloudTemplateImportStateMachine::resumeStep($intent) === 'published') {
|
|
|
|
|
$job = $intent->active_job_id === null ? null
|
|
|
|
|
@ -147,48 +157,53 @@ final class CloudTemplateImportPublication extends CloudTemplateImportModule
|
|
|
|
|
$intent
|
|
|
|
|
);
|
|
|
|
|
return ['intent' => $intent, 'template' => $template];
|
|
|
|
|
}, 3);
|
|
|
|
|
if (isset($prepared['ack'])) {
|
|
|
|
|
return $prepared['ack'];
|
|
|
|
|
}
|
|
|
|
|
/** @var CloudTemplateImportIntent $intent */
|
|
|
|
|
$intent = $prepared['intent'];
|
|
|
|
|
$taskActor = $actor;
|
|
|
|
|
$taskActor['user_id'] = (int) $intent->user_id;
|
|
|
|
|
$taskActor['request_idempotency_key'] =
|
|
|
|
|
'template-import-' . (string) $intent->id;
|
|
|
|
|
$ack = CloudTemplateCatalogService::distributePublishedImport(
|
|
|
|
|
$intent,
|
|
|
|
|
$taskActor,
|
|
|
|
|
$client
|
|
|
|
|
);
|
|
|
|
|
$jobId = filter_var($ack['task_id'] ?? null, FILTER_VALIDATE_INT,
|
|
|
|
|
['options' => ['min_range' => 1]]);
|
|
|
|
|
if ($jobId === false) {
|
|
|
|
|
throw new CloudError('云镜像导入任务回执无效', 502);
|
|
|
|
|
}
|
|
|
|
|
$connection->transaction(static function () use (
|
|
|
|
|
$id,
|
|
|
|
|
$jobId
|
|
|
|
|
): void {
|
|
|
|
|
$locked = self::lockIntent($id);
|
|
|
|
|
if ((string) $locked->state === 'source_ready') {
|
|
|
|
|
CloudTemplateImportStateMachine::move(
|
|
|
|
|
$locked,
|
|
|
|
|
'distributing'
|
|
|
|
|
);
|
|
|
|
|
}, 3);
|
|
|
|
|
if (isset($prepared['ack'])) {
|
|
|
|
|
return $prepared['ack'];
|
|
|
|
|
}
|
|
|
|
|
if (!in_array((string) $locked->state,
|
|
|
|
|
['distributing', 'ready', 'distribution_failed'], true)
|
|
|
|
|
|| ($locked->active_job_id !== null
|
|
|
|
|
&& (int) $locked->active_job_id !== (int) $jobId)) {
|
|
|
|
|
throw new CloudError('云镜像分发任务状态不一致', 409);
|
|
|
|
|
/** @var CloudTemplateImportIntent $intent */
|
|
|
|
|
$intent = $prepared['intent'];
|
|
|
|
|
$taskActor = $actor;
|
|
|
|
|
$taskActor['user_id'] = (int) $intent->user_id;
|
|
|
|
|
$taskActor['request_idempotency_key'] =
|
|
|
|
|
'template-import-' . (string) $intent->id;
|
|
|
|
|
$ack = CloudTemplateCatalogService::distributePublishedImport(
|
|
|
|
|
$intent,
|
|
|
|
|
$taskActor,
|
|
|
|
|
$client
|
|
|
|
|
);
|
|
|
|
|
$jobId = filter_var($ack['task_id'] ?? null,
|
|
|
|
|
FILTER_VALIDATE_INT,
|
|
|
|
|
['options' => ['min_range' => 1]]);
|
|
|
|
|
if ($jobId === false) {
|
|
|
|
|
throw new CloudError('云镜像导入任务回执无效', 502);
|
|
|
|
|
}
|
|
|
|
|
$locked->active_job_id = (int) $jobId;
|
|
|
|
|
$locked->updated_at = time();
|
|
|
|
|
$locked->save();
|
|
|
|
|
$connection->transaction(static function () use (
|
|
|
|
|
$id,
|
|
|
|
|
$jobId
|
|
|
|
|
): void {
|
|
|
|
|
$locked = self::lockIntent($id);
|
|
|
|
|
if ((string) $locked->state === 'source_ready') {
|
|
|
|
|
CloudTemplateImportStateMachine::move(
|
|
|
|
|
$locked,
|
|
|
|
|
'distributing'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (!in_array((string) $locked->state,
|
|
|
|
|
['distributing', 'ready', 'distribution_failed'], true)
|
|
|
|
|
|| ($locked->active_job_id !== null
|
|
|
|
|
&& (int) $locked->active_job_id !== (int) $jobId)) {
|
|
|
|
|
throw new CloudError(
|
|
|
|
|
'云镜像分发任务状态不一致',
|
|
|
|
|
409
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$locked->active_job_id = (int) $jobId;
|
|
|
|
|
$locked->updated_at = time();
|
|
|
|
|
$locked->save();
|
|
|
|
|
}, 3);
|
|
|
|
|
return $ack;
|
|
|
|
|
}, 3);
|
|
|
|
|
return $ack;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function settleDistributionLocked(
|
|
|
|
|
|