StartLepao.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const API = require("../../lib/API");
  2. const db = require('../../plugin/DataBase/db')
  3. const Redis = require('../../plugin/DataBase/Redis')
  4. const mq = require('../../plugin/mq')
  5. const { BaseStdResponse } = require("../../BaseStdResponse")
  6. // 出现异常情况时补充乐跑
  7. class StartLepao extends API {
  8. constructor() {
  9. super();
  10. this.noEncrypt()
  11. this.setPath('/Corn/StartLepao');
  12. this.setMethod('GET');
  13. }
  14. async onRequest(req, res) {
  15. try {
  16. let { time } = req.query
  17. if ([time].some(value => value === '' || value === null || value === undefined))
  18. return res.json({
  19. ...BaseStdResponse.MISSING_PARAMETER
  20. })
  21. res.json({
  22. ...BaseStdResponse.OK
  23. })
  24. this.logger.info('开始执行补充乐跑任务')
  25. const day = new Date().getDay()
  26. let sql = `
  27. SELECT name, create_user, student_num, token, uid, school_id, state
  28. FROM lepao_account
  29. WHERE auto_run = 1
  30. AND JSON_CONTAINS(auto_day, CAST(? AS JSON))
  31. AND (auto_time = ? OR (auto_time = -1 AND today_auto_time = ?))
  32. `
  33. let r = await db.query(sql, [day, time, time])
  34. if (!r)
  35. return this.logger.error('获取补充乐跑账号失败!')
  36. for (const item of r) {
  37. const { name, create_user, student_num, token, uid, school_id, state } = item
  38. this.logger.info(`${name}(${student_num})开始补充乐跑`)
  39. const isSuccess = await Redis.get(`lepaoSuccess:${student_num}`)
  40. if (isSuccess) {
  41. this.logger.info(`${name}(${student_num})当天已乐跑成功,不执行补充乐跑`)
  42. continue
  43. }
  44. try {
  45. const channel = await mq.getChannel('lepao_corn')
  46. await channel.assertQueue('task_queue', { durable: true })
  47. const taskId = `lepao:repair:${Date.now()}:${student_num}`
  48. const payload = {
  49. id: taskId,
  50. type: 'lepao.startRun',
  51. data: {
  52. taskId,
  53. account: student_num
  54. },
  55. retry: 0
  56. }
  57. channel.sendToQueue(
  58. 'task_queue',
  59. Buffer.from(JSON.stringify(payload)),
  60. { persistent: true, contentType: 'application/json' }
  61. )
  62. this.logger.info(`${name}(${student_num})已投递补充乐跑任务`)
  63. } catch (err) {
  64. this.logger.error(`${name}(${student_num})补充乐跑失败:${err.message || err}`)
  65. }
  66. }
  67. this.logger.info('补充乐跑任务执行完毕')
  68. } catch (error) {
  69. this.logger.error(error)
  70. }
  71. }
  72. }
  73. module.exports.StartLepao = StartLepao