2021-03-01 18:14:13 +08:00
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
import { PipelineTask } from './pipeline-task.entity';
|
|
|
|
import { Repository } from 'typeorm';
|
|
|
|
import { CreatePipelineTaskInput } from './dtos/create-pipeline-task.input';
|
|
|
|
import { RedisService } from 'nestjs-redis';
|
|
|
|
import { Pipeline } from '../pipelines/pipeline.entity';
|
|
|
|
import { InjectQueue } from '@nestjs/bull';
|
|
|
|
import { PIPELINE_TASK_QUEUE } from './pipeline-tasks.constants';
|
|
|
|
import { Queue } from 'bull';
|
2021-03-03 17:24:22 +08:00
|
|
|
import { LockFailedException } from '../commons/exceptions/lock-failed.exception';
|
2021-03-01 18:14:13 +08:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class PipelineTasksService {
|
|
|
|
constructor(
|
|
|
|
@InjectRepository(PipelineTask)
|
|
|
|
private readonly repository: Repository<PipelineTask>,
|
|
|
|
@InjectRepository(Pipeline)
|
|
|
|
private readonly pipelineRepository: Repository<Pipeline>,
|
|
|
|
private readonly redis: RedisService,
|
|
|
|
@InjectQueue(PIPELINE_TASK_QUEUE)
|
|
|
|
private readonly queue: Queue<PipelineTask>,
|
|
|
|
) {}
|
|
|
|
async addTask(dto: CreatePipelineTaskInput) {
|
|
|
|
const pipeline = await this.pipelineRepository.findOneOrFail({
|
|
|
|
where: { id: dto.pipelineId },
|
|
|
|
relations: ['project'],
|
|
|
|
});
|
|
|
|
const task = await this.repository.save(this.repository.create(dto));
|
|
|
|
task.pipeline = pipeline;
|
|
|
|
|
|
|
|
const [lckKey, tasksKey] = this.getRedisTokens(pipeline);
|
|
|
|
const redis = this.redis.getClient();
|
2021-03-03 17:24:22 +08:00
|
|
|
await redis.lpush(tasksKey, JSON.stringify(task)).finally(() => {
|
|
|
|
return redis.decr(lckKey);
|
|
|
|
});
|
|
|
|
await this.doNextTask(pipeline);
|
2021-03-01 18:14:13 +08:00
|
|
|
}
|
|
|
|
|
2021-03-02 16:28:37 +08:00
|
|
|
async doNextTask(pipeline: Pipeline) {
|
2021-03-03 17:24:22 +08:00
|
|
|
const [lckKey, tasksKey] = this.getRedisTokens(pipeline);
|
2021-03-01 18:14:13 +08:00
|
|
|
const redis = this.redis.getClient();
|
2021-03-03 17:24:22 +08:00
|
|
|
await redis.set(lckKey, 0, 'EX', 10, 'NX');
|
|
|
|
|
|
|
|
await new Promise(async (resolve, reject) => {
|
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
if ((await redis.incr(lckKey)) === 1) {
|
|
|
|
resolve(undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await redis.decr(lckKey);
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
|
|
}
|
|
|
|
reject(new LockFailedException(lckKey));
|
|
|
|
});
|
|
|
|
|
|
|
|
const task = JSON.parse(
|
|
|
|
(await redis.rpop(tasksKey).finally(() => redis.decr(lckKey))) ?? 'null',
|
|
|
|
);
|
2021-03-02 16:28:37 +08:00
|
|
|
if (task) {
|
2021-03-03 17:24:22 +08:00
|
|
|
await this.queue.add(task);
|
2021-03-01 18:14:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getRedisTokens(pipeline: Pipeline): [string, string] {
|
|
|
|
return [`pipeline-${pipeline.id}:lck`, `pipeline-${pipeline.id}:tasks`];
|
|
|
|
}
|
|
|
|
}
|