task.service.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. import { PhoneListService } from './../phone-list/phone-list.service'
  2. import { forwardRef, Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common'
  3. import { InjectRepository } from '@nestjs/typeorm'
  4. import { Task, TaskStatus } from './entities/task.entity'
  5. import { In, Repository } from 'typeorm'
  6. import { TaskItem, TaskItemStatus } from './entities/task-item.entity'
  7. import { PageRequest } from '../common/dto/page-request'
  8. import { paginate, Pagination } from 'nestjs-typeorm-paginate'
  9. import { EventsGateway } from '../events/events.gateway'
  10. import { randomUUID } from 'crypto'
  11. import { setTimeout } from 'timers/promises'
  12. import { DeviceService } from '../device/device.service'
  13. import { SysConfigService } from '../sys-config/sys-config.service'
  14. import { Users } from '../users/entities/users.entity'
  15. import { Balance } from '../balance/entities/balance.entities'
  16. import * as ExcelJS from 'exceljs'
  17. import * as moment from 'moment'
  18. import { BalanceService } from '../balance/balance.service'
  19. import Decimal from 'decimal.js'
  20. import { Role } from '../model/role.enum'
  21. import { Phone } from '../phone-list/entities/phone.entity'
  22. import { ta } from 'date-fns/locale'
  23. @Injectable()
  24. export class TaskService implements OnModuleInit {
  25. constructor(
  26. @InjectRepository(Task)
  27. private taskRepository: Repository<Task>,
  28. @InjectRepository(TaskItem)
  29. private taskItemRepository: Repository<TaskItem>,
  30. @InjectRepository(Users)
  31. private userRepository: Repository<Users>,
  32. @InjectRepository(Phone)
  33. private phoneRepository: Repository<Phone>,
  34. @InjectRepository(Balance)
  35. private balanceRepository: Repository<Balance>,
  36. @Inject(forwardRef(() => EventsGateway))
  37. private readonly eventsGateway: EventsGateway,
  38. private readonly phoneListService: PhoneListService,
  39. private readonly deviceService: DeviceService,
  40. private readonly sysConfigService: SysConfigService,
  41. private readonly balanceService: BalanceService
  42. ) {}
  43. onModuleInit() {
  44. this.taskRepository.update({ status: TaskStatus.PENDING }, { status: TaskStatus.IDLE })
  45. }
  46. private taskControllers: { [key: number]: AbortController } = {}
  47. async findAllTask(req: PageRequest<Task>): Promise<Pagination<Task>> {
  48. const result = await paginate<Task>(this.taskRepository, req.page, req.search)
  49. const taskItems = result.items
  50. if (taskItems.length > 0) {
  51. for (const task of taskItems) {
  52. if (
  53. task.status === TaskStatus.PENDING ||
  54. (task.status === TaskStatus.COMPLETED &&
  55. moment(task.createdAt).isSameOrAfter(moment().subtract(12, 'hours')))
  56. ) {
  57. const id = task.id
  58. const successCount = await this.taskItemRepository.countBy({
  59. taskId: id,
  60. status: 'success'
  61. })
  62. const totalCount = await this.taskItemRepository.countBy({
  63. taskId: id
  64. })
  65. if (totalCount === 0) {
  66. throw new Error('No tasks found for the given taskId.')
  67. }
  68. // 计算成功率
  69. const successRate = ((successCount / totalCount) * 100).toFixed(1) + '%'
  70. task.successRate = String(successRate)
  71. const sendCount = await this.taskItemRepository.countBy({
  72. taskId: id,
  73. status: In(['success', 'fail'])
  74. })
  75. task.sent = sendCount
  76. await this.taskRepository.save(task)
  77. }
  78. }
  79. }
  80. return result
  81. }
  82. async findAllTaskItem(req: PageRequest<TaskItem>): Promise<Pagination<TaskItem>> {
  83. return await paginate<TaskItem>(this.taskItemRepository, req.page, req.search)
  84. }
  85. async createTask(task: Task): Promise<Task> {
  86. let dynamicMessageList = null
  87. if (task.dynamicMessage && task.dynamicMessage !== '') {
  88. dynamicMessageList = task.dynamicMessage.split(',')
  89. }
  90. task = await this.taskRepository.save(task)
  91. const phones = await this.phoneListService.findPhoneByListId(task.listId)
  92. await this.taskItemRepository.save(
  93. phones.map((phone) => {
  94. const taskItem = new TaskItem()
  95. taskItem.taskId = task.id
  96. taskItem.number = phone.number
  97. if (dynamicMessageList !== null && task.message.includes('[#random#]')) {
  98. taskItem.message = task.message.replace(
  99. '[#random#]',
  100. dynamicMessageList[Math.floor(Math.random() * dynamicMessageList.length)]
  101. )
  102. } else {
  103. taskItem.message = task.message
  104. }
  105. taskItem.status = TaskStatus.IDLE
  106. return taskItem
  107. })
  108. )
  109. return task
  110. }
  111. async balanceVerification(id: number) {
  112. const task = await this.taskRepository.findOneBy({ id })
  113. // 获取用户信息
  114. const user = await this.userRepository.findOneBy({
  115. id: task.userId
  116. })
  117. if (user.roles.includes(Role.Admin)) {
  118. return 0
  119. }
  120. const cost: number = await this.getCost(task, user)
  121. // 验证余额
  122. if (cost > (user.balance || 0)) {
  123. return -1
  124. } else {
  125. return cost
  126. }
  127. }
  128. async delTask(id: number): Promise<void> {
  129. await this.taskRepository.delete(id)
  130. }
  131. async startTask(id: number): Promise<void> {
  132. const task = await this.taskRepository.findOneBy({ id })
  133. // 查询当前是否有任务执行
  134. const num = await this.taskRepository.count({
  135. where: {
  136. status: TaskStatus.PENDING
  137. }
  138. })
  139. // 当前有任务
  140. if (num > 0 && task.status === TaskStatus.IDLE) {
  141. // 当前任务进入队列
  142. task.status = TaskStatus.QUEUED
  143. await this.taskRepository.save(task)
  144. return
  145. }
  146. // 队列没有任务或队列中下一个待执行任务,启动此任务
  147. if (
  148. (task && task.status === TaskStatus.IDLE) ||
  149. task.status === TaskStatus.PAUSE ||
  150. task.status === TaskStatus.QUEUED
  151. ) {
  152. if (task.status !== TaskStatus.PAUSE) {
  153. const user = await this.userRepository.findOneBy({
  154. id: task.userId
  155. })
  156. if (!user.roles.includes(Role.Admin)) {
  157. // 开始任务前扣费
  158. const cost = await this.getCost(task, user)
  159. if (cost > (user.balance || 0)) {
  160. throw new Error('Insufficient balance!')
  161. }
  162. await this.balanceService.feeDeduction(task.userId, cost, user)
  163. }
  164. }
  165. task.status = TaskStatus.PENDING
  166. await this.taskRepository.save(task)
  167. await this.runTask(task)
  168. const newTask = await this.taskRepository.findOneBy({ id })
  169. if ([TaskStatus.COMPLETED, TaskStatus.PAUSE].includes(newTask.status)) {
  170. try {
  171. const successCount = await this.taskItemRepository.countBy({
  172. taskId: id,
  173. status: 'success'
  174. })
  175. const totalCount = await this.taskItemRepository.countBy({
  176. taskId: id
  177. })
  178. if (totalCount === 0) {
  179. throw new Error('No tasks found for the given taskId.')
  180. }
  181. // 计算成功率
  182. const successRate = ((successCount / totalCount) * 100).toFixed(1) + '%'
  183. newTask.successRate = String(successRate)
  184. const sendCount = await this.taskItemRepository.countBy({
  185. taskId: id,
  186. status: In(['success', 'fail'])
  187. })
  188. newTask.sent = sendCount
  189. await this.taskRepository.save(newTask)
  190. } catch (e) {
  191. Logger.error('Error startTask ', e, 'RcsService')
  192. }
  193. }
  194. }
  195. }
  196. async pauseTask(id: number): Promise<void> {
  197. const task = await this.taskRepository.findOneBy({ id })
  198. const successCount = await this.taskItemRepository.countBy({
  199. taskId: id,
  200. status: In(['success', 'fail'])
  201. })
  202. if (task && task.status === TaskStatus.PENDING) {
  203. task.status = TaskStatus.PAUSE
  204. task.sent = successCount
  205. await this.taskRepository.save(task)
  206. const controller = this.taskControllers[task.id]
  207. if (controller) {
  208. controller.abort()
  209. }
  210. }
  211. }
  212. async exportTaskItem(taskId: number) {
  213. const workbook = new ExcelJS.Workbook()
  214. const worksheet = workbook.addWorksheet('Sheet1')
  215. const taskItems = await this.taskItemRepository.find({
  216. where: {
  217. taskId: taskId
  218. }
  219. })
  220. // 设置列头
  221. worksheet.columns = [
  222. { header: '手机号', key: 'number', width: 30, style: { alignment: { horizontal: 'center' } } },
  223. { header: '是否有效', key: 'isValid', width: 15, style: { alignment: { horizontal: 'center' } } },
  224. { header: '发送成功', key: 'status', width: 15, style: { alignment: { horizontal: 'center' } } },
  225. {
  226. header: '发送时间',
  227. key: 'sendAt',
  228. width: 30,
  229. style: { alignment: { horizontal: 'center' }, numFmt: 'YYYY-MM-DD HH:mm:ss' }
  230. }
  231. ]
  232. taskItems.forEach((item) => {
  233. let valid = '有效'
  234. let status = '发送成功'
  235. const sendAt: Date = item.sendAt
  236. const formattedSendAt = moment(sendAt).format('YYYY-MM-DD HH:mm:ss')
  237. if (item.status === TaskItemStatus.FAIL) {
  238. valid = '无效'
  239. status = ''
  240. }
  241. worksheet.addRow({
  242. number: item.number,
  243. isValid: valid,
  244. status: status,
  245. sendAt: formattedSendAt
  246. })
  247. })
  248. return await workbook.xlsx.writeBuffer()
  249. }
  250. async getToBeSentNum() {
  251. let total = 0
  252. // 任务队列
  253. const tasks = await this.taskRepository.find({
  254. where: {
  255. status: TaskStatus.QUEUED
  256. },
  257. order: {
  258. createdAt: 'ASC'
  259. }
  260. })
  261. if (tasks.length > 0) {
  262. const list = tasks.map((task) => task.listId)
  263. // 队列中剩余任务发送数
  264. const number = await this.phoneRepository.countBy({
  265. listId: In(list)
  266. })
  267. total += number
  268. }
  269. // 正在执行的任务
  270. const current = await this.taskRepository.find({
  271. where: {
  272. status: TaskStatus.PENDING
  273. }
  274. })
  275. if (current.length > 0) {
  276. const number = await this.phoneRepository.countBy({
  277. listId: current[0].listId
  278. })
  279. total += number
  280. }
  281. return total
  282. }
  283. async getConfig(name, defValue) {
  284. try {
  285. return await this.sysConfigService.getNumber(name, defValue)
  286. } catch (error) {
  287. Logger.error('Error getting rcs wait time', error, 'RcsService')
  288. }
  289. return defValue
  290. }
  291. async updateTaskItemStatus(taskIds: number[], status: TaskItemStatus) {
  292. await this.taskItemRepository.update(
  293. {
  294. id: In(taskIds)
  295. },
  296. {
  297. status: status
  298. }
  299. )
  300. }
  301. async runTask(task: Task) {
  302. let controller = new AbortController()
  303. this.taskControllers[task.id] = controller
  304. try {
  305. let finish = false
  306. while (!finish) {
  307. if (controller.signal.aborted) {
  308. Logger.log('Task aborted', 'RcsService')
  309. return
  310. }
  311. task = await this.taskRepository.findOneBy({ id: task.id })
  312. let rcsWait = task.rcsWait || (await this.getConfig('rcs_wait', 2000))
  313. let rcsInterval = task.rcsInterval || (await this.getConfig('rcs_interval', 3000))
  314. let cleanCount = task.cleanCount || (await this.getConfig('clean_count', 20))
  315. let requestNumberInterval =
  316. task.requestNumberInterval || (await this.getConfig('request_number_interval', 100))
  317. let taskItems = await this.taskItemRepository.find({
  318. where: { taskId: task.id, status: TaskItemStatus.IDLE },
  319. take: 5
  320. })
  321. if (taskItems.length === 0) {
  322. finish = true
  323. task.status = TaskStatus.COMPLETED
  324. await this.taskRepository.save(task)
  325. // 从队列中获取下一个任务
  326. const tasks = await this.taskRepository.find({
  327. where: {
  328. status: TaskStatus.QUEUED
  329. },
  330. order: {
  331. createdAt: 'ASC'
  332. }
  333. })
  334. // 异步执行startTask方法
  335. if (tasks.length > 0) {
  336. this.startTask(tasks[0].id)
  337. }
  338. break
  339. }
  340. let device = null
  341. while (device === null) {
  342. if (controller.signal.aborted) {
  343. Logger.log('Task aborted', 'RcsService')
  344. return
  345. }
  346. device = await this.deviceService.findAvailableDevice()
  347. if (device === null) {
  348. Logger.log('No device available, waiting...', 'RcsService')
  349. await setTimeout(2000)
  350. }
  351. }
  352. await this.updateTaskItemStatus(
  353. taskItems.map((i) => i.id),
  354. TaskItemStatus.PENDING
  355. )
  356. await this.deviceService.setBusy(device.id, true)
  357. Logger.log(`Send task to device ${device.id}(${device.model})`, 'RcsService')
  358. Promise.race([
  359. this.eventsGateway.sendForResult(
  360. {
  361. id: randomUUID(),
  362. action: 'task',
  363. data: {
  364. config: {
  365. rcsWait,
  366. rcsInterval,
  367. cleanCount,
  368. requestNumberInterval,
  369. checkConnection: task.checkConnection
  370. },
  371. tasks: taskItems,
  372. taskId: task.id
  373. }
  374. },
  375. device.socketId
  376. ),
  377. setTimeout(60000).then(() => {
  378. return Promise.resolve({ error: 'Timeout' })
  379. })
  380. ])
  381. .then(async (res: any) => {
  382. if (res.error) {
  383. Logger.error('Task timeout', 'RcsService')
  384. await this.updateTaskItemStatus(
  385. taskItems.map((i) => i.id),
  386. TaskItemStatus.PENDING
  387. )
  388. } else {
  389. Logger.log(
  390. `Task completed: ${res.success.length} success, ${res.fail.length} fail, ${
  391. res.retry?.length || 0
  392. } retry`,
  393. 'RcsService'
  394. )
  395. if (res.success?.length > 0) {
  396. await this.updateTaskItemStatus(res.success, TaskItemStatus.SUCCESS)
  397. }
  398. if (res.fail?.length > 0) {
  399. await this.updateTaskItemStatus(res.fail, TaskItemStatus.FAIL)
  400. }
  401. if (res.retry?.length > 0) {
  402. await this.updateTaskItemStatus(res.retry, TaskItemStatus.PENDING)
  403. }
  404. }
  405. })
  406. .catch(async (e) => {
  407. Logger.error('Error running task', e.stack, 'RcsService')
  408. await this.updateTaskItemStatus(
  409. taskItems.map((i) => i.id),
  410. TaskItemStatus.PENDING
  411. )
  412. })
  413. }
  414. } catch (e) {
  415. Logger.error('Error running task', e.stack, 'RcsService')
  416. task.status = TaskStatus.ERROR
  417. task.error = e.message
  418. await this.taskRepository.save(task)
  419. }
  420. this.taskControllers[task.id] = null
  421. return task
  422. }
  423. async getCost(task: Task, user: Users) {
  424. const number: number = await this.phoneListService.findCountByListId(task.listId)
  425. // 费用 = 费率*需要发送量
  426. const rate = new Decimal(String(user.rate))
  427. const num = new Decimal(String(number))
  428. const cost = rate.mul(num)
  429. return cost.toNumber()
  430. }
  431. }