fix(exports): preserve processing state on async export failures
This commit is contained in:
parent
265599d005
commit
67ef6a6068
@ -63,17 +63,18 @@ export async function exportRoutes(app: FastifyInstance) {
|
|||||||
// Process async — respond immediately, update job status when done
|
// Process async — respond immediately, update job status when done
|
||||||
const log = req.log;
|
const log = req.log;
|
||||||
process.nextTick(async () => {
|
process.nextTick(async () => {
|
||||||
|
let currentJob = created;
|
||||||
try {
|
try {
|
||||||
const processingJob = await repo.updateExportJob({
|
currentJob = await repo.updateExportJob({
|
||||||
...created,
|
...currentJob,
|
||||||
status: 'processing',
|
status: 'processing',
|
||||||
startedAt: new Date().toISOString(),
|
startedAt: new Date().toISOString(),
|
||||||
});
|
});
|
||||||
const rows = await fetchExportData(created.type, access.productId, created.filters);
|
const rows = await fetchExportData(created.type, access.productId, created.filters);
|
||||||
const serialized = created.format === 'json' ? JSON.stringify(rows, null, 2) : toCsv(rows);
|
const serialized = created.format === 'json' ? JSON.stringify(rows, null, 2) : toCsv(rows);
|
||||||
const fileName = `${created.type}-${access.productId}-${Date.now()}.${created.format}`;
|
const fileName = `${created.type}-${access.productId}-${Date.now()}.${created.format}`;
|
||||||
await repo.updateExportJob({
|
currentJob = await repo.updateExportJob({
|
||||||
...processingJob,
|
...currentJob,
|
||||||
status: 'ready',
|
status: 'ready',
|
||||||
data: serialized,
|
data: serialized,
|
||||||
rowCount: rows.length,
|
rowCount: rows.length,
|
||||||
@ -86,7 +87,7 @@ export async function exportRoutes(app: FastifyInstance) {
|
|||||||
log.error({ err, exportId: created.id }, '[exports] Export job failed');
|
log.error({ err, exportId: created.id }, '[exports] Export job failed');
|
||||||
await repo
|
await repo
|
||||||
.updateExportJob({
|
.updateExportJob({
|
||||||
...created,
|
...currentJob,
|
||||||
status: 'failed',
|
status: 'failed',
|
||||||
error: err instanceof Error ? err.message : 'Unknown error',
|
error: err instanceof Error ? err.message : 'Unknown error',
|
||||||
completedAt: new Date().toISOString(),
|
completedAt: new Date().toISOString(),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user