|
|
|
@ -3,7 +3,7 @@ import type { InstanceCommands } from '~/interface/Jobs';
|
|
|
|
|
import { PubSubRedis } from '~/redis/pubsub-redis'; |
|
|
|
|
import { InstanceTypes } from '~/interface/Jobs'; |
|
|
|
|
|
|
|
|
|
export class JobsRedis extends PubSubRedis { |
|
|
|
|
export class JobsRedis { |
|
|
|
|
protected static logger = new Logger(JobsRedis.name); |
|
|
|
|
|
|
|
|
|
public static primaryCallbacks: { |
|
|
|
@ -13,24 +13,38 @@ export class JobsRedis extends PubSubRedis {
|
|
|
|
|
{}; |
|
|
|
|
|
|
|
|
|
static async initJobs() { |
|
|
|
|
if (!this.initialized) { |
|
|
|
|
if (!this.available) { |
|
|
|
|
if (!PubSubRedis.initialized) { |
|
|
|
|
if (!PubSubRedis.available) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
await this.init(); |
|
|
|
|
await PubSubRedis.init(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const onMessage = async (channel, message) => { |
|
|
|
|
const args = message.split(':'); |
|
|
|
|
const command = args.shift(); |
|
|
|
|
if (channel === InstanceTypes.WORKER) { |
|
|
|
|
this.workerCallbacks[command] && |
|
|
|
|
(await this.workerCallbacks[command](...args)); |
|
|
|
|
} else if (channel === InstanceTypes.PRIMARY) { |
|
|
|
|
this.primaryCallbacks[command] && |
|
|
|
|
(await this.primaryCallbacks[command](...args)); |
|
|
|
|
try { |
|
|
|
|
if (!message) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
const args = message.split(':'); |
|
|
|
|
const command = args.shift(); |
|
|
|
|
|
|
|
|
|
if (channel === InstanceTypes.WORKER) { |
|
|
|
|
this.workerCallbacks[command] && |
|
|
|
|
(await this.workerCallbacks[command](...args)); |
|
|
|
|
} else if (channel === InstanceTypes.PRIMARY) { |
|
|
|
|
this.primaryCallbacks[command] && |
|
|
|
|
(await this.primaryCallbacks[command](...args)); |
|
|
|
|
} |
|
|
|
|
} catch (error) { |
|
|
|
|
this.logger.error({ |
|
|
|
|
message: `Error processing redis pub-sub message ${message}`, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
PubSubRedis.redisSubscriber.on('message', onMessage); |
|
|
|
|
|
|
|
|
|
if (process.env.NC_WORKER_CONTAINER === 'true') { |
|
|
|
|
await this.subscribe(InstanceTypes.WORKER, async (message) => { |
|
|
|
|
await onMessage(InstanceTypes.WORKER, message); |
|
|
|
|