|
|
|
|
@ -21,12 +21,13 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
* policies. Callers must not maintain parallel state lists.
|
|
|
|
|
*
|
|
|
|
|
* @var array<string,array{
|
|
|
|
|
* transitions:list<string>,sources:list<string>,
|
|
|
|
|
* resume_step:string,transitions:list<string>,sources:list<string>,
|
|
|
|
|
* actions:array<string,list<string>>,resumable:list<string>,
|
|
|
|
|
* cancellable:list<string>,cleanup:list<string>,terminal:bool}>
|
|
|
|
|
*/
|
|
|
|
|
private const DEFINITIONS = [
|
|
|
|
|
'prepared' => [
|
|
|
|
|
'resume_step' => 'acquire',
|
|
|
|
|
'transitions' => ['uploading', 'fetching', 'cancelled'],
|
|
|
|
|
'sources' => ['upload', 'remote'],
|
|
|
|
|
'actions' => [
|
|
|
|
|
@ -39,6 +40,7 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
'terminal' => false,
|
|
|
|
|
],
|
|
|
|
|
'uploading' => [
|
|
|
|
|
'resume_step' => 'acquire',
|
|
|
|
|
'transitions' => [
|
|
|
|
|
'source_verifying', 'upload_failed', 'source_unknown',
|
|
|
|
|
'cancel_pending',
|
|
|
|
|
@ -55,6 +57,7 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
'terminal' => false,
|
|
|
|
|
],
|
|
|
|
|
'fetching' => [
|
|
|
|
|
'resume_step' => 'acquire',
|
|
|
|
|
'transitions' => [
|
|
|
|
|
'source_verifying', 'source_failed', 'source_unknown',
|
|
|
|
|
'cancelled',
|
|
|
|
|
@ -67,6 +70,7 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
'terminal' => false,
|
|
|
|
|
],
|
|
|
|
|
'source_verifying' => [
|
|
|
|
|
'resume_step' => 'verify',
|
|
|
|
|
'transitions' => [
|
|
|
|
|
'source_ready', 'upload_failed', 'source_failed',
|
|
|
|
|
'source_unknown',
|
|
|
|
|
@ -82,6 +86,7 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
'terminal' => false,
|
|
|
|
|
],
|
|
|
|
|
'source_ready' => [
|
|
|
|
|
'resume_step' => 'publish',
|
|
|
|
|
'transitions' => ['distributing', 'cancel_pending'],
|
|
|
|
|
'sources' => ['upload', 'remote'],
|
|
|
|
|
'actions' => [
|
|
|
|
|
@ -94,6 +99,7 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
'terminal' => false,
|
|
|
|
|
],
|
|
|
|
|
'distributing' => [
|
|
|
|
|
'resume_step' => 'published',
|
|
|
|
|
'transitions' => ['ready', 'distribution_failed'],
|
|
|
|
|
'sources' => ['upload', 'remote'],
|
|
|
|
|
'actions' => [
|
|
|
|
|
@ -106,6 +112,7 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
'terminal' => false,
|
|
|
|
|
],
|
|
|
|
|
'ready' => [
|
|
|
|
|
'resume_step' => 'published',
|
|
|
|
|
'transitions' => [],
|
|
|
|
|
'sources' => ['upload', 'remote'],
|
|
|
|
|
'actions' => [
|
|
|
|
|
@ -118,6 +125,7 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
'terminal' => true,
|
|
|
|
|
],
|
|
|
|
|
'upload_failed' => [
|
|
|
|
|
'resume_step' => 'acquire',
|
|
|
|
|
'transitions' => ['uploading', 'cancel_pending', 'cancelled'],
|
|
|
|
|
'sources' => ['upload'],
|
|
|
|
|
'actions' => [
|
|
|
|
|
@ -129,6 +137,7 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
'terminal' => false,
|
|
|
|
|
],
|
|
|
|
|
'source_failed' => [
|
|
|
|
|
'resume_step' => 'acquire',
|
|
|
|
|
'transitions' => ['fetching', 'cancelled'],
|
|
|
|
|
'sources' => ['remote'],
|
|
|
|
|
'actions' => ['remote' => ['resume_remote', 'cancel']],
|
|
|
|
|
@ -138,6 +147,7 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
'terminal' => false,
|
|
|
|
|
],
|
|
|
|
|
'source_unknown' => [
|
|
|
|
|
'resume_step' => 'verify',
|
|
|
|
|
'transitions' => ['source_verifying', 'cancel_pending'],
|
|
|
|
|
'sources' => ['upload', 'remote'],
|
|
|
|
|
'actions' => [
|
|
|
|
|
@ -150,6 +160,7 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
'terminal' => false,
|
|
|
|
|
],
|
|
|
|
|
'distribution_failed' => [
|
|
|
|
|
'resume_step' => 'published',
|
|
|
|
|
'transitions' => ['distributing', 'cancel_pending'],
|
|
|
|
|
'sources' => ['upload', 'remote'],
|
|
|
|
|
'actions' => [
|
|
|
|
|
@ -162,6 +173,7 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
'terminal' => false,
|
|
|
|
|
],
|
|
|
|
|
'cancel_pending' => [
|
|
|
|
|
'resume_step' => 'cancel',
|
|
|
|
|
'transitions' => ['cancelled', 'source_unknown'],
|
|
|
|
|
'sources' => ['upload'],
|
|
|
|
|
'actions' => ['upload' => ['cancel']],
|
|
|
|
|
@ -171,6 +183,7 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
'terminal' => false,
|
|
|
|
|
],
|
|
|
|
|
'cancelled' => [
|
|
|
|
|
'resume_step' => 'cancel',
|
|
|
|
|
'transitions' => [],
|
|
|
|
|
'sources' => ['upload', 'remote'],
|
|
|
|
|
'actions' => [
|
|
|
|
|
@ -238,6 +251,22 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
if (!in_array($action, $actions, true)) {
|
|
|
|
|
throw new CloudError('当前导入状态不能执行该操作', 409);
|
|
|
|
|
}
|
|
|
|
|
if ($action === self::ACTION_CANCEL && $state === 'source_unknown'
|
|
|
|
|
&& !self::capabilities($intent)['can_cancel']) {
|
|
|
|
|
throw new CloudError('当前上传结果尚未确认,不能终止原分段上传', 409);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function resumeStep(CloudTemplateImportIntent $intent): string
|
|
|
|
|
{
|
|
|
|
|
self::assertSourceState((string) $intent->source_kind, (string) $intent->state);
|
|
|
|
|
return self::DEFINITIONS[(string) $intent->state]['resume_step'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Provider proof that a previous abort was not applied. */
|
|
|
|
|
public static function retryableAbortCodes(): array
|
|
|
|
|
{
|
|
|
|
|
return ['multipart_abort_rejected', 'multipart_abort_not_applied'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return array{can_resume:bool,can_cancel:bool} */
|
|
|
|
|
@ -252,10 +281,8 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
if ($canCancel && $source === self::SOURCE_UPLOAD
|
|
|
|
|
&& $state === 'source_unknown') {
|
|
|
|
|
$canCancel = (string) $intent->multipart_upload_id === ''
|
|
|
|
|
|| in_array((string) $intent->error_code, [
|
|
|
|
|
'multipart_abort_rejected',
|
|
|
|
|
'multipart_abort_not_applied',
|
|
|
|
|
], true);
|
|
|
|
|
|| in_array((string) $intent->error_code,
|
|
|
|
|
self::retryableAbortCodes(), true);
|
|
|
|
|
}
|
|
|
|
|
return [
|
|
|
|
|
'can_resume' => in_array(
|
|
|
|
|
@ -304,7 +331,7 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
];
|
|
|
|
|
foreach ($definitions as $state => $definition) {
|
|
|
|
|
if ($state === '' || array_keys($definition) !== [
|
|
|
|
|
'transitions', 'sources', 'actions', 'resumable',
|
|
|
|
|
'resume_step', 'transitions', 'sources', 'actions', 'resumable',
|
|
|
|
|
'cancellable', 'cleanup', 'terminal',
|
|
|
|
|
]) {
|
|
|
|
|
throw new \LogicException(
|
|
|
|
|
@ -322,7 +349,9 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($definition['sources'] === []
|
|
|
|
|
if (!in_array($definition['resume_step'],
|
|
|
|
|
['acquire', 'verify', 'publish', 'published', 'cancel'], true)
|
|
|
|
|
|| $definition['sources'] === []
|
|
|
|
|
|| array_diff($definition['sources'], $sources) !== []
|
|
|
|
|
|| array_diff($definition['resumable'],
|
|
|
|
|
$definition['sources']) !== []
|
|
|
|
|
@ -350,7 +379,7 @@ final class CloudTemplateImportStateMachine
|
|
|
|
|
}
|
|
|
|
|
foreach ($definition['sources'] as $source) {
|
|
|
|
|
$stateActions = $definition['actions'][$source] ?? null;
|
|
|
|
|
if (!is_array($stateActions)) {
|
|
|
|
|
if (!is_array($stateActions) || $stateActions === []) {
|
|
|
|
|
throw new \LogicException(
|
|
|
|
|
'Cloud-template import source has no action definition'
|
|
|
|
|
);
|
|
|
|
|
|