feat(pipeline-tasks): debug log.

This commit is contained in:
Ivan
2021-03-25 18:09:45 +08:00
parent 607a4f57de
commit 713f5b2426
5 changed files with 203 additions and 28 deletions

View File

@ -12,6 +12,9 @@ import { LockFailedException } from '../commons/exceptions/lock-failed.exception
import { PubSub } from 'apollo-server-express';
import { TaskStatuses } from './enums/task-statuses.enum';
import { isNil } from 'ramda';
import debug from 'debug';
const log = debug('fennec:pipeline-tasks:service');
@Injectable()
export class PipelineTasksService {
@ -30,23 +33,30 @@ export class PipelineTasksService {
where: { id: dto.pipelineId },
relations: ['project'],
});
// const hasUnfinishedTask = await this.repository
// .findOne({
// pipelineId: pipeline.id,
// status: In([TaskStatuses.pending, TaskStatuses.working]),
// })
// .then((val) => !isNil(val));
// if (hasUnfinishedTask) {
// throw new ConflictException(
// 'there are still unfinished task in the current pipeline.',
// );
// }
const hasUnfinishedTask = await this.repository
.findOne({
pipelineId: dto.pipelineId,
commit: dto.commit,
status: In([TaskStatuses.pending, TaskStatuses.working]),
})
.then((val) => !isNil(val));
if (hasUnfinishedTask) {
throw new ConflictException(
'There are the same tasks among the unfinished tasks!',
);
}
const task = await this.repository.save(this.repository.create(dto));
task.pipeline = pipeline;
const tasksKey = this.getRedisTokens(pipeline)[1];
const redis = this.redis.getClient();
await redis.lpush(tasksKey, JSON.stringify(task));
log(
'add task %s:%s-%s',
task.id,
task.pipeline.branch,
task.commit.slice(0, 6),
);
await this.doNextTask(pipeline);
return task;
}
@ -63,6 +73,7 @@ export class PipelineTasksService {
const [lckKey, tasksKey] = this.getRedisTokens(pipeline);
const redis = this.redis.getClient();
log('doNextTask()');
const unLck = await new Promise<() => Promise<void>>(
async (resolve, reject) => {
const lckValue = Date.now().toString();
@ -90,7 +101,15 @@ export class PipelineTasksService {
(await redis.rpop(tasksKey).finally(() => unLck())) ?? 'null',
);
if (task) {
log(
'add task (%s:%s-%s) to queue',
task.id,
task.pipeline.branch,
task.commit.slice(0, 6),
);
await this.queue.add(task);
} else {
log('task is empty');
}
}