|
@@ -19,72 +19,48 @@ import { BalanceService } from '../balance/balance.service'
|
|
|
import Decimal from 'decimal.js'
|
|
import Decimal from 'decimal.js'
|
|
|
import { Role } from '../model/role.enum'
|
|
import { Role } from '../model/role.enum'
|
|
|
import { Phone } from '../phone-list/entities/phone.entity'
|
|
import { Phone } from '../phone-list/entities/phone.entity'
|
|
|
-import { ta } from 'date-fns/locale'
|
|
|
|
|
-
|
|
|
|
|
|
|
+import { Interval } from '@nestjs/schedule'
|
|
|
|
|
+import * as AsyncLock from 'async-lock'
|
|
|
|
|
+import { UsersService } from 'src/users/users.service'
|
|
|
@Injectable()
|
|
@Injectable()
|
|
|
export class TaskService implements OnModuleInit {
|
|
export class TaskService implements OnModuleInit {
|
|
|
|
|
+ private lock = new AsyncLock()
|
|
|
|
|
+ private TAG = 'TaskService'
|
|
|
constructor(
|
|
constructor(
|
|
|
@InjectRepository(Task)
|
|
@InjectRepository(Task)
|
|
|
private taskRepository: Repository<Task>,
|
|
private taskRepository: Repository<Task>,
|
|
|
@InjectRepository(TaskItem)
|
|
@InjectRepository(TaskItem)
|
|
|
private taskItemRepository: Repository<TaskItem>,
|
|
private taskItemRepository: Repository<TaskItem>,
|
|
|
- @InjectRepository(Users)
|
|
|
|
|
- private userRepository: Repository<Users>,
|
|
|
|
|
@InjectRepository(Phone)
|
|
@InjectRepository(Phone)
|
|
|
private phoneRepository: Repository<Phone>,
|
|
private phoneRepository: Repository<Phone>,
|
|
|
- @InjectRepository(Balance)
|
|
|
|
|
- private balanceRepository: Repository<Balance>,
|
|
|
|
|
@Inject(forwardRef(() => EventsGateway))
|
|
@Inject(forwardRef(() => EventsGateway))
|
|
|
private readonly eventsGateway: EventsGateway,
|
|
private readonly eventsGateway: EventsGateway,
|
|
|
private readonly phoneListService: PhoneListService,
|
|
private readonly phoneListService: PhoneListService,
|
|
|
private readonly deviceService: DeviceService,
|
|
private readonly deviceService: DeviceService,
|
|
|
private readonly sysConfigService: SysConfigService,
|
|
private readonly sysConfigService: SysConfigService,
|
|
|
- private readonly balanceService: BalanceService
|
|
|
|
|
|
|
+ private readonly balanceService: BalanceService,
|
|
|
|
|
+ private readonly userService: UsersService
|
|
|
) {
|
|
) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
onModuleInit() {
|
|
onModuleInit() {
|
|
|
- this.taskRepository.update({ status: TaskStatus.PENDING }, { status: TaskStatus.IDLE })
|
|
|
|
|
- this.taskItemRepository.update({ status: TaskStatus.PENDING }, { status: TaskItemStatus.IDLE })
|
|
|
|
|
|
|
+ this.lock.acquire('dispatchTask', async () => {
|
|
|
|
|
+ await this.taskItemRepository.update(
|
|
|
|
|
+ {
|
|
|
|
|
+ status: TaskItemStatus.PENDING
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ status: TaskItemStatus.IDLE
|
|
|
|
|
+ }
|
|
|
|
|
+ )
|
|
|
|
|
+ await setTimeout(10000)
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private taskControllers: { [key: number]: AbortController } = {}
|
|
private taskControllers: { [key: number]: AbortController } = {}
|
|
|
|
|
|
|
|
async findAllTask(req: PageRequest<Task>): Promise<Pagination<Task>> {
|
|
async findAllTask(req: PageRequest<Task>): Promise<Pagination<Task>> {
|
|
|
- const result = await paginate<Task>(this.taskRepository, req.page, req.search)
|
|
|
|
|
-
|
|
|
|
|
- const taskItems = result.items
|
|
|
|
|
- if (taskItems.length > 0) {
|
|
|
|
|
- for (const task of taskItems) {
|
|
|
|
|
- if (
|
|
|
|
|
- task.status === TaskStatus.PENDING ||
|
|
|
|
|
- (task.status === TaskStatus.COMPLETED &&
|
|
|
|
|
- moment(task.createdAt).isSameOrAfter(moment().subtract(2, 'hours')))
|
|
|
|
|
- ) {
|
|
|
|
|
- const id = task.id
|
|
|
|
|
- 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) + '%'
|
|
|
|
|
- task.successRate = String(successRate)
|
|
|
|
|
- const sendCount = await this.taskItemRepository.countBy({
|
|
|
|
|
- taskId: id,
|
|
|
|
|
- status: In(['success', 'fail'])
|
|
|
|
|
- })
|
|
|
|
|
- task.sent = sendCount
|
|
|
|
|
- await this.taskRepository.save(task)
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return result
|
|
|
|
|
|
|
+ return await paginate<Task>(this.taskRepository, req.page, req.search)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async findAllTaskItem(req: PageRequest<TaskItem>): Promise<Pagination<TaskItem>> {
|
|
async findAllTaskItem(req: PageRequest<TaskItem>): Promise<Pagination<TaskItem>> {
|
|
@@ -101,32 +77,35 @@ export class TaskService implements OnModuleInit {
|
|
|
dynamicMessageList = task.dynamicMessage.split(',')
|
|
dynamicMessageList = task.dynamicMessage.split(',')
|
|
|
}
|
|
}
|
|
|
task = await this.taskRepository.save(task)
|
|
task = await this.taskRepository.save(task)
|
|
|
- await this.taskItemRepository.save(
|
|
|
|
|
- phones.map((phone) => {
|
|
|
|
|
- const taskItem = new TaskItem()
|
|
|
|
|
- taskItem.taskId = task.id
|
|
|
|
|
- taskItem.number = phone.number
|
|
|
|
|
- if (dynamicMessageList !== null && task.message.includes('[#random#]')) {
|
|
|
|
|
- taskItem.message = task.message.replace(
|
|
|
|
|
- '[#random#]',
|
|
|
|
|
- dynamicMessageList[Math.floor(Math.random() * dynamicMessageList.length)]
|
|
|
|
|
- )
|
|
|
|
|
- } else {
|
|
|
|
|
- taskItem.message = task.message
|
|
|
|
|
- }
|
|
|
|
|
- taskItem.status = TaskStatus.IDLE
|
|
|
|
|
- return taskItem
|
|
|
|
|
- })
|
|
|
|
|
|
|
+
|
|
|
|
|
+ await this.phoneRepository.manager.insert(
|
|
|
|
|
+ TaskItem,
|
|
|
|
|
+ this.taskItemRepository.create(
|
|
|
|
|
+ phones.map((phone) => {
|
|
|
|
|
+ const taskItem = new TaskItem()
|
|
|
|
|
+ taskItem.taskId = task.id
|
|
|
|
|
+ taskItem.number = phone.number
|
|
|
|
|
+ if (dynamicMessageList !== null && task.message.includes('[#random#]')) {
|
|
|
|
|
+ taskItem.message = task.message.replace(
|
|
|
|
|
+ '[#random#]',
|
|
|
|
|
+ dynamicMessageList[Math.floor(Math.random() * dynamicMessageList.length)]
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ taskItem.message = task.message
|
|
|
|
|
+ }
|
|
|
|
|
+ taskItem.status = TaskStatus.IDLE
|
|
|
|
|
+ return taskItem
|
|
|
|
|
+ })
|
|
|
|
|
+ )
|
|
|
)
|
|
)
|
|
|
|
|
+
|
|
|
return task
|
|
return task
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async balanceVerification(id: number) {
|
|
async balanceVerification(id: number) {
|
|
|
const task = await this.taskRepository.findOneBy({ id })
|
|
const task = await this.taskRepository.findOneBy({ id })
|
|
|
// 获取用户信息
|
|
// 获取用户信息
|
|
|
- const user = await this.userRepository.findOneBy({
|
|
|
|
|
- id: task.userId
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ const user = await this.userService.findById(task.userId)
|
|
|
if (user.roles.includes(Role.Admin)) {
|
|
if (user.roles.includes(Role.Admin)) {
|
|
|
return 0
|
|
return 0
|
|
|
}
|
|
}
|
|
@@ -144,7 +123,24 @@ export class TaskService implements OnModuleInit {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async startTask(id: number): Promise<void> {
|
|
async startTask(id: number): Promise<void> {
|
|
|
- const task = await this.taskRepository.findOneBy({ id })
|
|
|
|
|
|
|
+ const task = await this.taskRepository.findOneOrFail({
|
|
|
|
|
+ where: { id }
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ if (task.status !== TaskStatus.IDLE) return
|
|
|
|
|
+
|
|
|
|
|
+ if (!task.paid) {
|
|
|
|
|
+ const user = await this.userService.findById(task.userId)
|
|
|
|
|
+ if (!user.roles.includes(Role.Admin)) {
|
|
|
|
|
+ // 开始任务前扣费
|
|
|
|
|
+ const cost = await this.getCost(task, user)
|
|
|
|
|
+ if (cost > (user.balance || 0)) {
|
|
|
|
|
+ throw new Error('Insufficient balance!')
|
|
|
|
|
+ }
|
|
|
|
|
+ await this.balanceService.feeDeduction(task.userId, cost)
|
|
|
|
|
+ await this.taskRepository.update({ id }, { paid: true })
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
// 查询当前是否有任务执行
|
|
// 查询当前是否有任务执行
|
|
|
const num = await this.taskRepository.count({
|
|
const num = await this.taskRepository.count({
|
|
@@ -152,81 +148,14 @@ export class TaskService implements OnModuleInit {
|
|
|
status: TaskStatus.PENDING
|
|
status: TaskStatus.PENDING
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
- // 当前有任务
|
|
|
|
|
- if (num > 0 && task.status === TaskStatus.IDLE) {
|
|
|
|
|
- // 当前任务进入队列
|
|
|
|
|
- task.status = TaskStatus.QUEUED
|
|
|
|
|
- await this.taskRepository.save(task)
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- // 队列没有任务或队列中下一个待执行任务,启动此任务
|
|
|
|
|
- if (
|
|
|
|
|
- (task && task.status === TaskStatus.IDLE) ||
|
|
|
|
|
- task.status === TaskStatus.PAUSE ||
|
|
|
|
|
- task.status === TaskStatus.QUEUED
|
|
|
|
|
- ) {
|
|
|
|
|
- if (task.status !== TaskStatus.PAUSE) {
|
|
|
|
|
- const user = await this.userRepository.findOneBy({
|
|
|
|
|
- id: task.userId
|
|
|
|
|
- })
|
|
|
|
|
- if (!user.roles.includes(Role.Admin)) {
|
|
|
|
|
- // 开始任务前扣费
|
|
|
|
|
- const cost = await this.getCost(task, user)
|
|
|
|
|
- if (cost > (user.balance || 0)) {
|
|
|
|
|
- throw new Error('Insufficient balance!')
|
|
|
|
|
- }
|
|
|
|
|
- await this.balanceService.feeDeduction(task.userId, cost, user)
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- task.status = TaskStatus.PENDING
|
|
|
|
|
- await this.taskRepository.save(task)
|
|
|
|
|
- await this.runTask(task)
|
|
|
|
|
-
|
|
|
|
|
- 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)
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
- Logger.error('Error startTask ', e, 'RcsService')
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ await this.taskRepository.update({ id }, { status: num > 0 ? TaskStatus.QUEUED : TaskStatus.PENDING })
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async pauseTask(id: number): Promise<void> {
|
|
async pauseTask(id: number): Promise<void> {
|
|
|
const task = await this.taskRepository.findOneBy({ id })
|
|
const task = await this.taskRepository.findOneBy({ id })
|
|
|
- const successCount = await this.taskItemRepository.countBy({
|
|
|
|
|
- taskId: id,
|
|
|
|
|
- status: In(['success', 'fail'])
|
|
|
|
|
- })
|
|
|
|
|
- if (task && task.status === TaskStatus.PENDING) {
|
|
|
|
|
- task.status = TaskStatus.PAUSE
|
|
|
|
|
- task.sent = successCount
|
|
|
|
|
- await this.taskRepository.save(task)
|
|
|
|
|
- const controller = this.taskControllers[task.id]
|
|
|
|
|
- if (controller) {
|
|
|
|
|
- controller.abort()
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (task.status === TaskStatus.PENDING || task.status === TaskStatus.QUEUED) {
|
|
|
|
|
+ await this.taskRepository.update({ id }, { status: TaskStatus.IDLE })
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -311,8 +240,8 @@ export class TaskService implements OnModuleInit {
|
|
|
async getConfig(name, defValue) {
|
|
async getConfig(name, defValue) {
|
|
|
try {
|
|
try {
|
|
|
return await this.sysConfigService.getNumber(name, defValue)
|
|
return await this.sysConfigService.getNumber(name, defValue)
|
|
|
- } catch (error) {
|
|
|
|
|
- Logger.error('Error getting rcs wait time', error, 'RcsService')
|
|
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ Logger.error('Error getting rcs wait time', e.stack, this.TAG)
|
|
|
}
|
|
}
|
|
|
return defValue
|
|
return defValue
|
|
|
}
|
|
}
|
|
@@ -335,7 +264,7 @@ export class TaskService implements OnModuleInit {
|
|
|
let finish = false
|
|
let finish = false
|
|
|
while (!finish) {
|
|
while (!finish) {
|
|
|
if (controller.signal.aborted) {
|
|
if (controller.signal.aborted) {
|
|
|
- Logger.log('Task aborted', 'RcsService')
|
|
|
|
|
|
|
+ Logger.log('Task aborted', this.TAG)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -382,12 +311,12 @@ export class TaskService implements OnModuleInit {
|
|
|
let device = null
|
|
let device = null
|
|
|
while (device === null) {
|
|
while (device === null) {
|
|
|
if (controller.signal.aborted) {
|
|
if (controller.signal.aborted) {
|
|
|
- Logger.log('Task aborted', 'RcsService')
|
|
|
|
|
|
|
+ Logger.log('Task aborted', this.TAG)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
device = await this.deviceService.findAvailableDevice()
|
|
device = await this.deviceService.findAvailableDevice()
|
|
|
if (device === null) {
|
|
if (device === null) {
|
|
|
- Logger.log('No device available, waiting...', 'RcsService')
|
|
|
|
|
|
|
+ Logger.log('No device available, waiting...', this.TAG)
|
|
|
await setTimeout(2000)
|
|
await setTimeout(2000)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -396,7 +325,7 @@ export class TaskService implements OnModuleInit {
|
|
|
TaskItemStatus.PENDING
|
|
TaskItemStatus.PENDING
|
|
|
)
|
|
)
|
|
|
await this.deviceService.setBusy(device.id, true)
|
|
await this.deviceService.setBusy(device.id, true)
|
|
|
- Logger.log(`Send task to device ${device.id}(${device.model})`, 'RcsService')
|
|
|
|
|
|
|
+ Logger.log(`Send task to device ${device.id}(${device.model})`, this.TAG)
|
|
|
|
|
|
|
|
Promise.race([
|
|
Promise.race([
|
|
|
this.eventsGateway.sendForResult(
|
|
this.eventsGateway.sendForResult(
|
|
@@ -423,7 +352,7 @@ export class TaskService implements OnModuleInit {
|
|
|
])
|
|
])
|
|
|
.then(async (res: any) => {
|
|
.then(async (res: any) => {
|
|
|
if (res.error) {
|
|
if (res.error) {
|
|
|
- Logger.error('Task timeout', 'RcsService')
|
|
|
|
|
|
|
+ Logger.error('Task timeout', this.TAG)
|
|
|
await this.updateTaskItemStatus(
|
|
await this.updateTaskItemStatus(
|
|
|
taskItems.map((i) => i.id),
|
|
taskItems.map((i) => i.id),
|
|
|
TaskItemStatus.IDLE
|
|
TaskItemStatus.IDLE
|
|
@@ -433,7 +362,7 @@ export class TaskService implements OnModuleInit {
|
|
|
`Task completed: ${res.success.length} success, ${res.fail.length} fail, ${
|
|
`Task completed: ${res.success.length} success, ${res.fail.length} fail, ${
|
|
|
res.retry?.length || 0
|
|
res.retry?.length || 0
|
|
|
} retry`,
|
|
} retry`,
|
|
|
- 'RcsService'
|
|
|
|
|
|
|
+ this.TAG
|
|
|
)
|
|
)
|
|
|
if (res.success?.length > 0) {
|
|
if (res.success?.length > 0) {
|
|
|
await this.updateTaskItemStatus(res.success, TaskItemStatus.SUCCESS)
|
|
await this.updateTaskItemStatus(res.success, TaskItemStatus.SUCCESS)
|
|
@@ -447,7 +376,7 @@ export class TaskService implements OnModuleInit {
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
.catch(async (e) => {
|
|
.catch(async (e) => {
|
|
|
- Logger.error('Error running task', e.stack, 'RcsService')
|
|
|
|
|
|
|
+ Logger.error('Error running task', e.stack, this.TAG)
|
|
|
await this.updateTaskItemStatus(
|
|
await this.updateTaskItemStatus(
|
|
|
taskItems.map((i) => i.id),
|
|
taskItems.map((i) => i.id),
|
|
|
TaskItemStatus.IDLE
|
|
TaskItemStatus.IDLE
|
|
@@ -455,7 +384,7 @@ export class TaskService implements OnModuleInit {
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
- Logger.error('Error running task', e.stack, 'RcsService')
|
|
|
|
|
|
|
+ Logger.error('Error running task', e.stack, this.TAG)
|
|
|
task.status = TaskStatus.ERROR
|
|
task.status = TaskStatus.ERROR
|
|
|
task.error = e.message
|
|
task.error = e.message
|
|
|
await this.taskRepository.save(task)
|
|
await this.taskRepository.save(task)
|
|
@@ -473,4 +402,136 @@ export class TaskService implements OnModuleInit {
|
|
|
const cost = rate.mul(num)
|
|
const cost = rate.mul(num)
|
|
|
return cost.toNumber()
|
|
return cost.toNumber()
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Interval(2000)
|
|
|
|
|
+ async scheduleTask() {
|
|
|
|
|
+ this.lock
|
|
|
|
|
+ .acquire(
|
|
|
|
|
+ 'dispatchTask',
|
|
|
|
|
+ async () => {
|
|
|
|
|
+ const task = await this.taskRepository.findOneBy({
|
|
|
|
|
+ status: TaskStatus.PENDING
|
|
|
|
|
+ })
|
|
|
|
|
+ if (task) {
|
|
|
|
|
+ await this.dispatchTask(task)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ const nextTask = await this.taskRepository.findOne({
|
|
|
|
|
+ where: {
|
|
|
|
|
+ status: TaskStatus.QUEUED
|
|
|
|
|
+ },
|
|
|
|
|
+ order: {
|
|
|
|
|
+ createdAt: 'ASC'
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ if (nextTask) {
|
|
|
|
|
+ await this.taskRepository.update({ id: nextTask.id }, { status: TaskStatus.PENDING })
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ timeout: 1
|
|
|
|
|
+ }
|
|
|
|
|
+ )
|
|
|
|
|
+ .catch((e) => {
|
|
|
|
|
+ if (e.message.includes('timed out')) return
|
|
|
|
|
+ Logger.error('Error dispatchTask', e.stack, this.TAG)
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async dispatchTask(task: Task) {
|
|
|
|
|
+ const batchSize = 200
|
|
|
|
|
+ const taskItems = await this.taskItemRepository.find({
|
|
|
|
|
+ where: {
|
|
|
|
|
+ taskId: task.id,
|
|
|
|
|
+ status: TaskItemStatus.IDLE
|
|
|
|
|
+ },
|
|
|
|
|
+ take: batchSize * 5
|
|
|
|
|
+ })
|
|
|
|
|
+ if (taskItems.length === 0) {
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const devices = await this.deviceService.findAvailableDevices(Math.ceil(taskItems.length / 5), task.matchDevice)
|
|
|
|
|
+ if (devices.length === 0) {
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const taskConfig = {
|
|
|
|
|
+ rcsWait: task.rcsWait || (await this.getConfig('rcs_wait', 2000)),
|
|
|
|
|
+ rcsInterval: task.rcsInterval || (await this.getConfig('rcs_interval', 3000)),
|
|
|
|
|
+ cleanCount: task.cleanCount || (await this.getConfig('clean_count', 20)),
|
|
|
|
|
+ requestNumberInterval: task.requestNumberInterval || (await this.getConfig('request_number_interval', 100)),
|
|
|
|
|
+ checkConnection: task.checkConnection
|
|
|
|
|
+ }
|
|
|
|
|
+ Promise.all(
|
|
|
|
|
+ devices.map(async (device, i) => {
|
|
|
|
|
+ const items = taskItems.slice(i * 5, i * 5 + 5)
|
|
|
|
|
+ await this.updateTaskItemStatus(
|
|
|
|
|
+ items.map((i) => i.id),
|
|
|
|
|
+ TaskItemStatus.PENDING
|
|
|
|
|
+ )
|
|
|
|
|
+ try {
|
|
|
|
|
+ const res: any = await Promise.race([
|
|
|
|
|
+ this.eventsGateway.sendForResult(
|
|
|
|
|
+ {
|
|
|
|
|
+ id: randomUUID(),
|
|
|
|
|
+ action: 'task',
|
|
|
|
|
+ data: {
|
|
|
|
|
+ config: taskConfig,
|
|
|
|
|
+ tasks: items,
|
|
|
|
|
+ taskId: task.id
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ device.socketId
|
|
|
|
|
+ ),
|
|
|
|
|
+ setTimeout(60000).then(() => {
|
|
|
|
|
+ return Promise.reject(new Error('timeout waiting for response'))
|
|
|
|
|
+ })
|
|
|
|
|
+ ])
|
|
|
|
|
+ Logger.log(
|
|
|
|
|
+ `Task completed: ${res.success.length} success, ${res.fail.length} fail, ${
|
|
|
|
|
+ res.retry?.length || 0
|
|
|
|
|
+ } retry`,
|
|
|
|
|
+ this.TAG
|
|
|
|
|
+ )
|
|
|
|
|
+ if (res.success?.length > 0) {
|
|
|
|
|
+ await this.updateTaskItemStatus(res.success, TaskItemStatus.SUCCESS)
|
|
|
|
|
+ }
|
|
|
|
|
+ if (res.fail?.length > 0) {
|
|
|
|
|
+ await this.updateTaskItemStatus(res.fail, TaskItemStatus.FAIL)
|
|
|
|
|
+ }
|
|
|
|
|
+ if (res.retry?.length > 0) {
|
|
|
|
|
+ await this.updateTaskItemStatus(res.retry, TaskItemStatus.IDLE)
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ Logger.error('Error running task', e.stack, this.TAG)
|
|
|
|
|
+ await this.updateTaskItemStatus(
|
|
|
|
|
+ items.map((i) => i.id),
|
|
|
|
|
+ TaskItemStatus.IDLE
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ ).then(async () => {
|
|
|
|
|
+ const counts = await this.taskItemRepository.manager.query(
|
|
|
|
|
+ `select status, count(*) as count from task_item where taskId = ${task.id} group by status`
|
|
|
|
|
+ )
|
|
|
|
|
+ Logger.log('Task counts', JSON.stringify(counts), this.TAG)
|
|
|
|
|
+ const successCount = parseInt(counts.find((c) => c.status === 'success')?.count) || 0
|
|
|
|
|
+ const failCount = parseInt(counts.find((c) => c.status === 'fail')?.count) || 0
|
|
|
|
|
+ const pendingCount = parseInt(counts.find((c) => c.status === 'pending')?.count) || 0
|
|
|
|
|
+ const idleCount = parseInt(counts.find((c) => c.status === 'idle')?.count) || 0
|
|
|
|
|
+ const finish = pendingCount === 0 && idleCount === 0
|
|
|
|
|
+ const data: Partial<Task> = {
|
|
|
|
|
+ sent: successCount + failCount,
|
|
|
|
|
+ successRate:
|
|
|
|
|
+ successCount + failCount > 0
|
|
|
|
|
+ ? ((successCount / (successCount + failCount)) * 100).toFixed(1) + '%'
|
|
|
|
|
+ : '0%'
|
|
|
|
|
+ }
|
|
|
|
|
+ if (finish) {
|
|
|
|
|
+ data.status = TaskStatus.COMPLETED
|
|
|
|
|
+ }
|
|
|
|
|
+ await this.taskRepository.update({ id: task.id }, data)
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|