|
|
@@ -1,4 +1,4 @@
|
|
|
-import { Inject, Injectable, OnModuleInit, forwardRef } from '@nestjs/common'
|
|
|
+import { Inject, Injectable, Logger, OnModuleInit, forwardRef } from '@nestjs/common'
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
import { PhoneList } from './entities/phone-list.entity'
|
|
|
import { Repository } from 'typeorm'
|
|
|
@@ -12,6 +12,8 @@ import axios from 'axios'
|
|
|
import { USACodeApiService } from './usacode-api-service'
|
|
|
import { Device } from './entities/device.entity'
|
|
|
import { NumberRequest } from './entities/number-request.entity'
|
|
|
+import { randomUUID } from 'crypto'
|
|
|
+import { setTimeout } from 'timers/promises'
|
|
|
|
|
|
@Injectable()
|
|
|
export class RcsService implements OnModuleInit {
|
|
|
@@ -36,6 +38,7 @@ export class RcsService implements OnModuleInit {
|
|
|
|
|
|
onModuleInit() {
|
|
|
this.deviceRepository.update({}, { online: false })
|
|
|
+ this.taskRepository.update({ status: TaskStatus.PENDING }, { status: TaskStatus.IDLE })
|
|
|
}
|
|
|
|
|
|
async findAllPhoneList(req: PageRequest<PhoneList>): Promise<Pagination<PhoneList>> {
|
|
|
@@ -111,10 +114,39 @@ export class RcsService implements OnModuleInit {
|
|
|
}
|
|
|
|
|
|
async runTask(task: Task) {
|
|
|
- const taskItems = await this.taskItemRepository.findBy({ taskId: task.id, status: TaskStatus.IDLE })
|
|
|
- for (const taskItem of taskItems) {
|
|
|
- taskItem.status = TaskStatus.PENDING
|
|
|
- await this.taskItemRepository.save(taskItem)
|
|
|
+ try {
|
|
|
+ let taskItems = await this.taskItemRepository.find({
|
|
|
+ where: { taskId: task.id, status: TaskStatus.IDLE },
|
|
|
+ take: 10
|
|
|
+ })
|
|
|
+ while (taskItems && taskItems.length > 0) {
|
|
|
+ let device = null
|
|
|
+ while (device === null) {
|
|
|
+ device = this.deviceRepository.findOne({
|
|
|
+ where: { online: true, canSend: true }
|
|
|
+ })
|
|
|
+ if (device === null) {
|
|
|
+ await setTimeout(2000)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ await this.eventsGateway.sendForResult(
|
|
|
+ {
|
|
|
+ id: randomUUID(),
|
|
|
+ action: 'task',
|
|
|
+ data: taskItems
|
|
|
+ },
|
|
|
+ device.socketId
|
|
|
+ )
|
|
|
+ taskItems = await this.taskItemRepository.find({
|
|
|
+ where: { taskId: task.id, status: TaskStatus.IDLE },
|
|
|
+ take: 10
|
|
|
+ })
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ Logger.error('Error running task', e, 'RcsService')
|
|
|
+ task.status = TaskStatus.ERROR
|
|
|
+ task.error = e.message
|
|
|
+ await this.taskRepository.save(task)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -122,12 +154,13 @@ export class RcsService implements OnModuleInit {
|
|
|
return await paginate<Device>(this.deviceRepository, req.page, req.search)
|
|
|
}
|
|
|
|
|
|
- async deviceConnect(id: string, model: string, name?: string, canSend: boolean = false) {
|
|
|
+ async deviceConnect(id: string, socketId: string, model: string, name?: string, canSend: boolean = false) {
|
|
|
let device = await this.deviceRepository.findOneBy({ id })
|
|
|
if (!device) {
|
|
|
device = new Device()
|
|
|
}
|
|
|
device.id = id
|
|
|
+ device.socketId = socketId
|
|
|
device.model = model
|
|
|
device.name = name
|
|
|
device.online = true
|