runforgeTaskMq.js 933 B

123456789101112131415161718192021222324252627282930
  1. /** JKES 乐跑任务队列(已无旧版工商队列) */
  2. const TASK_QUEUE = 'jkes_runforge_task_queue'
  3. async function assertRunforgeTaskIngress(channel, logger) {
  4. await channel.assertQueue(TASK_QUEUE, { durable: true })
  5. return { mode: 'direct', queue: TASK_QUEUE }
  6. }
  7. /**
  8. * 投递乐跑任务 JSON 消息体(与 Worker 消费格式一致)
  9. */
  10. function publishRunforgeTask(channel, messageObject, logger) {
  11. const body = Buffer.from(JSON.stringify(messageObject))
  12. const ok = channel.sendToQueue(TASK_QUEUE, body, {
  13. persistent: true,
  14. contentType: 'application/json'
  15. })
  16. if (!ok) {
  17. throw new Error(`MQ 背压,未能写入队列 ${TASK_QUEUE}`)
  18. }
  19. logger?.info?.(
  20. `乐跑任务已投递 MQ queue=${TASK_QUEUE} id=${messageObject?.id} type=${messageObject?.type}`
  21. )
  22. }
  23. module.exports = {
  24. TASK_QUEUE,
  25. assertRunforgeTaskIngress,
  26. publishRunforgeTask
  27. }