StartLepao.js 3.6 KB

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