Browse Source

自动乐跑增加随机延迟

Pchen. 1 month ago
parent
commit
0078b44a5c
3 changed files with 43 additions and 26 deletions
  1. 40 23
      apis/Corn/StartAutoLepao.js
  2. 2 2
      lib/Lepao/Worker.js
  3. 1 1
      plugin/Email/Email.js

+ 40 - 23
apis/Corn/StartAutoLepao.js

@@ -34,9 +34,24 @@ class StartAutoLepao extends API {
             if (!r)
             if (!r)
                 return this.logger.error('获取自动乐跑账号失败!')
                 return this.logger.error('获取自动乐跑账号失败!')
 
 
+            // 为本小时内随机打散投递时间,减轻瞬时并发(0 ~ 当前小时剩余毫秒数)
+            const nowMs = Date.now()
+            const hourEnd = new Date()
+            hourEnd.setHours(hourEnd.getHours() + 1, 0, 0, 0)
+            const spreadWindowMs = Math.max(0, hourEnd.getTime() - nowMs)
+
+            let channel
+            try {
+                channel = await mq.getChannel('lepao_corn')
+                await channel.assertQueue('runforge_task_queue', { durable: true })
+            } catch (err) {
+                this.logger.error(`自动乐跑:连接 MQ 失败:${err.message || err}`)
+                return
+            }
+
             for (const item of r) {
             for (const item of r) {
                 const { name, create_user, student_num, token, uid, school_id, state } = item
                 const { name, create_user, student_num, token, uid, school_id, state } = item
-                this.logger.info(`${name}(${student_num})开始乐跑`)
+                this.logger.info(`${name}(${student_num})将加入自动乐跑(本小时内随机调度)`)
 
 
                 const isSuccess = await Redis.get(`lepaoSuccess:${student_num}`)
                 const isSuccess = await Redis.get(`lepaoSuccess:${student_num}`)
                 if (isSuccess) {
                 if (isSuccess) {
@@ -44,30 +59,32 @@ class StartAutoLepao extends API {
                     continue
                     continue
                 }
                 }
 
 
-                try {
-                    const channel = await mq.getChannel('lepao_corn')
-                    await channel.assertQueue('runforge_task_queue', { durable: true })
+                const delayMs = spreadWindowMs > 0 ? Math.floor(Math.random() * spreadWindowMs) : 0
+                const fireAt = nowMs + delayMs
 
 
-                    const taskId = `lepao:auto:${Date.now()}:${student_num}`
-                    const payload = {
-                        id: taskId,
-                        type: 'lepao.startRun',
-                        data: {
-                            taskId,
-                            account: student_num
-                        },
-                        retry: 0
-                    }
+                setTimeout(() => {
+                    try {
+                        const taskId = `lepao:auto:${fireAt}:${student_num}`
+                        const payload = {
+                            id: taskId,
+                            type: 'lepao.startRun',
+                            data: {
+                                taskId,
+                                account: student_num
+                            },
+                            retry: 0
+                        }
 
 
-                    channel.sendToQueue(
-                        'runforge_task_queue',
-                        Buffer.from(JSON.stringify(payload)),
-                        { persistent: true, contentType: 'application/json' }
-                    )
-                    this.logger.info(`${name}(${student_num})已投递自动乐跑任务`)
-                } catch (err) {
-                    this.logger.error(`${name}(${student_num})乐跑失败:${err.message || err}`)
-                }
+                        channel.sendToQueue(
+                            'runforge_task_queue',
+                            Buffer.from(JSON.stringify(payload)),
+                            { persistent: true, contentType: 'application/json' }
+                        )
+                        this.logger.info(`${name}(${student_num})已投递自动乐跑任务(延迟约 ${Math.round(delayMs / 1000)}s)`)
+                    } catch (err) {
+                        this.logger.error(`${name}(${student_num})乐跑投递失败:${err.message || err}`)
+                    }
+                }, delayMs)
             }
             }
         } catch (error) {
         } catch (error) {
             this.logger.error(error)
             this.logger.error(error)

+ 2 - 2
lib/Lepao/Worker.js

@@ -140,8 +140,8 @@ class Worker {
         if (!uuid || !account || !result || !pathId) return
         if (!uuid || !account || !result || !pathId) return
         try {
         try {
             const time = Date.now()
             const time = Date.now()
-            const sql = 'INSERT INTO lepao_record (uuid, time, lepao_account, result, path_id, point_data) VALUES (?, ?, ?, ?, ?, ?)'
-            await db.query(sql, [uuid, time, account, result, pathId, JSON.stringify(pointData || [])])
+            const sql = 'INSERT INTO lepao_record (uuid, time, lepao_account, result, path_id, point_data, state) VALUES (?, ?, ?, ?, ?, ?, ?)'
+            await db.query(sql, [uuid, time, account, result, pathId, JSON.stringify(pointData || []), 1])
         } catch (error) {
         } catch (error) {
             this.logger.error(`写入乐跑记录失败: ${error.stack || error}`)
             this.logger.error(`写入乐跑记录失败: ${error.stack || error}`)
         }
         }

+ 1 - 1
plugin/Email/Email.js

@@ -36,7 +36,7 @@ async function sendEmail(email, subject, content) {
 
 
             try {
             try {
                 await transporter.sendMail(mail)
                 await transporter.sendMail(mail)
-                logger.info(`邮件发送成功,使用账号: ${currentConfig.user}`)
+                logger.info(`${email}邮件发送成功,使用账号: ${currentConfig.user}`)
                 return resolve()
                 return resolve()
             } catch (error) {
             } catch (error) {
                 logger.error(`邮件发送失败 (${currentConfig.user}),错误:`, error.stack)
                 logger.error(`邮件发送失败 (${currentConfig.user}),错误:`, error.stack)