mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 10:55:27 +00:00
feat(storage): add archive storage support for assets (#514)
* feat(storage): add archive storage support for assets * fix(webui): show archive icon in front of file name in breadcrumb bar * fix(webui): display archive icon in breadcrumbs when restoring * refactor(transcode): simplify restore asset workflow to single while loop * docs(configuration): add archive storage documentation and readme entries * fix(webui): show archive icon in front of version names and prevent version card overflow * fix(webui): fix overflow in manage versions dialog scroll area * fix(webui): prevent manage versions dialog overflow on long file names * docs(configuration): fix archive storage environment variables and defaults * fix(storage): address code review findings for archive storage
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import type { WorkflowTask } from '@shumai/db'
|
||||
import '@shumai/db/src/prisma-json-types'
|
||||
import {
|
||||
commitArchiveDbStateActivity,
|
||||
copyOriginalToArchiveStorageActivity,
|
||||
deleteMainOriginalAfterArchiveActivity,
|
||||
prepareArchiveOrRestoreActivity,
|
||||
revertArchiveOrRestoreStatusOnFailureActivity,
|
||||
} from '../activities/archive'
|
||||
import { completeTask, startTask } from './common'
|
||||
|
||||
export async function archiveAssetWorkflow(task: WorkflowTask): Promise<void> {
|
||||
const storageKeyId = task.payload?.archive?.storageKeyId
|
||||
|
||||
try {
|
||||
await startTask(task)
|
||||
|
||||
const prep = await prepareArchiveOrRestoreActivity({
|
||||
taskId: task.id,
|
||||
assetId: task.assetId,
|
||||
storageKeyId,
|
||||
mode: 'archive',
|
||||
})
|
||||
|
||||
if (prep.noop) {
|
||||
await completeTask(task.id)
|
||||
return
|
||||
}
|
||||
|
||||
await copyOriginalToArchiveStorageActivity({
|
||||
storageKeyId: prep.storageKeyId,
|
||||
key: prep.key,
|
||||
expectedSize: prep.expectedSize,
|
||||
contentType: prep.contentType,
|
||||
})
|
||||
|
||||
await commitArchiveDbStateActivity({
|
||||
storageKeyId: prep.storageKeyId,
|
||||
})
|
||||
|
||||
await deleteMainOriginalAfterArchiveActivity({
|
||||
key: prep.key,
|
||||
})
|
||||
|
||||
await completeTask(task.id)
|
||||
} catch (err) {
|
||||
const errorMessage = err instanceof Error ? err.message : String(err)
|
||||
await revertArchiveOrRestoreStatusOnFailureActivity({
|
||||
taskId: task.id,
|
||||
storageKeyId,
|
||||
assetId: task.assetId,
|
||||
errorMessage,
|
||||
}).catch((revertErr) => {
|
||||
console.error('Failed to revert archive status on failure:', revertErr)
|
||||
})
|
||||
throw err
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,17 @@ export async function overlayImageAnnotationWorkflow(task: WorkflowTask): Promis
|
||||
})
|
||||
}
|
||||
|
||||
if (
|
||||
key === asset.storageKey?.key &&
|
||||
(asset.storageKey?.isArchived || asset.status === 'archived')
|
||||
) {
|
||||
throw ApplicationFailure.create({
|
||||
message:
|
||||
'Original image file is archived and no proxy is available. Restore the asset to overlay annotations.',
|
||||
nonRetryable: true,
|
||||
})
|
||||
}
|
||||
|
||||
const keyWithOverlay = await overlayAnnotationsActivity({
|
||||
assetKey: key,
|
||||
assetId: asset.id,
|
||||
|
||||
@@ -17,6 +17,12 @@ export async function renderPdfPagesWorkflow(task: WorkflowTask): Promise<void>
|
||||
|
||||
const { asset, key } = await fetchAssetWithKey(task.assetId)
|
||||
const pdfKey = (asset.media as PrismaJson.MediaInfo | null)?.pdfTranscode?.key || key
|
||||
if (pdfKey === key && (asset.storageKey?.isArchived || asset.status === 'archived')) {
|
||||
throw ApplicationFailure.create({
|
||||
message: 'Original PDF file is archived. Restore the asset to render pages.',
|
||||
nonRetryable: true,
|
||||
})
|
||||
}
|
||||
|
||||
const pages = await renderPdfPagesActivity({
|
||||
taskId: task.id,
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
import type { WorkflowTask } from '@shumai/db'
|
||||
import '@shumai/db/src/prisma-json-types'
|
||||
import { ApplicationFailure, sleep } from '@shumai/workflow-core'
|
||||
import {
|
||||
checkOrInitiateColdRestoreActivity,
|
||||
commitRestoreDbStateActivity,
|
||||
copyOriginalFromArchiveToMainActivity,
|
||||
deleteArchiveOriginalAfterRestoreActivity,
|
||||
prepareArchiveOrRestoreActivity,
|
||||
revertArchiveOrRestoreStatusOnFailureActivity,
|
||||
} from '../activities/archive'
|
||||
import { completeTask, startTask } from './common'
|
||||
|
||||
const DEFAULT_INITIAL_RESTORE_POLL_MS = 60_000
|
||||
const DEFAULT_STEADY_RESTORE_POLL_MS = 300_000
|
||||
const MAX_RESTORE_WAIT_MS = 48 * 60 * 60 * 1000
|
||||
|
||||
export async function restoreAssetWorkflow(task: WorkflowTask): Promise<void> {
|
||||
const storageKeyId = task.payload?.archive?.storageKeyId
|
||||
|
||||
try {
|
||||
await startTask(task)
|
||||
|
||||
const prep = await prepareArchiveOrRestoreActivity({
|
||||
taskId: task.id,
|
||||
assetId: task.assetId,
|
||||
storageKeyId,
|
||||
mode: 'restore',
|
||||
})
|
||||
|
||||
if (prep.noop) {
|
||||
await completeTask(task.id)
|
||||
return
|
||||
}
|
||||
|
||||
const customPollMs = process.env.ARCHIVE_RESTORE_POLL_INTERVAL_MS
|
||||
? parseInt(process.env.ARCHIVE_RESTORE_POLL_INTERVAL_MS, 10)
|
||||
: undefined
|
||||
const initialPollMs =
|
||||
customPollMs && Number.isFinite(customPollMs) && customPollMs > 0
|
||||
? customPollMs
|
||||
: DEFAULT_INITIAL_RESTORE_POLL_MS
|
||||
const steadyPollMs =
|
||||
customPollMs && Number.isFinite(customPollMs) && customPollMs > 0
|
||||
? customPollMs
|
||||
: DEFAULT_STEADY_RESTORE_POLL_MS
|
||||
|
||||
let elapsedWaitMs = 0
|
||||
let pollAttempt = 0
|
||||
|
||||
while (elapsedWaitMs < MAX_RESTORE_WAIT_MS) {
|
||||
const restoreCheck = await checkOrInitiateColdRestoreActivity({ key: prep.key })
|
||||
|
||||
if (restoreCheck.status === 'ready') {
|
||||
const copyResult = await copyOriginalFromArchiveToMainActivity({
|
||||
storageKeyId: prep.storageKeyId,
|
||||
key: prep.key,
|
||||
expectedSize: prep.expectedSize,
|
||||
contentType: prep.contentType,
|
||||
})
|
||||
|
||||
if (copyResult.status === 'copied') {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
const waitMs = pollAttempt < 10 ? initialPollMs : steadyPollMs
|
||||
await sleep(waitMs)
|
||||
elapsedWaitMs += waitMs
|
||||
pollAttempt++
|
||||
}
|
||||
|
||||
if (elapsedWaitMs >= MAX_RESTORE_WAIT_MS) {
|
||||
throw ApplicationFailure.create({
|
||||
message: `Cold storage restore timed out after 48 hours for key: ${prep.key}`,
|
||||
nonRetryable: true,
|
||||
})
|
||||
}
|
||||
|
||||
await commitRestoreDbStateActivity({
|
||||
storageKeyId: prep.storageKeyId,
|
||||
})
|
||||
|
||||
await deleteArchiveOriginalAfterRestoreActivity({
|
||||
key: prep.key,
|
||||
})
|
||||
|
||||
await completeTask(task.id)
|
||||
} catch (err) {
|
||||
const errorMessage = err instanceof Error ? err.message : String(err)
|
||||
await revertArchiveOrRestoreStatusOnFailureActivity({
|
||||
taskId: task.id,
|
||||
storageKeyId,
|
||||
assetId: task.assetId,
|
||||
errorMessage,
|
||||
}).catch((revertErr) => {
|
||||
console.error('Failed to revert restore status on failure:', revertErr)
|
||||
})
|
||||
throw err
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,14 @@ export async function takeVideoScreenshotsWorkflow(task: WorkflowTask): Promise<
|
||||
}
|
||||
}
|
||||
|
||||
if (targetKey === key && (asset.storageKey?.isArchived || asset.status === 'archived')) {
|
||||
throw ApplicationFailure.create({
|
||||
message:
|
||||
'Original video file is archived and no proxy is available. Restore the asset to take screenshots.',
|
||||
nonRetryable: true,
|
||||
})
|
||||
}
|
||||
|
||||
const screenshots = await takeScreenshotsActivity({
|
||||
taskId: task.id,
|
||||
assetKey: targetKey,
|
||||
|
||||
Reference in New Issue
Block a user