feat-pipelines #1

Merged
Ivan merged 25 commits from feat-pipelines into master 2021-03-24 20:50:41 +08:00
3 changed files with 38 additions and 6 deletions
Showing only changes of commit 31a200206f - Show all commits

View File

@ -0,0 +1,16 @@
import { OnQueueCompleted, Process, Processor } from '@nestjs/bull';
import { Job } from 'bull';
import { PipelineTask } from './pipeline-task.entity';
import { PIPELINE_TASK_QUEUE } from './pipeline-tasks.constants';
import { PipelineTasksService } from './pipeline-tasks.service';
@Processor(PIPELINE_TASK_QUEUE)
export class PipelineTaskConsumer {
constructor(private readonly service: PipelineTasksService) {}
@Process()
async doTask() {}
@OnQueueCompleted()
onCompleted(job: Job<PipelineTask>) {
this.service.doNextTask(job.data.pipeline);
}
}

View File

@ -2,6 +2,10 @@ import { Test, TestingModule } from '@nestjs/testing';
import { PipelineTasksService } from './pipeline-tasks.service';
import { getRepositoryToken } from '@nestjs/typeorm';
import { PipelineTask } from './pipeline-task.entity';
import { PIPELINE_TASK_QUEUE } from './pipeline-tasks.constants';
import { getQueueToken } from '@nestjs/bull';
import { RedisService } from 'nestjs-redis';
import { Pipeline } from '../pipelines/pipeline.entity';
describe('PipelineTasksService', () => {
let service: PipelineTasksService;
@ -14,6 +18,19 @@ describe('PipelineTasksService', () => {
provide: getRepositoryToken(PipelineTask),
useValue: {},
},
PipelineTasksService,
{
provide: getRepositoryToken(Pipeline),
useValue: {},
},
{
provide: getQueueToken(PIPELINE_TASK_QUEUE),
useValue: {},
},
{
provide: RedisService,
useValue: {},
},
],
}).compile();

View File

@ -44,13 +44,12 @@ export class PipelineTasksService {
}
}
async doTask(task: PipelineTask) {
const tasksKey = this.getRedisTokens(task.pipeline)[1];
async doNextTask(pipeline: Pipeline) {
const tasksKey = this.getRedisTokens(pipeline)[1];
const redis = this.redis.getClient();
const nextTask = await redis.rpop(tasksKey);
if (nextTask) {
this.doTask(task).then();
const task = JSON.parse((await redis.rpop(tasksKey)) ?? 'null');
if (task) {
this.queue.add(task);
}
}