|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
import { Injectable } from '@nestjs/common'; |
|
|
|
|
import Redis from 'ioredis'; |
|
|
|
|
import { InstanceTypes } from '~/interface/Jobs'; |
|
|
|
|
|
|
|
|
|
@Injectable() |
|
|
|
|
export class JobsRedisService { |
|
|
|
@ -7,25 +8,25 @@ export class JobsRedisService {
|
|
|
|
|
private redisSubscriber: Redis; |
|
|
|
|
private unsubscribeCallbacks: { [key: string]: () => void } = {}; |
|
|
|
|
|
|
|
|
|
public primaryCallbacks: { [key: string]: () => void } = {}; |
|
|
|
|
public workerCallbacks: { [key: string]: () => void } = {}; |
|
|
|
|
public instanceCallbacks: { [key: string]: () => void } = {}; |
|
|
|
|
|
|
|
|
|
constructor() { |
|
|
|
|
this.redisClient = new Redis(process.env.NC_REDIS_JOB_URL); |
|
|
|
|
this.redisSubscriber = new Redis(process.env.NC_REDIS_JOB_URL); |
|
|
|
|
|
|
|
|
|
if (process.env.NC_WORKER_CONTAINER === 'true') { |
|
|
|
|
this.redisSubscriber.subscribe('workers'); |
|
|
|
|
this.redisSubscriber.subscribe(InstanceTypes.WORKER); |
|
|
|
|
} else { |
|
|
|
|
this.redisSubscriber.subscribe('instances'); |
|
|
|
|
this.redisSubscriber.subscribe(InstanceTypes.PRIMARY); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const onMessage = (channel, message) => { |
|
|
|
|
console.log('onMessage', channel, message); |
|
|
|
|
if (channel === 'workers') { |
|
|
|
|
if (channel === InstanceTypes.WORKER) { |
|
|
|
|
this.workerCallbacks[message] && this.workerCallbacks[message](); |
|
|
|
|
} else if (channel === 'instances') { |
|
|
|
|
this.instanceCallbacks[message] && this.instanceCallbacks[message](); |
|
|
|
|
} else if (channel === InstanceTypes.PRIMARY) { |
|
|
|
|
this.primaryCallbacks[message] && this.primaryCallbacks[message](); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -71,7 +72,7 @@ export class JobsRedisService {
|
|
|
|
|
workerCount(): Promise<number> { |
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
this.redisClient.publish( |
|
|
|
|
'workers', |
|
|
|
|
InstanceTypes.WORKER, |
|
|
|
|
'count', |
|
|
|
|
(error, numberOfSubscribers) => { |
|
|
|
|
if (error) { |
|
|
|
|