oms/backend/app.js
liufumingren fc58f79b42 feat(backend): 新增商品导出任务
- 新增 export_tasks 表与模型(迁移 20260916000000),记录导出类型、状态、进度、
  文件名与错误信息
- app/service/exportTask.js:生成 CSV(exceljs)并落盘到 upload/exports/<主体>/<年>/<月>/<日>
- app/schedule/exportWorker.js 定时执行待处理任务,cleanupExport.js 清理过期文件
- 接口:POST /api/export-tasks 提交、GET /api/export-tasks 列表与进度、
  GET /api/export-tasks/:id/download 下载
- app/utils/exportDefs.js 定义各导出类型的数据源;warehouseProduct / stockRecord 补充
  库存相关数据
- 契约补 exportTask 系列与 apikey 安全定义;.eslintrc 放行相关文件与 apikey 标签
- package-lock 同步
2026-09-14 22:14:23 +08:00

44 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 应用启动自定义(Boot 类)
* - didReady:应用启动完成后,把遗留的「处理中」导出任务重置为「待处理」,交给全局队列重跑
* (避免上次进程异常退出导致任务永久卡在处理中)。
* - beforeClose:egg-scripts(npm start)优雅退出时,把「处理中」任务重置为「待处理」,
* 使其在下一次启动时被重新认领处理(仅对生产环境生效,开发环境 local 不干预,避免刷新即重置)。
*/
class AppBootHook {
constructor(app) {
this.app = app;
}
async didReady() {
if (this.app.config.env === 'local') return;
try {
const ExportTask = this.app.model.ExportTask;
if (ExportTask) {
const [ n ] = await ExportTask.update({ status: 0 }, { where: { status: 1 } });
this.app.logger.info('[export] 启动重置处理中任务为待处理,共 ' + (n || 0) + ' 条');
}
} catch (e) {
this.app.logger.error('[export] 启动重置处理中任务失败', e);
}
}
async beforeClose() {
if (this.app.config.env === 'local') return;
try {
const ExportTask = this.app.model.ExportTask;
if (ExportTask) {
// 有进行中的任务:重置为待处理,随后再退出(由 egg-scripts 的 SIGTERM 流程触发)
await ExportTask.update({ status: 0 }, { where: { status: 1 } });
this.app.logger.info('[export] 优雅退出:已将处理中任务重置为待处理');
}
} catch (e) {
this.app.logger.error('[export] 优雅退出重置任务失败', e);
}
}
}
module.exports = AppBootHook;