// app/schedule/sync_finished_wave_batch.js 'use strict'; const EXECUTION_LOG_CACHE_KEY = 'schedule:sync_finished_wave_batch:last_execution'; const EXECUTION_LOG_CACHE_TTL = 300; // 5 分钟 const EXECUTION_LOG_HISTORY_CACHE_KEY = 'schedule:sync_finished_wave_batch:history'; const EXECUTION_LOG_HISTORY_MAX = 20; // 保留最近 20 次执行记录 const EXECUTION_LOG_HISTORY_TTL = 86400; // 1 天 /** * 将本次执行日志写入缓存。 * * @param {Object} ctx Egg 上下文。 * @param {Object} logEntry 日志条目。 * @return {Promise} */ async function writeExecutionLogToCache(ctx, logEntry) { try { if (ctx.app && typeof ctx.app.setCacheAsync === 'function') { await ctx.app.setCacheAsync(EXECUTION_LOG_CACHE_KEY, logEntry, EXECUTION_LOG_CACHE_TTL); } } catch (err) { ctx.logger.warn(`[sync-finished-wave-batch] 写入执行日志缓存失败: ${err.message}`); } } /** * 将本次执行日志追加到历史记录缓存(保留最近 N 次)。 * * @param {Object} ctx Egg 上下文。 * @param {Object} logEntry 日志条目。 * @return {Promise} */ async function appendExecutionHistory(ctx, logEntry) { try { if (typeof ctx.app.getCacheAsync !== 'function' || typeof ctx.app.setCacheAsync !== 'function') { return; } const rawHistory = await ctx.app.getCacheAsync(EXECUTION_LOG_HISTORY_CACHE_KEY); const history = Array.isArray(rawHistory) ? rawHistory : []; history.unshift(logEntry); await ctx.app.setCacheAsync( EXECUTION_LOG_HISTORY_CACHE_KEY, history.slice(0, EXECUTION_LOG_HISTORY_MAX), EXECUTION_LOG_HISTORY_TTL ); } catch (err) { ctx.logger.warn(`[sync-finished-wave-batch] 写入执行历史缓存失败: ${err.message}`); } } /** * 每 1 分钟在单 worker 中执行一次波次自动终结。 * * 将"订单已全部离开待处理表"的活动波次(wave_status IN (0,1))收敛为 * wave_status=5(复核完成),修复遗留 0 单波次在 batchList 中展示的问题。 */ module.exports = { schedule: { interval: '1m', type: 'worker', immediate: false, disable: false, }, /** * 执行波次自动终结任务。 * * @param {Object} ctx Egg 上下文。 * @return {Promise} 无返回值。 */ async task(ctx) { if (ctx.app._syncFinishedWaveBatchRunning) { return; } ctx.app._syncFinishedWaveBatchRunning = true; const startedAt = Date.now(); let status = 'success'; let errorMessage = ''; let result = null; try { result = await ctx.service.systems.waveBatchClosure.closeFinishedWaves(); if (result && result.closed_count > 0) { ctx.logger.info( `[sync-finished-wave-batch] 自动终结 ${result.closed_count} 个波次: ${JSON.stringify(result.closed_waves)}` ); } } catch (error) { status = 'error'; errorMessage = error.message; ctx.logger.error(`[sync-finished-wave-batch] 执行失败: ${error.message}`); } finally { ctx.app._syncFinishedWaveBatchRunning = false; } const logEntry = { executed_at: new Date().toISOString(), duration_ms: Date.now() - startedAt, status, error_message: errorMessage, candidate_count: result ? result.candidate_count : 0, empty_count: result ? result.empty_count : 0, closed_count: result ? result.closed_count : 0, pending_confirm_count: result ? result.pending_confirm_count : 0, unbound_count: result ? result.unbound_count : 0, misread_count: result ? result.misread_count : 0, probe_failed_count: result ? result.probe_failed_count : 0, warehouse_ids: result ? result.warehouse_ids : [], closed_waves: result ? result.closed_waves : [], }; await writeExecutionLogToCache(ctx, logEntry); await appendExecutionHistory(ctx, logEntry); ctx.logger.info( `[sync-finished-wave-batch] 本轮完成 closed=${result ? result.closed_count : 0} candidate=${result ? result.candidate_count : 0}` + ` pendingConfirm=${result ? result.pending_confirm_count : 0}` + ` unbound=${result ? result.unbound_count : 0} misread=${result ? result.misread_count : 0}` + ` probeFailed=${result ? result.probe_failed_count : 0}` + ` warehouses=${result ? result.warehouse_ids.length : 0}` ); }, };