StartLepao.js 3.0 KB

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