wangzhibo
2026-07-16 bac4362a0b7726d38a23d38f0d7913f2c2bab262
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { App, Init, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { TaskInfoEntity } from '../entity/info';
import * as _ from 'lodash';
import { IMidwayApplication } from '@midwayjs/core';
import { CoolQueueHandle } from '@cool-midway/task';
import { TaskBullService } from './bull';
import { TaskLocalService } from './local';
import { TaskLogEntity } from '../entity/log';
/**
 * 任务
 */
@Provide()
@Scope(ScopeEnum.Request, { allowDowngrade: true })
export class TaskInfoService extends BaseService {
  @InjectEntityModel(TaskInfoEntity)
  taskInfoEntity: Repository<TaskInfoEntity>;
 
  @InjectEntityModel(TaskLogEntity)
  taskLogEntity: Repository<TaskLogEntity>;
 
  type: 'local' | 'bull' = 'local';
 
  @App()
  app: IMidwayApplication;
 
  @Inject()
  taskBullService: TaskBullService;
 
  @Inject()
  taskLocalService: TaskLocalService;
 
  @Init()
  async init() {
    await super.init();
    await this.initType();
    this.setEntity(this.taskInfoEntity);
  }
 
  /**
   * 初始化任务类型
   */
  async initType() {
    try {
      const check = await this.app
        .getApplicationContext()
        .getAsync(CoolQueueHandle);
      if (check) {
        this.type = 'bull';
      } else {
        this.type = 'local';
      }
    } catch (e) {
      this.type = 'local';
    }
    return this.type;
  }
 
  /**
   * 停止任务
   * @param id
   */
  async stop(id) {
    this.type === 'bull'
      ? await this.taskBullService.stop(id)
      : await this.taskLocalService.stop(id);
  }
 
  /**
   * 开始任务
   * @param id
   * @param type
   */
  async start(id, type?) {
    this.type === 'bull'
      ? await this.taskBullService.start(id)
      : await this.taskLocalService.start(id, type);
  }
  /**
   * 手动执行一次
   * @param id
   */
  async once(id) {
    this.type === 'bull'
      ? this.taskBullService.once(id)
      : this.taskLocalService.once(id);
  }
  /**
   * 检查任务是否存在
   * @param jobId
   */
  async exist(jobId) {
    this.type === 'bull'
      ? this.taskBullService.exist(jobId)
      : this.taskLocalService.exist(jobId);
  }
  /**
   * 新增或修改
   * @param params
   */
  async addOrUpdate(params) {
    this.type === 'bull'
      ? this.taskBullService.addOrUpdate(params)
      : this.taskLocalService.addOrUpdate(params);
  }
  /**
   * 删除
   * @param ids
   */
  async delete(ids) {
    this.type === 'bull'
      ? this.taskBullService.delete(ids)
      : this.taskLocalService.delete(ids);
  }
  /**
   * 任务日志
   * @param query
   */
  async log(query) {
    const { id, status } = query;
    const find = await this.taskLogEntity
      .createQueryBuilder('a')
      .select(['a.*', 'b.name as taskName'])
      .leftJoin(TaskInfoEntity, 'b', 'a.taskId = b.id')
      .where('a.taskId = :id', { id });
    if (status || status == 0) {
      find.andWhere('a.status = :status', { status });
    }
    return await this.entityRenderPage(find, query);
  }
 
  /**
   * 初始化任务
   */
  async initTask() {
    this.type === 'bull'
      ? this.taskBullService.initTask()
      : this.taskLocalService.initTask();
  }
 
  /**
   * 详情
   * @param id
   * @returns
   */
  async info(id: any): Promise<any> {
    this.type === 'bull'
      ? this.taskBullService.info(id)
      : this.taskLocalService.info(id);
  }
}