فهرست منبع

已发送优化

wuyi 1 سال پیش
والد
کامیت
0e080e9254
1فایلهای تغییر یافته به همراه54 افزوده شده و 44 حذف شده
  1. 54 44
      src/task/task.service.ts

+ 54 - 44
src/task/task.service.ts

@@ -75,54 +75,64 @@ export class TaskService implements OnModuleInit {
         if ((task && task.status === TaskStatus.IDLE) || task.status === TaskStatus.PAUSE) {
         if ((task && task.status === TaskStatus.IDLE) || task.status === TaskStatus.PAUSE) {
             task.status = TaskStatus.PENDING
             task.status = TaskStatus.PENDING
             await this.taskRepository.save(task)
             await this.taskRepository.save(task)
-            const newTask = await this.runTask(task)
+            await this.runTask(task)
 
 
-            try {
-                const successCount = await this.taskItemRepository.countBy({
-                    taskId: id,
-                    status: 'success'
-                })
-                const totalCount = await this.taskItemRepository.countBy({
-                    taskId: id
-                })
-                if (totalCount === 0) {
-                    throw new Error('No tasks found for the given taskId.')
-                }
-                // 计算成功率
-                const successRate = ((successCount / totalCount) * 100).toFixed(1) + '%'
-                newTask.successRate = String(successRate)
-                const sendCount = await this.taskItemRepository.countBy({
-                    taskId: id,
-                    status: In(['success', 'fail'])
-                })
-                newTask.sent = sendCount
-                await this.taskRepository.save(newTask)
+            let finish = false
+            while (!finish) {
+                // 每隔5s查询一次
+                await setTimeout(5000)
+                const newTask = await this.taskRepository.findOneBy({ id })
+                if ([TaskStatus.COMPLETED, TaskStatus.PAUSE].includes(newTask.status)) {
+                    try {
+                        const successCount = await this.taskItemRepository.countBy({
+                            taskId: id,
+                            status: 'success'
+                        })
+                        const totalCount = await this.taskItemRepository.countBy({
+                            taskId: id
+                        })
+                        if (totalCount === 0) {
+                            throw new Error('No tasks found for the given taskId.')
+                        }
+                        // 计算成功率
+                        const successRate = ((successCount / totalCount) * 100).toFixed(1) + '%'
+                        newTask.successRate = String(successRate)
+                        const sendCount = await this.taskItemRepository.countBy({
+                            taskId: id,
+                            status: In(['success', 'fail'])
+                        })
+                        newTask.sent = sendCount
+                        await this.taskRepository.save(newTask)
 
 
-                // 获取用户信息
-                const user = await this.userRepository.findOneBy({
-                    id: task.userId
-                })
-                // 已发送
-                const send: number = user.send
-                user.send = send + successCount
-                // 费用 = 费率*成功发送
-                const rate: number = user.rate
-                const cost: number = rate * successCount
-                // 从用户余额中减去费用 可能有余额不足的情况,后续优化
-                const latestBalance: number = (user.balance || 0) - cost
-                user.balance = latestBalance
+                        // 获取用户信息
+                        const user = await this.userRepository.findOneBy({
+                            id: task.userId
+                        })
+                        // 已发送
+                        const send: number = user.send
+                        user.send = send + successCount
+                        // 费用 = 费率*成功发送
+                        const rate: number = user.rate
+                        const cost: number = rate * successCount
+                        // 从用户余额中减去费用 可能有余额不足的情况,后续优化
+                        const latestBalance: number = (user.balance || 0) - cost
+                        user.balance = latestBalance
 
 
-                // 余额表更新
-                const balance = await this.balanceRepository.findOne({
-                    where: {
-                        userId: task.userId
+                        // 余额表更新
+                        const balance = await this.balanceRepository.findOne({
+                            where: {
+                                userId: task.userId
+                            }
+                        })
+                        balance.currentBalance = latestBalance
+                        finish = true
+                        await this.balanceRepository.save(balance)
+                        await this.userRepository.save(user)
+                        break
+                    } catch (e) {
+                        Logger.error('Error startTask ', e, 'RcsService')
                     }
                     }
-                })
-                balance.currentBalance = latestBalance
-                await this.balanceRepository.save(balance)
-                await this.userRepository.save(user)
-            } catch (e) {
-                Logger.error('Error startTask ', e, 'RcsService')
+                }
             }
             }
         }
         }
     }
     }