Преглед на файлове

refactor(device): 优化 APK 更新逻辑,减少请求频率并改进设备处理方式

xiongzhu преди 1 година
родител
ревизия
b24c4b7f77
променени са 1 файла, в които са добавени 32 реда и са изтрити 25 реда
  1. 32 25
      src/device/device.service.ts

+ 32 - 25
src/device/device.service.ts

@@ -377,7 +377,7 @@ export class DeviceService implements OnModuleInit {
         )
     }
 
-    async findRequestingDeviceCount(){
+    async findRequestingDeviceCount() {
         return await this.deviceRepository.countBy({
             online: true,
             canSend: true,
@@ -385,7 +385,7 @@ export class DeviceService implements OnModuleInit {
         })
     }
 
-    @Interval(2 * 60 * 1000)
+    @Interval(20 * 1000)
     async scheduleApkUpdate() {
         await this.lock.acquire(
             'scheduleApkUpdate',
@@ -394,30 +394,37 @@ export class DeviceService implements OnModuleInit {
                 if (!apkUrl) return
                 const apkVersion = await this.sysConfigService.getNumber('modifier_apk_version', 0)
                 if (!apkVersion) return
-                const devices = await this.deviceRepository.findBy({
-                    online: true,
-                    busy: false
-                })
-                for (const device of devices) {
-                    if (!device.version || Number(device.version) < apkVersion) {
-                        try {
-                            this.eventsGateway.send(
-                                {
-                                    id: randomUUID(),
-                                    action: 'installApk',
-                                    data: {
-                                        apkUrl: apkUrl
-                                    }
-                                },
-                                device.socketId
-                            )
-                        } catch (e) {
-                            Logger.error('Error updating apk', 'device')
-                        }
-                    }
-                    await setTimeout(1000)
+                const devices = (
+                    await this.deviceRepository.findBy({
+                        online: true,
+                        busy: false
+                    })
+                ).filter((device) => !device.version || Number(device.version) < apkVersion)
+                for (let i = 0; i < Math.ceil(devices.length / 20); i++) {
+                    await Promise.all(
+                        devices
+                            .splice(i * 20, 20)
+                            .sort(() => Math.random() - 0.5)
+                            .map(async (d) => {
+                                try {
+                                    Logger.log(`Update apk for ${d.name}`, 'device')
+                                    this.eventsGateway.send(
+                                        {
+                                            id: randomUUID(),
+                                            action: 'installApk',
+                                            data: {
+                                                apkUrl: apkUrl
+                                            }
+                                        },
+                                        d.socketId
+                                    )
+                                } catch (e) {
+                                    Logger.error('Error updating apk', 'device')
+                                }
+                            })
+                    )
+                    await setTimeout(10000)
                 }
-                await setTimeout(30000)
             },
             { timeout: 1 }
         )