Files
MikrotikManager/backend/src/services/backup-job-execution.ts
T

25 lines
1.8 KiB
TypeScript

import type { BackupJobStatusResponse } from "@mmapp/contracts/backups"
import type { AppendEventBody } from "@mmapp/contracts/events"
import type { BackupMeta } from "./backup-service.js"
export async function recordBackupEvent(input: AppendEventBody, append: (input: AppendEventBody) => Promise<void>, warn: (error: unknown) => void): Promise<void> {
try { await append(input) } catch (error) { warn(error) }
}
export async function processBackupJob(job: BackupJobStatusResponse, ids: string[], notes: string | undefined, deps: {
create: (id: string, kind: "manual", notes?: string) => Promise<BackupMeta>
event: (input: AppendEventBody) => Promise<void>
}) {
job.status = "running"
job.startedAt = new Date().toISOString()
for (const id of ids) {
try { job.created.push(await deps.create(id, "manual", notes)) }
catch (error) { job.failures.push({ serverId: id, error: error instanceof Error ? error.message : String(error) }) }
finally { job.completed += 1 }
}
job.status = "done"
job.finishedAt = new Date().toISOString()
const uploadFailures = job.created.filter(item => item.uploadError)
const partial = job.failures.length > 0 || uploadFailures.length > 0
await deps.event({ level: partial ? "warning" : "info", eventType: "backups.job.done", sourceModule: "backups", title: partial ? "Бэкап завершен с ошибками" : "Бэкап завершен", message: "Создано: " + job.created.length + ", ошибок устройств: " + job.failures.length + ", ошибок загрузки: " + uploadFailures.length, entityType: "backup_job", entityId: job.id, payload: { total: job.total, completed: job.completed, failures: job.failures, uploadFailures: uploadFailures.map(item => ({ id: item.id, error: item.uploadError })), notes: notes ?? null } })
}