Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(queue): restore task deletion and simplify dashboard controls

Timothy Yin1e0737950eda
diff --git a/app/pages/dashboard/queue.vue b/app/pages/dashboard/queue.vue
index f88de7a3..130a8035 100644
--- a/app/pages/dashboard/queue.vue
+++ b/app/pages/dashboard/queue.vue
@@ -140,10 +140,10 @@ const retryAllFailedTasks = async () => {
}
}
-// 删除单个任务(仅适用于失败任务)
+// 删除单个已完成或失败任务
const deleteTask = async (taskId: number) => {
try {
- await $fetch(`/api/queue/failed/${taskId}`, {
+ await $fetch(`/api/queue/task/${taskId}`, {
method: 'DELETE',
})
@@ -279,30 +279,43 @@ useIntervalFn(refreshData, 10000) // 每10秒刷新一次
<template #right>
<UButton
icon="tabler:refresh"
- variant="soft"
+ color="neutral"
+ variant="ghost"
+ size="sm"
+ :aria-label="$t('dashboard.queue.actions.refresh')"
+ :title="$t('dashboard.queue.actions.refresh')"
:loading="isLoading"
+ :disabled="isLoading"
@click="refreshData"
+ />
+ <UDropdownMenu
+ :content="{ align: 'end' }"
+ :items="[
+ [{
+ label: $t('dashboard.queue.actions.retryAll'),
+ icon: 'tabler:refresh',
+ disabled: isLoading,
+ onSelect: () => retryAllFailedTasks(),
+ }],
+ [{
+ label: $t('dashboard.queue.actions.clearNonActive'),
+ icon: 'tabler:trash',
+ color: 'error',
+ disabled: isLoading,
+ onSelect: () => clearNonActiveTasks(),
+ }],
+ ]"
>
- {{ $t('dashboard.queue.actions.refresh') }}
- </UButton>
- <UButton
- icon="tabler:refresh"
- color="warning"
- variant="soft"
- :loading="isLoading"
- @click="retryAllFailedTasks"
- >
- {{ $t('dashboard.queue.actions.retryAll') }}
- </UButton>
- <UButton
- icon="tabler:trash"
- color="error"
- variant="soft"
- :loading="isLoading"
- @click="clearNonActiveTasks"
- >
- {{ $t('dashboard.queue.actions.clearNonActive') }}
- </UButton>
+ <UButton
+ icon="tabler:dots-vertical"
+ color="neutral"
+ variant="ghost"
+ size="sm"
+ :aria-label="$t('dashboard.queue.actions.moreActions')"
+ :title="$t('dashboard.queue.actions.moreActions')"
+ :disabled="isLoading"
+ />
+ </UDropdownMenu>
</template>
</UDashboardNavbar>
</template>
@@ -314,25 +327,25 @@ useIntervalFn(refreshData, 10000) // 每10秒刷新一次
<DashboardIndicator
:title="$t('dashboard.queue.indicator.pending')"
icon="tabler:clock"
- color="orange"
+ variant="subtle"
:value="queueStats.pending"
/>
<DashboardIndicator
:title="$t('dashboard.queue.indicator.processing')"
icon="tabler:loader"
- color="blue"
+ variant="subtle"
:value="queueStats.processing"
/>
<DashboardIndicator
:title="$t('dashboard.queue.indicator.completed')"
icon="tabler:check"
- color="green"
+ variant="subtle"
:value="queueStats.completed"
/>
<DashboardIndicator
:title="$t('dashboard.queue.indicator.failed')"
icon="tabler:alert-triangle"
- color="red"
+ variant="subtle"
:value="queueStats.failed"
/>
</div>
@@ -445,7 +458,10 @@ useIntervalFn(refreshData, 10000) // 每10秒刷新一次
{{ $t('dashboard.queue.buttons.retry') }}
</UButton>
<UButton
- v-if="row.original.status !== 'in-stage'"
+ v-if="
+ row.original.status === 'completed' ||
+ row.original.status === 'failed'
+ "
icon="tabler:trash"
size="xs"
variant="soft"
diff --git a/i18n/locales/en.json b/i18n/locales/en.json
index 65270bb0..2ce7e4b0 100644
--- a/i18n/locales/en.json
+++ b/i18n/locales/en.json
@@ -1056,6 +1056,7 @@
"clearFailed": "Clear Failed",
"clearAll": "Clear All",
"retrySelected": "Retry Selected",
+ "moreActions": "More actions",
"retryAll": "Retry All Failed",
"clearNonActive": "Clear Non-Active"
},
diff --git a/i18n/locales/ja.json b/i18n/locales/ja.json
index 6e871858..31248842 100644
--- a/i18n/locales/ja.json
+++ b/i18n/locales/ja.json
@@ -982,7 +982,8 @@
"clearAll": "すべてクリア",
"retrySelected": "選択した項目を再試行",
"retryAll": "すべての失敗を再試行",
- "clearNonActive": "非アクティブタスクをクリア"
+ "clearNonActive": "非アクティブタスクをクリア",
+ "moreActions": "その他の操作"
},
"table": {
"id": "タスクID",
diff --git a/i18n/locales/ru.json b/i18n/locales/ru.json
index f0655c7a..1260e12a 100644
--- a/i18n/locales/ru.json
+++ b/i18n/locales/ru.json
@@ -1056,6 +1056,7 @@
"clearFailed": "Удалить задачи с ошибкой",
"clearAll": "Удалить все",
"retrySelected": "Повторить выбранные",
+ "moreActions": "Другие действия",
"retryAll": "Повторить все задачи с ошибкой",
"clearNonActive": "Удалить неактивные"
},
diff --git a/i18n/locales/zh-Hans.json b/i18n/locales/zh-Hans.json
index dc36eea9..06af00fc 100644
--- a/i18n/locales/zh-Hans.json
+++ b/i18n/locales/zh-Hans.json
@@ -1056,6 +1056,7 @@
"clearFailed": "清理失败任务",
"clearAll": "清理全部",
"retrySelected": "重试选中",
+ "moreActions": "更多操作",
"retryAll": "重试全部失败",
"clearNonActive": "清理非活跃任务"
},
diff --git a/i18n/locales/zh-Hant-HK.json b/i18n/locales/zh-Hant-HK.json
index 3f2e2c5f..621d135c 100644
--- a/i18n/locales/zh-Hant-HK.json
+++ b/i18n/locales/zh-Hant-HK.json
@@ -981,6 +981,7 @@
"clearFailed": "清理失敗任務",
"clearAll": "清理全部",
"retrySelected": "重試選中",
+ "moreActions": "更多操作",
"retryAll": "重試全部失敗",
"clearNonActive": "清理非活躍任務"
},
diff --git a/i18n/locales/zh-Hant-TW.json b/i18n/locales/zh-Hant-TW.json
index eaa209df..7c2d59bd 100644
--- a/i18n/locales/zh-Hant-TW.json
+++ b/i18n/locales/zh-Hant-TW.json
@@ -981,6 +981,7 @@
"clearFailed": "清理失敗任務",
"clearAll": "清理全部",
"retrySelected": "重試選中",
+ "moreActions": "更多操作",
"retryAll": "重試全部失敗",
"clearNonActive": "清理非活躍任務"
},
diff --git a/server/api/queue/task/[taskId].delete.ts b/server/api/queue/task/[taskId].delete.ts
new file mode 100644
index 00000000..539ad308
--- /dev/null
+++ b/server/api/queue/task/[taskId].delete.ts
@@ -0,0 +1,44 @@
+import { and, eq, inArray } from 'drizzle-orm'
+
+/** 删除指定的已完成或失败任务。 */
+export default defineEventHandler(async (event) => {
+ await requireUserSession(event)
+
+ const taskId = Number(getRouterParam(event, 'taskId'))
+ if (!Number.isSafeInteger(taskId) || taskId <= 0) {
+ throw createError({
+ statusCode: 400,
+ statusMessage: 'Task ID must be a positive integer',
+ })
+ }
+
+ const db = useDB()
+ // 将状态限制放在删除语句中,避免删除已被重试的任务。
+ const deletedTask = db
+ .delete(tables.pipelineQueue)
+ .where(
+ and(
+ eq(tables.pipelineQueue.id, taskId),
+ inArray(tables.pipelineQueue.status, ['completed', 'failed']),
+ ),
+ )
+ .returning({ id: tables.pipelineQueue.id })
+ .get()
+
+ if (!deletedTask) {
+ const task = db
+ .select({ id: tables.pipelineQueue.id })
+ .from(tables.pipelineQueue)
+ .where(eq(tables.pipelineQueue.id, taskId))
+ .get()
+
+ throw createError({
+ statusCode: task ? 409 : 404,
+ statusMessage: task
+ ? 'Only completed or failed tasks can be deleted'
+ : 'Task not found',
+ })
+ }
+
+ return { success: true, taskId: deletedTask.id }
+})
diff --git a/tests/queue-task-delete.test.ts b/tests/queue-task-delete.test.ts
new file mode 100644
index 00000000..96a4bb8d
--- /dev/null
+++ b/tests/queue-task-delete.test.ts
@@ -0,0 +1,86 @@
+import assert from 'node:assert/strict'
+import test from 'node:test'
+import Database from 'better-sqlite3'
+import { drizzle } from 'drizzle-orm/better-sqlite3'
+import { pipelineQueue } from '../server/database/schema.ts'
+
+test('single task deletion only removes finished queue records', async (t) => {
+ const sqlite = new Database(':memory:')
+ sqlite.exec(
+ 'CREATE TABLE pipeline_queue (id INTEGER PRIMARY KEY, status TEXT NOT NULL)',
+ )
+ const db = drizzle(sqlite)
+ const globals = {
+ defineEventHandler: (handler: unknown) => handler,
+ requireUserSession: async (event: { authenticated: boolean }) => {
+ if (!event.authenticated)
+ throw Object.assign(new Error('Unauthorized'), { statusCode: 401 })
+ },
+ getRouterParam: (event: { taskId: string }) => event.taskId,
+ createError: (options: { statusCode: number; statusMessage: string }) =>
+ Object.assign(new Error(options.statusMessage), options),
+ useDB: () => db,
+ tables: { pipelineQueue },
+ }
+ const previous = Object.getOwnPropertyDescriptors(globalThis)
+ Object.assign(globalThis, globals)
+
+ try {
+ const { default: handler } =
+ await import('../server/api/queue/task/[taskId].delete.ts')
+ const call = (taskId: string, authenticated = true) =>
+ handler({ taskId, authenticated } as never)
+ const seed = (status: string) => {
+ sqlite.exec('DELETE FROM pipeline_queue')
+ sqlite
+ .prepare('INSERT INTO pipeline_queue VALUES (1, ?), (2, ?)')
+ .run(status, 'completed')
+ }
+ const remaining = () =>
+ sqlite.prepare('SELECT id FROM pipeline_queue ORDER BY id').all()
+
+ for (const status of ['completed', 'failed']) {
+ await t.test(
+ `deletes ${status} task without removing other records`,
+ async () => {
+ seed(status)
+ assert.deepEqual(await call('1'), { success: true, taskId: 1 })
+ assert.deepEqual(remaining(), [{ id: 2 }])
+ await assert.rejects(call('1'), { statusCode: 404 })
+ },
+ )
+ }
+
+ for (const status of ['pending', 'in-stages']) {
+ await t.test(
+ `rejects ${status} task, including a task retried before deletion`,
+ async () => {
+ seed(status)
+ await assert.rejects(call('1'), { statusCode: 409 })
+ assert.deepEqual(remaining(), [{ id: 1 }, { id: 2 }])
+ },
+ )
+ }
+
+ await t.test('rejects invalid IDs and missing tasks', async () => {
+ seed('completed')
+ for (const id of ['', 'abc', '0', '-1', '1.5', '9007199254740992']) {
+ await assert.rejects(call(id), { statusCode: 400 })
+ }
+ await assert.rejects(call('999'), { statusCode: 404 })
+ assert.deepEqual(remaining(), [{ id: 1 }, { id: 2 }])
+ })
+
+ await t.test('requires authentication before deleting', async () => {
+ seed('completed')
+ await assert.rejects(call('1', false), { statusCode: 401 })
+ assert.deepEqual(remaining(), [{ id: 1 }, { id: 2 }])
+ })
+ } finally {
+ for (const key of Object.keys(globals)) {
+ if (previous[key]) Object.defineProperty(globalThis, key, previous[key])
+ else Reflect.deleteProperty(globalThis, key)
+ }
+ sqlite.close()
+ }
+})