'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;