'use strict'; /** * 定时消费订单创建时入队的自动分配快递队列(wms_waybill_allocate_queue)。 * * 背景:订单创建后的异步流程逐单直接取号,大批量订单创建时 N 条并发取号事务 * 与结算事务产生 wms_store_warehouse / wms_order_express 的 AB-BA 死锁。 * 本任务按仓库+货主串行消费,保证同一签约关系下同一时刻只有一个取号事务在跑。 * * 截单语义:订单创建时已判定(截单/未审核不入队),已入队的正常消费,消费侧不再校验截单。 * 间隔 15 秒、type: worker(仅一个 worker 执行),带重入保护。 */ const MAX_CONSUME_ROWS_PER_WINDOW = 200; const MAX_RETRY_COUNT = 3; const RETRY_BASE_DELAY_MS = 60 * 1000; const PROCESSING_STALE_MS = 10 * 60 * 1000; const MAX_ERROR_MESSAGE_LENGTH = 500; module.exports = { schedule: { interval: '15s', type: 'worker', immediate: false, disable: false, }, async task(ctx) { // 防止重入 if (ctx.app._consumeWaybillAllocateQueueRunning) return; ctx.app._consumeWaybillAllocateQueueRunning = true; try { await consumePendingRows(ctx); } catch (err) { ctx.logger.error(`[consume-waybill-allocate-queue] 消费异常: ${err.message}`, { stack: err.stack }); } finally { ctx.app._consumeWaybillAllocateQueueRunning = false; } }, }; /** * 批量更新队列行状态。 * @param {Object} ctx Egg 上下文。 * @param {Array} rows 队列行(含 id)。 * @param {string} status 目标状态。 * @param {string|null} errorMessage 失败原因。 * @return {Promise} 无返回值。 */ async function updateQueueRowsStatus(ctx, rows, status, errorMessage) { const queueModel = ctx.model && ctx.model.WaybillAllocateQueue; if (!queueModel || !Array.isArray(rows) || rows.length === 0) { return; } const { Op } = ctx.app.Sequelize; const ids = rows.map(row => Number(row.id)).filter(id => Number.isInteger(id) && id > 0); if (ids.length === 0) { return; } await queueModel.update({ status, error_message: errorMessage ? String(errorMessage).slice(0, MAX_ERROR_MESSAGE_LENGTH) : null, updated_at: new Date(), }, { where: { id: { [Op.in]: ids, }, }, }); } /** * 组消费失败或未完成时:重试次数+1,未超限退回 pending(带退避),超限转 failed。 * @param {Object} ctx Egg 上下文。 * @param {Array} rows 队列行。 * @param {string} errorMessage 失败原因。 * @param {boolean} failImmediately 是否立即判失败(如 warehouse_id 缺失,重试无意义)。 * @return {Promise} 无返回值。 */ async function markGroupRetryOrFailed(ctx, rows, errorMessage, failImmediately) { const queueModel = ctx.model && ctx.model.WaybillAllocateQueue; if (!queueModel || !Array.isArray(rows) || rows.length === 0) { return; } const normalizedErrorMessage = String(errorMessage || '运单分配失败'); for (const row of rows) { const rowId = Number(row.id); if (!Number.isInteger(rowId) || rowId <= 0) { continue; } const retryCount = Number(row.retry_count) || 0; if (failImmediately || retryCount + 1 >= MAX_RETRY_COUNT) { await queueModel.update({ status: 'failed', retry_count: retryCount + 1, error_message: normalizedErrorMessage.slice(0, MAX_ERROR_MESSAGE_LENGTH), updated_at: new Date(), }, { where: { id: rowId }, }); } else { const delay = (retryCount + 1) * RETRY_BASE_DELAY_MS; await queueModel.update({ status: 'pending', retry_count: retryCount + 1, next_retry_at: new Date(Date.now() + delay), error_message: null, updated_at: new Date(), }, { where: { id: rowId }, }); } } } /** * 拉取并消费待分配队列。 * @param {Object} ctx Egg 上下文。 * @return {Promise} 无返回值。 */ async function consumePendingRows(ctx) { const queueModel = ctx.model && ctx.model.WaybillAllocateQueue; const expressService = ctx.service && ctx.service.systems && ctx.service.systems.express; if (!queueModel || typeof queueModel.findAll !== 'function' || !expressService || typeof expressService.createWaybillCodePipeline !== 'function') { return; } const { Op } = ctx.app.Sequelize; // 1) 兜底恢复:上次运行中断遗留的 processing 行退回 pending await queueModel.update({ status: 'pending', updated_at: new Date(), }, { where: { status: 'processing', updated_at: { [Op.lt]: new Date(Date.now() - PROCESSING_STALE_MS), }, }, }); // 2) 拉取待消费行 const now = new Date(); const pendingRows = await queueModel.findAll({ where: { status: 'pending', [Op.or]: [ { next_retry_at: null }, { next_retry_at: { [Op.lte]: now } }, ], }, order: [[ 'id', 'ASC' ]], limit: MAX_CONSUME_ROWS_PER_WINDOW, raw: true, }); if (!Array.isArray(pendingRows) || pendingRows.length === 0) { return; } // 3) 标记消费中(单 worker + 重入保护下仅作状态记录) const pendingIds = pendingRows.map(row => Number(row.id)).filter(id => Number.isInteger(id) && id > 0); if (pendingIds.length > 0) { await queueModel.update({ status: 'processing', updated_at: new Date(), }, { where: { id: { [Op.in]: pendingIds, }, }, }); } // 4) 按 warehouse_id + store_id 分组,逐组串行消费 const groupMap = new Map(); for (const row of pendingRows) { const warehouseId = Number(row.warehouse_id) || 0; const storeId = Number(row.store_id) || 0; const groupKey = `${warehouseId}:${storeId}`; if (!groupMap.has(groupKey)) { groupMap.set(groupKey, { warehouseId, storeId, rows: [], }); } groupMap.get(groupKey).rows.push(row); } const orderExpressModel = ctx.model && ctx.model.OrderExpress; for (const group of groupMap.values()) { const orders = group.rows .map(row => { const orderId = Number(row.order_id); const businessKey = Number(row.business_key) || 1; return { order_id: orderId, business_key: businessKey }; }) .filter(order => Number.isInteger(order.order_id) && order.order_id > 0); if (group.warehouseId <= 0 || orders.length === 0) { await markGroupRetryOrFailed(ctx, group.rows, '队列数据缺少有效 warehouse_id/order_id', true); continue; } try { await expressService.createWaybillCodePipeline(orders, { awaitTasks: true, warehouseId: group.warehouseId, }); // 5) 判定结果:取到单号(status=1)的订单视为完成 const doneKeySet = new Set(); if (orderExpressModel && typeof orderExpressModel.findAll === 'function') { const doneRows = await orderExpressModel.findAll({ where: { warehouse_id: group.warehouseId, [Op.or]: orders.map(order => ({ order_id: order.order_id, business_key: order.business_key, })), status: 1, }, attributes: [ 'order_id', 'business_key' ], raw: true, }); for (const doneRow of Array.isArray(doneRows) ? doneRows : []) { doneKeySet.add(`${Number(doneRow.order_id)}:${Number(doneRow.business_key)}`); } } const doneRows = group.rows.filter(row => doneKeySet.has(`${Number(row.order_id)}:${Number(row.business_key) || 1}`)); const restRows = group.rows.filter(row => !doneKeySet.has(`${Number(row.order_id)}:${Number(row.business_key) || 1}`)); await updateQueueRowsStatus(ctx, doneRows, 'done', null); await markGroupRetryOrFailed(ctx, restRows, '运单分配未完成', false); ctx.logger.info(`[consume-waybill-allocate-queue] 组消费完成 warehouse_id=${group.warehouseId} store_id=${group.storeId} total=${group.rows.length} done=${doneRows.length} rest=${restRows.length}`); } catch (error) { ctx.logger.error(`[consume-waybill-allocate-queue] 组消费失败 warehouse_id=${group.warehouseId} store_id=${group.storeId}: ${error.message}`); await markGroupRetryOrFailed(ctx, group.rows, error && error.message ? error.message : String(error), false); } } }