feat(storage): add team auto-archive assets policy and background job (#516)

* feat(storage): add team auto-archive assets policy and background job

* fix(storage): address code review feedback on lock safety, starvation, and permissions

* docs: update readme and expand archive storage documentation with auto-archive details

* feat(storage): log info message on auto-archive job start and finish with archived count

* style(storage): stack age threshold and grace period vertically with updated grace period description

* fix(storage): only log auto-archive info messages when candidate assets are found to process
This commit is contained in:
Yiling-J
2026-11-04 14:40:44 +08:00
committed by GitHub
parent ba3c6dbfee
commit 2ecbd49d18
23 changed files with 1805 additions and 39 deletions
+84
View File
@@ -1,3 +1,4 @@
import * as path from 'path'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { teamService } from '@shumai/core/src/team/team'
@@ -388,6 +389,13 @@ describe('TeamService', () => {
username: undefined,
},
appearance: { hideAgent: false },
archiveStorage: { enabled: false },
autoArchive: {
enabled: false,
method: 'not_updated',
days: 30,
postRestoreGraceDays: 30,
},
})
await teamService.updateSettings(team.id, 'theme', 'dark')
@@ -417,6 +425,13 @@ describe('TeamService', () => {
username: undefined,
},
appearance: { hideAgent: false },
archiveStorage: { enabled: false },
autoArchive: {
enabled: false,
method: 'not_updated',
days: 30,
postRestoreGraceDays: 30,
},
})
await teamService.updateSettings(team.id, 'transcode.videoStrategy', 'multi')
@@ -456,6 +471,13 @@ describe('TeamService', () => {
},
appearance: { hideAgent: true },
semanticSearchEnabled: false,
archiveStorage: { enabled: false },
autoArchive: {
enabled: false,
method: 'not_updated',
days: 30,
postRestoreGraceDays: 30,
},
})
})
@@ -633,4 +655,66 @@ describe('TeamService', () => {
delete process.env.ENABLE_EMBEDDING_AGENT
}
})
it('rejects enabling auto-archive when archive storage is not configured', async () => {
delete process.env.ARCHIVE_STORAGE_BACKEND
const owner = await prisma.user.create({
data: { name: 'Owner AA', email: 'owner-aa@example.com', password: 'pw' },
})
const team = await teamService.createTeam(owner, { name: 'AA Team' })
await expect(
teamService.updateSettings(team.id, 'autoArchive', {
enabled: true,
method: 'not_updated',
days: 30,
postRestoreGraceDays: 30,
}),
).rejects.toThrow('Cannot enable auto archive when archive storage is not configured')
await expect(teamService.updateSettings(team.id, 'autoArchive.enabled', true)).rejects.toThrow(
'Cannot enable auto archive when archive storage is not configured',
)
})
it('allows updating auto-archive settings when archive storage is configured', async () => {
const saved = { ...process.env }
process.env.ARCHIVE_STORAGE_BACKEND = 'local'
process.env.ARCHIVE_LOCAL_ROOT = '/tmp/shumai-archive-test-team'
try {
const owner = await prisma.user.create({
data: { name: 'Owner AA 2', email: 'owner-aa2@example.com', password: 'pw' },
})
const team = await teamService.createTeam(owner, { name: 'AA Team 2' })
await teamService.updateSettings(team.id, 'autoArchive', {
enabled: true,
method: 'after_created',
days: 45,
postRestoreGraceDays: 15,
})
const settings = await teamService.getSettings(team.id)
expect(settings.autoArchive).toEqual({
enabled: true,
method: 'after_created',
days: 45,
postRestoreGraceDays: 15,
})
expect(settings.archiveStorage).toEqual({
enabled: true,
backend: 'local',
localRoot: path.resolve('/tmp/shumai-archive-test-team'),
})
// Non-admin call should mask backend/root details
const readerSettings = await teamService.getSettings(team.id, { isAdmin: false })
expect(readerSettings.archiveStorage).toEqual({
enabled: true,
})
} finally {
process.env = saved
}
})
})
+81 -1
View File
@@ -10,6 +10,10 @@ import { AgentService, agentService } from '@shumai/core/src/agent/agent'
import { sandboxService } from '@shumai/core/src/sandbox/sandbox-service'
import { turntableService } from '@shumai/core/src/turntable/turntable'
import { gotenbergService } from '@shumai/core/src/gotenberg/gotenberg'
import {
getArchiveStorageConfig,
isArchiveStorageEnabled,
} from '@shumai/core/src/s3/archive-storage'
import {
ServiceCreateTeamRequest,
ServiceGetUserTeamsRequest,
@@ -23,6 +27,9 @@ import {
SandboxSettings,
UpdateSandboxSettingsRequest,
TeamSettingsResponse,
AutoArchiveMethod,
AutoArchiveSettings,
DEFAULT_AUTO_ARCHIVE_SETTINGS,
} from '@shumai/dtos'
export class TeamService {
@@ -300,7 +307,10 @@ export class TeamService {
})
}
async getSettings(teamId: string): Promise<TeamSettingsResponse> {
async getSettings(
teamId: string,
options?: { isAdmin?: boolean },
): Promise<TeamSettingsResponse> {
const team = await prisma.team.findUnique({
where: { id: teamId },
})
@@ -355,6 +365,33 @@ export class TeamService {
settings.turntable = await turntableService.getSettings(teamId)
settings.gotenberg = await gotenbergService.getSettings(teamId)
const archiveConfig = getArchiveStorageConfig()
if (!archiveConfig.enabled) {
settings.archiveStorage = { enabled: false }
} else if (options?.isAdmin === false) {
settings.archiveStorage = { enabled: true }
} else if (archiveConfig.backend === 'local') {
settings.archiveStorage = {
enabled: true,
backend: 'local',
localRoot: archiveConfig.localRoot,
}
} else {
settings.archiveStorage = {
enabled: true,
backend: 's3',
endpoint: archiveConfig.endpoint,
bucket: archiveConfig.bucket,
}
}
settings.autoArchive = {
enabled: settings.autoArchive?.enabled ?? false,
method: settings.autoArchive?.method ?? 'not_updated',
days: settings.autoArchive?.days ?? 30,
postRestoreGraceDays: settings.autoArchive?.postRestoreGraceDays ?? 30,
}
return settings
}
@@ -421,6 +458,49 @@ export class TeamService {
}
settings.appearance.hideAgent = Boolean(value)
delete settings['appearance.hideAgent']
} else if (key === 'autoArchive') {
const autoArchiveVal = value as AutoArchiveSettings
if (autoArchiveVal.enabled && !isArchiveStorageEnabled()) {
throw new HTTPException(400, {
message: 'Cannot enable auto archive when archive storage is not configured',
})
}
settings.autoArchive = {
enabled: Boolean(autoArchiveVal.enabled),
method: autoArchiveVal.method || 'not_updated',
days: Number(autoArchiveVal.days) || 30,
postRestoreGraceDays: Number(autoArchiveVal.postRestoreGraceDays) || 30,
}
} else if (key === 'autoArchive.enabled') {
const enabled = Boolean(value)
if (enabled && !isArchiveStorageEnabled()) {
throw new HTTPException(400, {
message: 'Cannot enable auto archive when archive storage is not configured',
})
}
if (!settings.autoArchive) {
settings.autoArchive = { ...DEFAULT_AUTO_ARCHIVE_SETTINGS }
}
settings.autoArchive.enabled = enabled
delete settings['autoArchive.enabled']
} else if (key === 'autoArchive.method') {
if (!settings.autoArchive) {
settings.autoArchive = { ...DEFAULT_AUTO_ARCHIVE_SETTINGS }
}
settings.autoArchive.method = value as AutoArchiveMethod
delete settings['autoArchive.method']
} else if (key === 'autoArchive.days') {
if (!settings.autoArchive) {
settings.autoArchive = { ...DEFAULT_AUTO_ARCHIVE_SETTINGS }
}
settings.autoArchive.days = Number(value)
delete settings['autoArchive.days']
} else if (key === 'autoArchive.postRestoreGraceDays') {
if (!settings.autoArchive) {
settings.autoArchive = { ...DEFAULT_AUTO_ARCHIVE_SETTINGS }
}
settings.autoArchive.postRestoreGraceDays = Number(value)
delete settings['autoArchive.postRestoreGraceDays']
} else {
settings[key] = value
}