wangrong
2022-07-08 7e9badac4f3df3650e72bd419f9cdaa485b054d6
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import { App, Inject, Logger, Provide } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import { TaskInfoEntity } from '../entity/info';
import { TaskLogEntity } from '../entity/log';
import { ILogger } from '@midwayjs/logger';
import { IMidwayWebApplication } from '@midwayjs/web';
import * as _ from 'lodash';
import { Utils } from '../../../comm/utils';
import { TaskInfoQueue } from '../queue/task';
 
/**
 * 任务
 */
@Provide()
export class TaskInfoService extends BaseService {
  @InjectEntityModel(TaskInfoEntity)
  taskInfoEntity: Repository<TaskInfoEntity>;
 
  @Logger()
  logger: ILogger;
 
  @InjectEntityModel(TaskLogEntity)
  taskLogEntity: Repository<TaskLogEntity>;
 
  @Inject()
  taskInfoQueue: TaskInfoQueue;
 
  @App()
  app: IMidwayWebApplication;
 
  @Inject()
  utils: Utils;
 
  /**
   * 停止任务
   * @param id
   */
  async stop(id) {
    const task = await this.taskInfoEntity.findOne({ id });
    if (task) {
      const job = await this.exist(task.id);
      if (job) {
        await this.taskInfoQueue.removeRepeatable(JSON.parse(task.repeatConf));
      }
      task.status = 0;
      await this.taskInfoEntity.update(task.id, task);
      await this.updateNextRunTime(task.id);
    }
  }
 
  /**
   * 开始任务
   * @param id
   * @param type
   */
  async start(id, type?) {
    const task = await this.taskInfoEntity.findOne({ id });
    task.status = 1;
    if (type) {
      task.type = type;
    }
    await this.addOrUpdate(task);
  }
 
  /**
   * 手动执行一次
   * @param id
   */
  async once(id) {
    const task = await this.taskInfoEntity.findOne({ id });
    if (task) {
      await this.taskInfoQueue.add(
        {
          ...task,
          isOnce: true,
        },
        {
          jobId: task.id.toString(),
          removeOnComplete: true,
          removeOnFail: true,
        }
      );
    }
  }
 
  /**
   * 检查任务是否存在
   * @param jobId
   */
  async exist(jobId) {
    const result = await this.taskInfoQueue.getRepeatableJobs();
    const ids = result.map(e => {
      return e.id;
    });
    return ids.includes(jobId.toString());
  }
 
  /**
   * 新增或修改
   * @param params
   */
  async addOrUpdate(params) {
    let repeatConf;
    await this.getOrmManager().transaction(async transactionalEntityManager => {
      if (params.taskType === 0) {
        params.limit = null;
        params.every = null;
      } else {
        params.cron = null;
      }
      await transactionalEntityManager.save(TaskInfoEntity, params);
 
      if (params.status === 1) {
        const exist = await this.exist(params.id);
        if (exist) {
          await this.taskInfoQueue.removeRepeatable(
            JSON.parse(params.repeatConf)
          );
        }
        const { every, limit, startDate, endDate, cron } = params;
        const repeat = {
          every,
          limit,
          jobId: params.id,
          startDate,
          endDate,
          cron,
        };
        await this.utils.removeEmptyP(repeat);
        const result = await this.taskInfoQueue.add(params, {
          jobId: params.id,
          removeOnComplete: true,
          removeOnFail: true,
          repeat,
        });
        if (!result) {
          throw new Error('任务添加失败,请检查任务配置');
        }
        // await transactionalEntityManager.update(TaskInfoEntity, params.id, {
        //   jobId: opts.jobId,
        // });
        repeatConf = result.opts;
      }
    });
    if (params.status === 1) {
      this.utils.sleep(1000);
      await this.updateNextRunTime(params.id);
      await this.nativeQuery(
        'update task_info a set a.repeatConf = ? where a.id = ?',
        [JSON.stringify(repeatConf.repeat), params.id]
      );
    }
  }
 
