/*
 * @Author: YKH
 * @Date: 2021-12-08 10:26:27
 * @LastEditTime: 2022-03-22 23:05:10
 * @Description: 
 * @FilePath: \cool-admin\cool-admin-midway-master\src\app\modules\task\queue\task.ts
 */
import { App, Inject, Provide } from '@midwayjs/decorator';
import { IMidwayWebApplication } from '@midwayjs/web';
import { ICoolQueue, Queue } from 'midwayjs-cool-queue';
import { TaskInfoService } from '../service/info';

/**
 * 任务
 */
@Queue()
@Provide()
export abstract class TaskInfoQueue implements ICoolQueue {
  @App()
  app: IMidwayWebApplication;

  @Inject()
  taskInfoService: TaskInfoService;

  async data(job: any, done: any): Promise<void> {
    try {
      // console.log('收到的数据', job.data);
      const result = await this.taskInfoService.invokeService(job.data.service);
      this.taskInfoService.record(job.data, 1, JSON.stringify(result));
    } catch (error) {
      this.taskInfoService.record(job.data, 0, error);
    }
    this.taskInfoService.updateNextRunTime(job.data.id);
    this.taskInfoService.updateStatus();
    done();
  }
}
