StartLepao.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 l.name, l.student_num
  32. FROM lepao_account l
  33. INNER JOIN jw_account j
  34. ON j.create_user = l.create_user
  35. AND j.username = l.student_num
  36. AND j.state = 1
  37. AND j.password IS NOT NULL
  38. AND j.password <> ''
  39. WHERE l.auto_run = 1 AND l.state = 1
  40. AND JSON_CONTAINS(l.auto_day, CAST(? AS JSON))
  41. AND (l.auto_time = ? OR (l.auto_time = -1 AND l.today_auto_time = ?))
  42. `
  43. let r = await db.query(sql, [day, time, time])
  44. if (!r)
  45. return this.logger.error('获取补充乐跑账号失败!')
  46. for (const item of r) {
  47. const { name, student_num } = item
  48. this.logger.info(`${name}(${student_num})开始补充乐跑`)
  49. const isSuccess = await Redis.get(`lepaoSuccess:${student_num}`)
  50. if (isSuccess) {
  51. this.logger.info(`${name}(${student_num})当天已乐跑成功,不执行补充乐跑`)
  52. continue
  53. }
  54. try {
  55. const channel = await mq.getChannel('lepao_corn')
  56. await assertRunforgeTaskIngress(channel, this.logger)
  57. const taskId = `lepao:repair:${Date.now()}:${student_num}`
  58. const payload = {
  59. id: taskId,
  60. type: 'lepao.startRun',
  61. data: {
  62. taskId,
  63. account: student_num,
  64. runMode: 'auto'
  65. },
  66. retry: 0
  67. }
  68. publishRunforgeTask(channel, payload)
  69. this.logger.info(`${name}(${student_num})已投递补充乐跑任务`)
  70. } catch (err) {
  71. this.logger.error(`${name}(${student_num})补充乐跑失败:${err.message || err}`)
  72. }
  73. }
  74. this.logger.info('补充乐跑任务执行完毕')
  75. } catch (error) {
  76. this.logger.error(error)
  77. }
  78. }
  79. }
  80. module.exports.StartLepao = StartLepao