  /**
   * 删除
   * @param ids
   */
  async delete(ids) {
    let idArr;
    if (ids instanceof Array) {
      idArr = ids;
    } else {
      idArr = ids.split(',');
    }
    for (const id of idArr) {
      const task = await this.taskInfoEntity.findOne({ id });
      const exist = await this.exist(task.id);
      if (exist) {
        await this.taskInfoQueue.removeRepeatable(JSON.parse(task.repeatConf));
      }
      await this.taskInfoEntity.delete({ id });
      await this.taskLogEntity.delete({ taskId: id });
    }
  }
 
  /**
   * 任务日志
   * @param query
   */
  async log(query) {
    const { id, status } = query;
    return await this.sqlRenderPage(
      `
      SELECT
          a.*,
          b.NAME AS taskName
      FROM
      task_log a
      JOIN task_info b ON a.taskId = b.id
      where 1=1
      ${this.setSql(id, 'and a.taskId = ?', [id])}
      ${this.setSql(status, 'and a.status = ?', [status])}
      `,
      query
    );
  }
 
  /**
   * 保存任务记录,成功任务每个任务保留最新20条日志,失败日志不会删除
   * @param task
   * @param status
   * @param detail
   */
  async record(task, status, detail?) {
    await this.taskLogEntity.save({
      taskId: task.id,
      status,
      detail: detail || '',
    });
    await this.nativeQuery(
      `DELETE a
      FROM
      task_log a,
          ( SELECT id FROM task_log where taskId = ? AND status = 1 ORDER BY id DESC LIMIT ?, 1 ) b
      WHERE
      a.taskId = ? AND
      a.status = 1 AND
      a.id < b.id`,
      [task.id, 19, task.id]
    ); // 日志保留最新的20条
  }
 
  /**
   * 初始化任务
   */
  async initTask() {
    const runningTasks = await this.taskInfoEntity.find({ status: 1 });
    if (!_.isEmpty(runningTasks)) {
      for (const task of runningTasks) {
        const job = await this.exist(task.id); // 任务已存在就不添加
        if (!job) {
          this.logger.info(`init task ${task.name}`);
          await this.addOrUpdate(task);
        }
      }
    }
  }
 
  /**
   * 任务ID
   * @param jobId
   */
  async getNextRunTime(jobId) {
    let nextRunTime;
    const result = await this.taskInfoQueue.getRepeatableJobs();
    const task = _.find(result, { id: jobId + '' });
    if (task) {
      nextRunTime = new Date(task.next);
    }
    return nextRunTime;
  }
 
  /**
   * 更新下次执行时间
   * @param jobId
   */
  async updateNextRunTime(jobId) {
    await this.nativeQuery(
      'update task_info a set a.nextRunTime = ? where a.id = ?',
      [await this.getNextRunTime(jobId), jobId]
    );
  }
 
  /**
   * 刷新任务状态
   */
  async updateStatus(jobId) {
    const result = await this.taskInfoQueue.getRepeatableJobs();
    const job = _.find(result, { id: jobId + '' });
    // @ts-ignore
    const task = await this.taskInfoEntity.findOne({ id: job.id });
    const nextTime = await this.getNextRunTime(task.id);
    if (task) {
      if (task.nextRunTime.getTime() == nextTime.getTime()) {
        task.status = 0;
        task.nextRunTime = nextTime;
        this.taskInfoQueue.removeRepeatableByKey(job.key);
      } else {
        task.nextRunTime = nextTime;
      }
      await this.taskInfoEntity.update(task.id, task);
    }
  }
 
  /**
   * 调用service
   * @param serviceStr
   */
  async invokeService(serviceStr) {
    if (serviceStr) {
      const arr = serviceStr.split('.');
      const service = await this.app.getApplicationContext().getAsync(arr[0]);
      for (const child of arr) {
        if (child.includes('(')) {
          const lastArr = child.split('(');
          const param = lastArr[1].replace(')', '');
          if (!param) {
            return service[lastArr[0]]();
          } else {
            return service[lastArr[0]](JSON.parse(param));
          }
        }
      }
    }
  }
}