fix-delay-simple.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env node
  2. /**
  3. * 简单修复延迟任务脚本
  4. * 直接使用数据库查询和更新
  5. */
  6. const { Client } = require('pg');
  7. async function main() {
  8. console.log('=== 延迟任务简单修复脚本 ===\n');
  9. const client = new Client({
  10. host: '127.0.0.1',
  11. port: 5432,
  12. user: 'postgres',
  13. password: '',
  14. database: 'postgres'
  15. });
  16. try {
  17. await client.connect();
  18. console.log('数据库连接成功\n');
  19. // 1. 检查延迟任务
  20. console.log('1. 检查延迟任务状态...');
  21. const tasksResult = await client.query(`
  22. SELECT
  23. task_id,
  24. order_id,
  25. print_status,
  26. scheduled_at,
  27. EXTRACT(EPOCH FROM (NOW() - scheduled_at)) as seconds_late
  28. FROM feie_print_task_mt
  29. WHERE print_status IN ('DELAYED', 'PENDING')
  30. AND scheduled_at <= NOW()
  31. ORDER BY scheduled_at
  32. `);
  33. const tasks = tasksResult.rows;
  34. console.log(`找到 ${tasks.length} 个超时的延迟任务:`);
  35. tasks.forEach((task, index) => {
  36. console.log(` ${index + 1}. ${task.task_id} - 订单: ${task.order_id}`);
  37. console.log(` 状态: ${task.print_status}, 计划时间: ${task.scheduled_at}`);
  38. console.log(` 已超时: ${Math.round(task.seconds_late)} 秒\n`);
  39. });
  40. if (tasks.length === 0) {
  41. console.log('没有找到超时的延迟任务');
  42. return;
  43. }
  44. // 2. 检查订单状态
  45. console.log('2. 检查订单状态...');
  46. for (const task of tasks) {
  47. if (!task.order_id) continue;
  48. const orderResult = await client.query(`
  49. SELECT pay_state, state
  50. FROM order_mt
  51. WHERE id = $1 AND tenant_id = 1
  52. `, [task.order_id]);
  53. if (orderResult.rows.length === 0) {
  54. console.log(` 订单 ${task.order_id} 不存在,标记任务为取消`);
  55. await client.query(`
  56. UPDATE feie_print_task_mt
  57. SET print_status = 'CANCELLED',
  58. updated_at = NOW(),
  59. cancel_reason = 'ORDER_NOT_FOUND'
  60. WHERE task_id = $1
  61. `, [task.task_id]);
  62. } else {
  63. const order = orderResult.rows[0];
  64. console.log(` 订单 ${task.order_id}: 支付状态=${order.pay_state}, 订单状态=${order.state}`);
  65. // 检查是否已退款 (pay_state = 3) 或订单关闭 (state = 5)
  66. if (order.pay_state === 3 || order.state === 5) {
  67. console.log(` 订单 ${task.order_id} 已退款/关闭,取消打印任务`);
  68. await client.query(`
  69. UPDATE feie_print_task_mt
  70. SET print_status = 'CANCELLED',
  71. updated_at = NOW(),
  72. cancel_reason = 'REFUND'
  73. WHERE task_id = $1
  74. `, [task.task_id]);
  75. } else {
  76. console.log(` 订单 ${task.order_id} 正常,标记任务为失败(模拟打印失败)`);
  77. await client.query(`
  78. UPDATE feie_print_task_mt
  79. SET print_status = 'FAILED',
  80. updated_at = NOW(),
  81. error_message = '手动修复:任务超时未执行'
  82. WHERE task_id = $1
  83. `, [task.task_id]);
  84. }
  85. }
  86. }
  87. // 3. 验证结果
  88. console.log('\n3. 验证修复结果...');
  89. const remainingResult = await client.query(`
  90. SELECT COUNT(*) as count
  91. FROM feie_print_task_mt
  92. WHERE print_status IN ('DELAYED', 'PENDING')
  93. AND scheduled_at <= NOW()
  94. `);
  95. console.log(`剩余未处理的延迟任务: ${remainingResult.rows[0].count} 个`);
  96. if (remainingResult.rows[0].count > 0) {
  97. console.log('\n⚠️ 仍有未处理的延迟任务,建议:');
  98. console.log(' 1. 检查调度器是否启动');
  99. console.log(' 2. 检查飞鹅打印机配置');
  100. console.log(' 3. 检查网络连接');
  101. } else {
  102. console.log('\n✅ 所有超时延迟任务已处理');
  103. }
  104. } catch (error) {
  105. console.error('修复过程中发生错误:', error);
  106. } finally {
  107. await client.end();
  108. }
  109. console.log('\n=== 修复脚本执行完成 ===');
  110. }
  111. // 执行修复
  112. main().catch(console.error);