Compare commits
No commits in common. "752db8a0c5b6670b19e0a572799c3ad12c80b762" and "092cf9c41849c940ef127c9fdf415577d0cca175" have entirely different histories.
752db8a0c5
...
092cf9c418
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -6,8 +6,6 @@
|
||||
"lpush",
|
||||
"lrange",
|
||||
"metatype",
|
||||
"pmessage",
|
||||
"psubscribe",
|
||||
"rpop",
|
||||
"rpush"
|
||||
]
|
||||
|
26
package-lock.json
generated
26
package-lock.json
generated
@ -27,7 +27,6 @@
|
||||
"debug": "^4.3.1",
|
||||
"graphql": "^15.5.0",
|
||||
"graphql-tools": "^7.0.2",
|
||||
"ioredis": "^4.25.0",
|
||||
"js-yaml": "^4.0.0",
|
||||
"nestjs-redis": "^1.2.8",
|
||||
"observable-to-async-generator": "^1.0.1-rc",
|
||||
@ -46,7 +45,6 @@
|
||||
"@types/body-parser": "^1.19.0",
|
||||
"@types/debug": "^4.1.5",
|
||||
"@types/express": "^4.17.8",
|
||||
"@types/ioredis": "^4.22.2",
|
||||
"@types/jest": "^26.0.15",
|
||||
"@types/js-yaml": "^4.0.0",
|
||||
"@types/node": "^14.14.6",
|
||||
@ -3280,9 +3278,9 @@
|
||||
"integrity": "sha512-2aoSC4UUbHDj2uCsCxcG/vRMXey/m17bC7UwitVm5hn22nI8O8Y9iDpA76Orc+DWkQ4zZrOKEshCqR/jSuXAHA=="
|
||||
},
|
||||
"node_modules/@types/ioredis": {
|
||||
"version": "4.22.2",
|
||||
"resolved": "https://registry.npmjs.org/@types/ioredis/-/ioredis-4.22.2.tgz",
|
||||
"integrity": "sha512-32nEh2Eq20xlgtIqVx1kkJkpqh5V1wqMsumK8Y9Izma/vW9k8Ro7sD+y4G6SyCqz8u9+HcssZ6+E83Modta38w==",
|
||||
"version": "4.22.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/ioredis/-/ioredis-4.22.1.tgz",
|
||||
"integrity": "sha512-GxXT828fkvBeThO68ZJg8cD2haqea5ANBJaxA+UZqLranNkEnQ8N7QLPtykwWbN/sRQz75O7kj+PNmCKF4CEKw==",
|
||||
"dependencies": {
|
||||
"@types/node": "*"
|
||||
}
|
||||
@ -8555,9 +8553,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/ioredis": {
|
||||
"version": "4.25.0",
|
||||
"resolved": "https://registry.npmjs.org/ioredis/-/ioredis-4.25.0.tgz",
|
||||
"integrity": "sha512-UoeqXpZB05aerGD3gB9NiigMsAyph+N+GWH8+3lX1+26caVV03GkL6JoLxS2HCxyvqCWbNsVSZTAp5W12qe23A==",
|
||||
"version": "4.24.4",
|
||||
"resolved": "https://registry.npmjs.org/ioredis/-/ioredis-4.24.4.tgz",
|
||||
"integrity": "sha512-v28xxBENyTmReC6lVTL7EkAPjVF8cuGtDEjGDi1Z2n7htsC2WdiDceZrCIPeuPiLa6kDFWHb1Y8O0upZROsMgA==",
|
||||
"dependencies": {
|
||||
"cluster-key-slot": "^1.1.0",
|
||||
"debug": "^4.3.1",
|
||||
@ -18260,9 +18258,9 @@
|
||||
"integrity": "sha512-2aoSC4UUbHDj2uCsCxcG/vRMXey/m17bC7UwitVm5hn22nI8O8Y9iDpA76Orc+DWkQ4zZrOKEshCqR/jSuXAHA=="
|
||||
},
|
||||
"@types/ioredis": {
|
||||
"version": "4.22.2",
|
||||
"resolved": "https://registry.npmjs.org/@types/ioredis/-/ioredis-4.22.2.tgz",
|
||||
"integrity": "sha512-32nEh2Eq20xlgtIqVx1kkJkpqh5V1wqMsumK8Y9Izma/vW9k8Ro7sD+y4G6SyCqz8u9+HcssZ6+E83Modta38w==",
|
||||
"version": "4.22.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/ioredis/-/ioredis-4.22.1.tgz",
|
||||
"integrity": "sha512-GxXT828fkvBeThO68ZJg8cD2haqea5ANBJaxA+UZqLranNkEnQ8N7QLPtykwWbN/sRQz75O7kj+PNmCKF4CEKw==",
|
||||
"requires": {
|
||||
"@types/node": "*"
|
||||
}
|
||||
@ -22387,9 +22385,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"ioredis": {
|
||||
"version": "4.25.0",
|
||||
"resolved": "https://registry.npmjs.org/ioredis/-/ioredis-4.25.0.tgz",
|
||||
"integrity": "sha512-UoeqXpZB05aerGD3gB9NiigMsAyph+N+GWH8+3lX1+26caVV03GkL6JoLxS2HCxyvqCWbNsVSZTAp5W12qe23A==",
|
||||
"version": "4.24.4",
|
||||
"resolved": "https://registry.npmjs.org/ioredis/-/ioredis-4.24.4.tgz",
|
||||
"integrity": "sha512-v28xxBENyTmReC6lVTL7EkAPjVF8cuGtDEjGDi1Z2n7htsC2WdiDceZrCIPeuPiLa6kDFWHb1Y8O0upZROsMgA==",
|
||||
"requires": {
|
||||
"cluster-key-slot": "^1.1.0",
|
||||
"debug": "^4.3.1",
|
||||
|
@ -40,7 +40,6 @@
|
||||
"debug": "^4.3.1",
|
||||
"graphql": "^15.5.0",
|
||||
"graphql-tools": "^7.0.2",
|
||||
"ioredis": "^4.25.0",
|
||||
"js-yaml": "^4.0.0",
|
||||
"nestjs-redis": "^1.2.8",
|
||||
"observable-to-async-generator": "^1.0.1-rc",
|
||||
@ -59,7 +58,6 @@
|
||||
"@types/body-parser": "^1.19.0",
|
||||
"@types/debug": "^4.1.5",
|
||||
"@types/express": "^4.17.8",
|
||||
"@types/ioredis": "^4.22.2",
|
||||
"@types/jest": "^26.0.15",
|
||||
"@types/js-yaml": "^4.0.0",
|
||||
"@types/node": "^14.14.6",
|
||||
|
@ -16,7 +16,6 @@ import { RawBodyMiddleware } from './commons/middlewares/raw-body.middleware';
|
||||
import { GiteaWebhooksController } from './webhooks/gitea-webhooks.controller';
|
||||
import { ParseBodyMiddleware } from './commons/middlewares/parse-body.middleware';
|
||||
import { BullModule } from '@nestjs/bull';
|
||||
import { PubSubModule } from './commons/pub-sub/pub-sub.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -52,19 +51,8 @@ import { PubSubModule } from './commons/pub-sub/pub-sub.module';
|
||||
useFactory: (configService: ConfigService) => ({
|
||||
redis: {
|
||||
host: configService.get<string>('db.redis.host', 'localhost'),
|
||||
port: configService.get<number>('db.redis.port', undefined),
|
||||
password: configService.get<string>('db.redis.password', undefined),
|
||||
},
|
||||
}),
|
||||
inject: [ConfigService],
|
||||
}),
|
||||
PubSubModule.forRootAsync({
|
||||
imports: [ConfigModule],
|
||||
useFactory: (configService: ConfigService) => ({
|
||||
redis: {
|
||||
host: configService.get<string>('db.redis.host', 'localhost'),
|
||||
port: configService.get<number>('db.redis.port', undefined),
|
||||
password: configService.get<string>('db.redis.password', undefined),
|
||||
port: configService.get<number>('db.redis.port', 6379),
|
||||
password: configService.get<string>('db.redis.password', ''),
|
||||
},
|
||||
}),
|
||||
inject: [ConfigService],
|
||||
|
@ -1,10 +1,8 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { PasswordConverter } from './services/password-converter';
|
||||
import { PubSubModule } from './pub-sub/pub-sub.module';
|
||||
|
||||
@Module({
|
||||
providers: [PasswordConverter],
|
||||
exports: [PasswordConverter],
|
||||
imports: [PubSubModule],
|
||||
})
|
||||
export class CommonsModule {}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { ArgumentMetadata, Injectable, PipeTransform } from '@nestjs/common';
|
||||
import { plainToClass } from 'class-transformer';
|
||||
import { sanitize } from '@neuralegion/class-sanitizer/dist';
|
||||
|
||||
@Injectable()
|
||||
export class SanitizePipe implements PipeTransform {
|
||||
@ -12,11 +12,13 @@ export class SanitizePipe implements PipeTransform {
|
||||
return value;
|
||||
}
|
||||
const constructorFunction = metadata.metatype;
|
||||
if (!constructorFunction || value instanceof constructorFunction) {
|
||||
if (!constructorFunction) {
|
||||
return value;
|
||||
}
|
||||
value = Object.assign(new constructorFunction(), value);
|
||||
try {
|
||||
return plainToClass(constructorFunction, value);
|
||||
sanitize(value);
|
||||
return value;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw err;
|
||||
|
@ -1,5 +0,0 @@
|
||||
import { Inject } from '@nestjs/common';
|
||||
import { getPubSubToken } from '../utils/token';
|
||||
|
||||
export const InjectPubSub = (name?: string): ParameterDecorator =>
|
||||
Inject(getPubSubToken(name));
|
@ -1,8 +0,0 @@
|
||||
import { ModuleMetadata } from '@nestjs/common';
|
||||
import { PubSubOptions } from './pub-sub-options.interface';
|
||||
|
||||
export interface PubSubAsyncConfig extends Pick<ModuleMetadata, 'imports'> {
|
||||
name?: string;
|
||||
useFactory: (...args: any[]) => Promise<PubSubOptions> | PubSubOptions;
|
||||
inject?: any[];
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
import { RedisOptions } from 'ioredis';
|
||||
|
||||
export interface PubSubOptions {
|
||||
name?: string;
|
||||
redis: RedisOptions;
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
export type PubSubRawMessage<T> =
|
||||
| PubSubRawNextMessage<T>
|
||||
| PubSubRawErrorMessage<T>
|
||||
| PubSubRawCompleteMessage<T>;
|
||||
export interface PubSubRawNextMessage<T> {
|
||||
type: 'next';
|
||||
message: T;
|
||||
}
|
||||
export interface PubSubRawErrorMessage<T> {
|
||||
type: 'error';
|
||||
error: string;
|
||||
}
|
||||
export interface PubSubRawCompleteMessage<T> {
|
||||
type: 'complete';
|
||||
}
|
@ -1 +0,0 @@
|
||||
export const DEFAULT_PUB_SUB_NAME = 'default';
|
@ -1,48 +0,0 @@
|
||||
import { DynamicModule, Module } from '@nestjs/common';
|
||||
import { PubSubService } from './pub-sub.service';
|
||||
import {
|
||||
createAsyncPubSubProviders,
|
||||
createPubSubProvider,
|
||||
} from './pub-sub.providers';
|
||||
import { PubSubOptions } from './interfaces/pub-sub-options.interface';
|
||||
import { PubSubAsyncConfig } from './interfaces/pub-sub-async-config.interface';
|
||||
import { getPubSubConfigToken } from './utils/token';
|
||||
|
||||
@Module({
|
||||
providers: [PubSubService],
|
||||
})
|
||||
export class PubSubModule {
|
||||
public static forRoot(options: PubSubOptions): DynamicModule {
|
||||
const providers = [
|
||||
{
|
||||
provide: getPubSubConfigToken(options.name),
|
||||
useValue: options,
|
||||
},
|
||||
createPubSubProvider(options.name),
|
||||
];
|
||||
return {
|
||||
global: true,
|
||||
module: PubSubModule,
|
||||
providers,
|
||||
exports: providers,
|
||||
};
|
||||
}
|
||||
public static forRootAsync(...configs: PubSubAsyncConfig[]): DynamicModule {
|
||||
const providers = createAsyncPubSubProviders(configs);
|
||||
return {
|
||||
global: true,
|
||||
module: PubSubModule,
|
||||
imports: configs
|
||||
.map((config) => config.imports)
|
||||
.flat()
|
||||
.filter((o, i, a) => a.indexOf(o) === i),
|
||||
providers,
|
||||
exports: providers,
|
||||
};
|
||||
}
|
||||
public static forFeature(): DynamicModule {
|
||||
return {
|
||||
module: PubSubModule,
|
||||
};
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
import { Provider } from '@nestjs/common';
|
||||
import { PubSubAsyncConfig } from './interfaces/pub-sub-async-config.interface';
|
||||
import { PubSubOptions } from './interfaces/pub-sub-options.interface';
|
||||
import { PubSub } from './pub-sub';
|
||||
import { getPubSubConfigToken, getPubSubToken } from './utils/token';
|
||||
|
||||
export function createPubSubProvider(name: string): Provider {
|
||||
return {
|
||||
provide: getPubSubToken(name),
|
||||
useFactory: (option: PubSubOptions) => new PubSub(option),
|
||||
inject: [getPubSubConfigToken(name)],
|
||||
};
|
||||
}
|
||||
|
||||
export function createOptionsProvider(config: PubSubAsyncConfig): Provider {
|
||||
return {
|
||||
provide: getPubSubConfigToken(config.name),
|
||||
useFactory: config.useFactory,
|
||||
inject: config.inject || [],
|
||||
};
|
||||
}
|
||||
export function createAsyncPubSubProviders(
|
||||
configs: PubSubAsyncConfig[],
|
||||
): Provider[] {
|
||||
return configs
|
||||
.map((config) => [
|
||||
createOptionsProvider(config),
|
||||
createPubSubProvider(config.name),
|
||||
])
|
||||
.flat();
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { PubSubService } from './pub-sub.service';
|
||||
|
||||
describe('PubsubService', () => {
|
||||
let service: PubSubService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [PubSubService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<PubSubService>(PubSubService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
@ -1,9 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PubSubOptions } from './interfaces/pub-sub-options.interface';
|
||||
|
||||
@Injectable()
|
||||
export class PubSubService {
|
||||
private options = new Map<string, PubSubOptions>();
|
||||
private pubClient;
|
||||
private;
|
||||
}
|
@ -1,87 +0,0 @@
|
||||
import debug from 'debug';
|
||||
import { tap } from 'rxjs/operators';
|
||||
import { PubSub } from './pub-sub';
|
||||
|
||||
debug.enable('app:pubsub:*');
|
||||
|
||||
describe('PubSub', () => {
|
||||
let instance: PubSub;
|
||||
|
||||
beforeEach(async () => {
|
||||
instance = new PubSub({
|
||||
name: 'default',
|
||||
redis: {
|
||||
host: 'localhost',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(instance).toBeDefined();
|
||||
});
|
||||
|
||||
it('should can send and receive message', async () => {
|
||||
const arr = new Array(10)
|
||||
.fill(undefined)
|
||||
.map(() => Math.random().toString(36).slice(2, 8));
|
||||
const results: string[] = [];
|
||||
instance
|
||||
.message$<string>('test')
|
||||
.pipe(
|
||||
tap((val) => {
|
||||
console.log(val);
|
||||
}),
|
||||
)
|
||||
.subscribe((val) => results.push(val));
|
||||
await new Promise((r) => setTimeout(r, 1000));
|
||||
await Promise.all([...arr.map((str) => instance.publish('test', str))]);
|
||||
await new Promise((r) => setTimeout(r, 1000));
|
||||
expect(results).toMatchObject(arr);
|
||||
});
|
||||
|
||||
it('should complete', async () => {
|
||||
const arr = new Array(10)
|
||||
.fill(undefined)
|
||||
.map(() => Math.random().toString(36).slice(2, 8));
|
||||
const results: string[] = [];
|
||||
instance
|
||||
.message$<string>('test')
|
||||
.pipe(
|
||||
tap((val) => {
|
||||
console.log(val);
|
||||
}),
|
||||
)
|
||||
.subscribe((val) => results.push(val));
|
||||
await new Promise((r) => setTimeout(r, 1000));
|
||||
await Promise.all([...arr.map((str) => instance.publish('test', str))]);
|
||||
await instance.finish('test');
|
||||
await Promise.all([...arr.map((str) => instance.publish('test', str))]);
|
||||
await new Promise((r) => setTimeout(r, 1000));
|
||||
expect(results).toMatchObject(arr);
|
||||
});
|
||||
it('should error', async () => {
|
||||
const arr = new Array(10)
|
||||
.fill(undefined)
|
||||
.map(() => Math.random().toString(36).slice(2, 8));
|
||||
const results: string[] = [];
|
||||
let error: string;
|
||||
instance
|
||||
.message$<string>('test')
|
||||
.pipe(
|
||||
tap((val) => {
|
||||
console.log(val);
|
||||
}),
|
||||
)
|
||||
.subscribe({
|
||||
next: (val) => results.push(val),
|
||||
error: (err) => (error = err.message),
|
||||
});
|
||||
await new Promise((r) => setTimeout(r, 1000));
|
||||
await Promise.all([...arr.map((str) => instance.publish('test', str))]);
|
||||
await instance.throwError('test', 'TEST ERROR MESSAGE');
|
||||
await Promise.all([...arr.map((str) => instance.publish('test', str))]);
|
||||
await new Promise((r) => setTimeout(r, 1000));
|
||||
expect(results).toMatchObject(arr);
|
||||
expect(error).toEqual('TEST ERROR MESSAGE');
|
||||
});
|
||||
});
|
@ -1,115 +0,0 @@
|
||||
import debug from 'debug';
|
||||
import { EventEmitter } from 'events';
|
||||
import IORedis, { Redis } from 'ioredis';
|
||||
import { from, fromEvent, Observable } from 'rxjs';
|
||||
import { filter, map, share, switchMap, takeWhile, tap } from 'rxjs/operators';
|
||||
import { ApplicationException } from '../exceptions/application.exception';
|
||||
import { PubSubOptions } from './interfaces/pub-sub-options.interface';
|
||||
import {
|
||||
PubSubRawMessage,
|
||||
PubSubRawNextMessage,
|
||||
} from './interfaces/pub-sub-raw-message.interface';
|
||||
|
||||
const log = debug('fennec:pubsub:instance');
|
||||
export class PubSub extends EventEmitter {
|
||||
pubRedis: Redis;
|
||||
pSubRedis: Redis;
|
||||
channels: string[] = [];
|
||||
event$: Observable<[string, string, any]>;
|
||||
|
||||
constructor(private readonly options: PubSubOptions) {
|
||||
super();
|
||||
this.pubRedis = new IORedis(this.options.redis);
|
||||
this.pSubRedis = new IORedis(this.options.redis);
|
||||
|
||||
this.pSubRedis.on('pmessage', (...args) =>
|
||||
log.extend('raw')('%s %s %o', ...args),
|
||||
);
|
||||
|
||||
this.event$ = fromEvent<[string, string, string]>(
|
||||
this.pSubRedis,
|
||||
'pmessage',
|
||||
).pipe(
|
||||
map((ev) => {
|
||||
try {
|
||||
ev[2] = JSON.parse(ev[2]);
|
||||
} catch (err) {
|
||||
log('WARN: is not json');
|
||||
return null;
|
||||
}
|
||||
log('on message: %s %s %o', ...ev);
|
||||
return ev;
|
||||
}),
|
||||
filter((v) => !!v),
|
||||
share(),
|
||||
);
|
||||
}
|
||||
|
||||
async disconnect() {
|
||||
log('disconnecting to redis...');
|
||||
this.pubRedis.disconnect();
|
||||
this.pSubRedis.disconnect();
|
||||
log('disconnected');
|
||||
}
|
||||
|
||||
async registerChannel(channel: string) {
|
||||
if (this.channels.includes(channel)) {
|
||||
return;
|
||||
}
|
||||
this.channels.push(channel);
|
||||
|
||||
return await this.pSubRedis.psubscribe(channel);
|
||||
}
|
||||
|
||||
private async redisPublish<T>(channel: string, message: PubSubRawMessage<T>) {
|
||||
log.extend('publish')('channel: %s, message: %O', channel, message);
|
||||
return await this.pubRedis.publish(channel, JSON.stringify(message));
|
||||
}
|
||||
|
||||
async publish(channel: string, message: any): Promise<number> {
|
||||
return await this.redisPublish(channel, {
|
||||
type: 'next',
|
||||
message,
|
||||
});
|
||||
}
|
||||
|
||||
async finish(channel: string): Promise<number> {
|
||||
return await this.redisPublish(channel, {
|
||||
type: 'complete',
|
||||
});
|
||||
}
|
||||
|
||||
async throwError(channel: string, error: string): Promise<number> {
|
||||
return await this.redisPublish(channel, {
|
||||
type: 'error',
|
||||
error,
|
||||
});
|
||||
}
|
||||
|
||||
message$ = <T>(channel: string): Observable<T> => {
|
||||
return from(this.registerChannel(channel))
|
||||
.pipe(switchMap(() => this.event$))
|
||||
.pipe(
|
||||
filter(([pattern]) => pattern === channel),
|
||||
tap(([pattern, channel, message]) => {
|
||||
log.extend('subscribe')(
|
||||
'channel: %s, match: %s, message: %O',
|
||||
channel,
|
||||
pattern,
|
||||
message,
|
||||
);
|
||||
}),
|
||||
takeWhile(([pattern, channel, message]) => {
|
||||
if (pattern === channel) {
|
||||
if (message.type === 'error') {
|
||||
throw new ApplicationException(message.error);
|
||||
}
|
||||
return message.type !== 'complete';
|
||||
}
|
||||
return true;
|
||||
}),
|
||||
map((ev) => ev[2] as PubSubRawMessage<T>),
|
||||
map((message: PubSubRawNextMessage<T>) => message.message),
|
||||
);
|
||||
};
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
import { DEFAULT_PUB_SUB_NAME } from '../pub-sub.constants';
|
||||
|
||||
export function getPubSubToken(name) {
|
||||
return `app:pub-usb:${name || DEFAULT_PUB_SUB_NAME}`;
|
||||
}
|
||||
export function getPubSubConfigToken(name) {
|
||||
return `app:pub-usb:config:${name || DEFAULT_PUB_SUB_NAME}`;
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
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 {
|
||||
@ -10,7 +9,6 @@ export class PipelineTaskLogMessage {
|
||||
@Field(() => PipelineUnits, { nullable: true })
|
||||
unit?: PipelineUnits;
|
||||
@Field()
|
||||
@Type(() => Date)
|
||||
time: Date;
|
||||
@Field()
|
||||
message: string;
|
||||
|
@ -1,9 +1,8 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { observableToAsyncIterable } from 'graphql-tools';
|
||||
import { log } from 'console';
|
||||
import { PubSub } from 'graphql-subscriptions';
|
||||
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';
|
||||
@ -14,11 +13,9 @@ const LOG_TIMEOUT_SECONDS = 10_000;
|
||||
|
||||
@Injectable()
|
||||
export class PipelineTaskLogsService {
|
||||
constructor(
|
||||
private readonly redisService: RedisService,
|
||||
@InjectPubSub()
|
||||
private readonly pubSub: PubSub,
|
||||
) {}
|
||||
constructor(private readonly redisService: RedisService) {}
|
||||
|
||||
pubSub = new PubSub();
|
||||
|
||||
get redis() {
|
||||
return this.redisService.getClient();
|
||||
@ -76,6 +73,6 @@ export class PipelineTaskLogsService {
|
||||
}
|
||||
|
||||
watchLogs(task: PipelineTask) {
|
||||
return observableToAsyncIterable(this.pubSub.message$(this.getKeys(task)));
|
||||
return this.pubSub.asyncIterator(this.getKeys(task));
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +1,3 @@
|
||||
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,9 +8,12 @@ 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 } from './pipeline-tasks.constants';
|
||||
import {
|
||||
PIPELINE_TASK_QUEUE,
|
||||
PIPELINE_TASK_LOG_PUBSUB,
|
||||
} from './pipeline-tasks.constants';
|
||||
import { PipelineTaskLogsService } from './pipeline-task-logs.service';
|
||||
import { PubSubModule } from '../commons/pub-sub/pub-sub.module';
|
||||
import { PubSub } from 'apollo-server-express';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -18,7 +21,6 @@ import { PubSubModule } from '../commons/pub-sub/pub-sub.module';
|
||||
BullModule.registerQueue({
|
||||
name: PIPELINE_TASK_QUEUE,
|
||||
}),
|
||||
PubSubModule.forFeature(),
|
||||
RedisModule,
|
||||
ReposModule,
|
||||
],
|
||||
@ -27,6 +29,10 @@ import { PubSubModule } from '../commons/pub-sub/pub-sub.module';
|
||||
PipelineTasksResolver,
|
||||
PipelineTaskConsumer,
|
||||
PipelineTaskLogsService,
|
||||
{
|
||||
provide: Symbol(PIPELINE_TASK_LOG_PUBSUB),
|
||||
useValue: new PubSub(),
|
||||
},
|
||||
],
|
||||
exports: [PipelineTasksService],
|
||||
})
|
||||
|
@ -5,7 +5,6 @@ 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 {
|
||||
@ -21,8 +20,7 @@ export class PipelineTasksResolver {
|
||||
|
||||
@Subscription(() => PipelineTaskLogMessage, {
|
||||
resolve: (value) => {
|
||||
const data = plainToClass(PipelineTaskLogMessage, value);
|
||||
return data;
|
||||
return value;
|
||||
},
|
||||
})
|
||||
async pipelineTaskLog(@Args() args: PipelineTaskLogArgs) {
|
||||
|
@ -9,17 +9,16 @@ 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>,
|
||||
@ -28,8 +27,6 @@ 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({
|
||||
@ -121,13 +118,12 @@ export class PipelineTasksService {
|
||||
}
|
||||
|
||||
async updateTask(task: PipelineTask) {
|
||||
this.pubSub.publish(`task:${task.id}`, task);
|
||||
this.pubSub.publish(task.id, task);
|
||||
return await this.repository.save(task);
|
||||
}
|
||||
|
||||
async watchTaskUpdated(id: string) {
|
||||
log('watchTaskUpdated %s', id);
|
||||
return observableToAsyncIterable(this.pubSub.message$(`task:${id}`));
|
||||
return this.pubSub.asyncIterator(id);
|
||||
}
|
||||
|
||||
getRedisTokens(pipeline: Pipeline): [string, string] {
|
||||
|
@ -1,10 +1,12 @@
|
||||
import {
|
||||
Args,
|
||||
Info,
|
||||
Parent,
|
||||
ResolveField,
|
||||
Resolver,
|
||||
Subscription,
|
||||
} from '@nestjs/graphql';
|
||||
import { GraphQLResolveInfo } from 'graphql';
|
||||
import { PipelineTasksService } from '../pipeline-tasks/pipeline-tasks.service';
|
||||
import { LogFields, LogList } from '../repos/dtos/log-list.model';
|
||||
import { PipelinesService } from './pipelines.service';
|
||||
@ -20,7 +22,11 @@ export class CommitLogsResolver {
|
||||
return value;
|
||||
},
|
||||
})
|
||||
async listLogsForPipeline(@Args('id', { type: () => String }) id: string) {
|
||||
async listLogsForPipeline(
|
||||
@Args('id', { type: () => String }) id: string,
|
||||
@Info() info: GraphQLResolveInfo,
|
||||
) {
|
||||
info.returnType.toString();
|
||||
const job = await this.service.listLogsForPipeline(id);
|
||||
return (async function* () {
|
||||
yield await job.finished();
|
||||
|
Loading…
Reference in New Issue
Block a user