- 新增 wms_waybill_allocate_queue 队列表(migration + model),订单创建异步流程仅入队(截单/未审核不入队) - 新增 consume_waybill_allocate_queue 定时任务:15s/worker/重入保护,按仓库+货主串行批量消费,同关系下同一时刻仅一个取号事务 - express_platform 三处结算事务统一先 FOR UPDATE wms_store_warehouse 再更新 wms_order_express(lockAndDeductWaybillSettlement),消除 AB-BA 死锁
108 lines
3.3 KiB
JavaScript
108 lines
3.3 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 创建订单创建后自动分配快递的消费队列表 wms_waybill_allocate_queue。
|
||
*
|
||
* 背景:订单创建后的异步流程原为逐单直接取号,大批量订单创建时会产生 N 条并发
|
||
* createWaybillCode 回写事务与结算事务的 AB-BA 死锁(wms_store_warehouse 与
|
||
* wms_order_express 锁序相反)。改造为创建时只入队(截单/未审核不入队),由定时
|
||
* 任务按仓库+货主串行批量消费。
|
||
*
|
||
* 字段:
|
||
* - order_id / business_key:待分配运单的订单与业务键(与 wms_order_express 一致)
|
||
* - warehouse_id / store_id:消费分组与数据隔离维度
|
||
* - status:pending(待消费)/ processing(消费中)/ done(完成)/ failed(超限失败)
|
||
* - retry_count / next_retry_at / error_message:重试与失败信息
|
||
*
|
||
* 索引:
|
||
* - uk_order_id_business_key:幂等入队唯一键
|
||
* - idx_status_id:消费拉取(status=pending 按 id 升序)
|
||
* - idx_status_updated_at:处理中兜底恢复扫描
|
||
*/
|
||
|
||
const TABLE_NAME = 'wms_waybill_allocate_queue';
|
||
|
||
module.exports = {
|
||
async up(queryInterface, Sequelize) {
|
||
const { BIGINT, INTEGER, STRING, DATE } = Sequelize;
|
||
await queryInterface.createTable(TABLE_NAME, {
|
||
id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
primaryKey: true,
|
||
autoIncrement: true,
|
||
allowNull: false,
|
||
comment: '主键',
|
||
},
|
||
order_id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
comment: '订单ID(关联订单分表)',
|
||
},
|
||
business_key: {
|
||
type: INTEGER(3),
|
||
allowNull: false,
|
||
defaultValue: 1,
|
||
comment: '业务键(与 wms_order_express.business_key 一致)',
|
||
},
|
||
warehouse_id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
comment: '仓库ID(消费分组与数据隔离维度)',
|
||
},
|
||
store_id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
defaultValue: 0,
|
||
comment: '货主ID(消费分组维度)',
|
||
},
|
||
status: {
|
||
type: STRING(20),
|
||
allowNull: false,
|
||
defaultValue: 'pending',
|
||
comment: '状态:pending/processing/done/failed',
|
||
},
|
||
retry_count: {
|
||
type: INTEGER(3),
|
||
allowNull: false,
|
||
defaultValue: 0,
|
||
comment: '重试次数',
|
||
},
|
||
next_retry_at: {
|
||
type: DATE,
|
||
allowNull: true,
|
||
comment: '下次可消费时间(失败退避)',
|
||
},
|
||
error_message: {
|
||
type: STRING(500),
|
||
allowNull: true,
|
||
comment: '最近一次失败原因',
|
||
},
|
||
created_at: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
comment: '创建时间',
|
||
},
|
||
updated_at: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
comment: '更新时间',
|
||
},
|
||
});
|
||
|
||
await queryInterface.addIndex(TABLE_NAME, [ 'order_id', 'business_key' ], {
|
||
name: 'uk_order_id_business_key',
|
||
unique: true,
|
||
});
|
||
await queryInterface.addIndex(TABLE_NAME, [ 'status', 'id' ], {
|
||
name: 'idx_status_id',
|
||
});
|
||
await queryInterface.addIndex(TABLE_NAME, [ 'status', 'updated_at' ], {
|
||
name: 'idx_status_updated_at',
|
||
});
|
||
},
|
||
|
||
async down(queryInterface) {
|
||
await queryInterface.dropTable(TABLE_NAME);
|
||
},
|
||
};
|