feat: 使用 PubSub 进行消息广播。
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import { PipelineTask } from './../pipeline-task.entity';
|
||||
import { PipelineUnits } from '../enums/pipeline-units.enum';
|
||||
import { Field, HideField, ObjectType } from '@nestjs/graphql';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
@ObjectType()
|
||||
export class PipelineTaskLogMessage {
|
||||
@ -9,6 +10,7 @@ export class PipelineTaskLogMessage {
|
||||
@Field(() => PipelineUnits, { nullable: true })
|
||||
unit?: PipelineUnits;
|
||||
@Field()
|
||||
@Type(() => Date)
|
||||
time: Date;
|
||||
@Field()
|
||||
message: string;
|
||||
|
@ -1,8 +1,9 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { log } from 'console';
|
||||
import { PubSub } from 'graphql-subscriptions';
|
||||
import { observableToAsyncIterable } from 'graphql-tools';
|
||||
import { RedisService } from 'nestjs-redis';
|
||||
import { find, omit, propEq } from 'ramda';
|
||||
import { InjectPubSub } from '../commons/pub-sub/decorators/inject-pub-sub.decorator';
|
||||
import { PubSub } from '../commons/pub-sub/pub-sub';
|
||||
import { PipelineUnits } from './enums/pipeline-units.enum';
|
||||
import { TaskStatuses } from './enums/task-statuses.enum';
|
||||
import { PipelineTaskLogMessage } from './models/pipeline-task-log-message.module';
|
||||
@ -13,9 +14,11 @@ const LOG_TIMEOUT_SECONDS = 10_000;
|
||||
|
||||
@Injectable()
|
||||
export class PipelineTaskLogsService {
|
||||
constructor(private readonly redisService: RedisService) {}
|
||||
|
||||
pubSub = new PubSub();
|
||||
constructor(
|
||||
private readonly redisService: RedisService,
|
||||
@InjectPubSub()
|
||||
private readonly pubSub: PubSub,
|
||||
) {}
|
||||
|
||||
get redis() {
|
||||
return this.redisService.getClient();
|
||||
@ -73,6 +76,6 @@ export class PipelineTaskLogsService {
|
||||
}
|
||||
|
||||
watchLogs(task: PipelineTask) {
|
||||
return this.pubSub.asyncIterator(this.getKeys(task));
|
||||
return observableToAsyncIterable(this.pubSub.message$(this.getKeys(task)));
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,2 @@
|
||||
export const PIPELINE_TASK_QUEUE = 'PIPELINE_TASK_QUEUE';
|
||||
export const PIPELINE_TASK_LOG_QUEUE = 'PIPELINE_TASK_LOG_QUEUE';
|
||||
export const PIPELINE_TASK_LOG_PUBSUB = 'PIPELINE_TASK_LOG_PUBSUB';
|
||||
|
@ -8,12 +8,10 @@ import { ReposModule } from '../repos/repos.module';
|
||||
import { RedisModule } from 'nestjs-redis';
|
||||
import { BullModule } from '@nestjs/bull';
|
||||
import { PipelineTaskConsumer } from './pipeline-task.consumer';
|
||||
import {
|
||||
PIPELINE_TASK_QUEUE,
|
||||
PIPELINE_TASK_LOG_PUBSUB,
|
||||
} from './pipeline-tasks.constants';
|
||||
import { PIPELINE_TASK_QUEUE } from './pipeline-tasks.constants';
|
||||
import { PipelineTaskLogsService } from './pipeline-task-logs.service';
|
||||
import { PubSub } from 'apollo-server-express';
|
||||
import { PubSubModule } from '../commons/pub-sub/pub-sub.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -21,6 +19,7 @@ import { PubSub } from 'apollo-server-express';
|
||||
BullModule.registerQueue({
|
||||
name: PIPELINE_TASK_QUEUE,
|
||||
}),
|
||||
PubSubModule.forFeature(),
|
||||
RedisModule,
|
||||
ReposModule,
|
||||
],
|
||||
@ -29,10 +28,6 @@ import { PubSub } from 'apollo-server-express';
|
||||
PipelineTasksResolver,
|
||||
PipelineTaskConsumer,
|
||||
PipelineTaskLogsService,
|
||||
{
|
||||
provide: Symbol(PIPELINE_TASK_LOG_PUBSUB),
|
||||
useValue: new PubSub(),
|
||||
},
|
||||
],
|
||||
exports: [PipelineTasksService],
|
||||
})
|
||||
|
@ -5,6 +5,7 @@ import { CreatePipelineTaskInput } from './dtos/create-pipeline-task.input';
|
||||
import { PipelineTaskLogMessage } from './models/pipeline-task-log-message.module';
|
||||
import { PipelineTaskLogArgs } from './dtos/pipeline-task-log.args';
|
||||
import { PipelineTaskLogsService } from './pipeline-task-logs.service';
|
||||
import { plainToClass } from 'class-transformer';
|
||||
|
||||
@Resolver()
|
||||
export class PipelineTasksResolver {
|
||||
@ -20,7 +21,8 @@ export class PipelineTasksResolver {
|
||||
|
||||
@Subscription(() => PipelineTaskLogMessage, {
|
||||
resolve: (value) => {
|
||||
return value;
|
||||
const data = plainToClass(PipelineTaskLogMessage, value);
|
||||
return data;
|
||||
},
|
||||
})
|
||||
async pipelineTaskLog(@Args() args: PipelineTaskLogArgs) {
|
||||
|
@ -9,16 +9,17 @@ import { InjectQueue } from '@nestjs/bull';
|
||||
import { PIPELINE_TASK_QUEUE } from './pipeline-tasks.constants';
|
||||
import { Queue } from 'bull';
|
||||
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';
|
||||
import { InjectPubSub } from '../commons/pub-sub/decorators/inject-pub-sub.decorator';
|
||||
import { PubSub } from '../commons/pub-sub/pub-sub';
|
||||
import { observableToAsyncIterable } from '@graphql-tools/utils';
|
||||
|
||||
const log = debug('fennec:pipeline-tasks:service');
|
||||
|
||||
@Injectable()
|
||||
export class PipelineTasksService {
|
||||
pubSub = new PubSub();
|
||||
constructor(
|
||||
@InjectRepository(PipelineTask)
|
||||
private readonly repository: Repository<PipelineTask>,
|
||||
@ -27,6 +28,8 @@ export class PipelineTasksService {
|
||||
@InjectQueue(PIPELINE_TASK_QUEUE)
|
||||
private readonly queue: Queue<PipelineTask>,
|
||||
private readonly redis: RedisService,
|
||||
@InjectPubSub()
|
||||
private readonly pubSub: PubSub,
|
||||
) {}
|
||||
async addTask(dto: CreatePipelineTaskInput) {
|
||||
const pipeline = await this.pipelineRepository.findOneOrFail({
|
||||
@ -118,12 +121,12 @@ export class PipelineTasksService {
|
||||
}
|
||||
|
||||
async updateTask(task: PipelineTask) {
|
||||
this.pubSub.publish(task.id, task);
|
||||
this.pubSub.publish(`task:${task.id}`, task);
|
||||
return await this.repository.save(task);
|
||||
}
|
||||
|
||||
async watchTaskUpdated(id: string) {
|
||||
return this.pubSub.asyncIterator(id);
|
||||
return observableToAsyncIterable(this.pubSub.message$(`task:${id}`));
|
||||
}
|
||||
|
||||
getRedisTokens(pipeline: Pipeline): [string, string] {
|
||||
|
Reference in New Issue
Block a user