mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
* support redis cluster * cleanup, fix config schema * set default prefix to bull * initial commit * improve logging * improve types and refactor * list support and refactor * fix redis service and tests * add comment * add redis and cache prefix * use injection * lint fix * clean schema comments * improve naming, tests, cluster client * merge master * cache returns unknown instead of T * update cache service, tests and doc * remove console.log * do not cache null or undefined values * fix merge * lint fix
58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import { Service } from 'typedi';
|
|
import { WORKER_RESPONSE_REDIS_LIST } from './RedisServiceHelper';
|
|
import type { RedisServiceWorkerResponseObject } from './RedisServiceCommands';
|
|
import { RedisServiceBaseReceiver } from './RedisServiceBaseClasses';
|
|
import { LoggerProxy, jsonParse } from 'n8n-workflow';
|
|
|
|
@Service()
|
|
export class RedisServiceListReceiver extends RedisServiceBaseReceiver {
|
|
async init(): Promise<void> {
|
|
await super.init('list-receiver');
|
|
}
|
|
|
|
async popFromHead(list: string): Promise<string | null | undefined> {
|
|
if (!this.redisClient) {
|
|
await this.init();
|
|
}
|
|
return this.redisClient?.lpop(list);
|
|
}
|
|
|
|
async popFromTail(list: string): Promise<string | null | undefined> {
|
|
if (!this.redisClient) {
|
|
await this.init();
|
|
}
|
|
return this.redisClient?.rpop(list);
|
|
}
|
|
|
|
private poppedResultToWorkerResponse(
|
|
poppedResult: string | null | undefined,
|
|
list: string = WORKER_RESPONSE_REDIS_LIST,
|
|
): RedisServiceWorkerResponseObject | null {
|
|
if (poppedResult) {
|
|
try {
|
|
const workerResponse = jsonParse<RedisServiceWorkerResponseObject>(poppedResult);
|
|
if (workerResponse) {
|
|
// TODO: Handle worker response
|
|
console.log('Received worker response', workerResponse);
|
|
}
|
|
return workerResponse;
|
|
} catch (error) {
|
|
LoggerProxy.warn(
|
|
`Error parsing worker response on list ${list}: ${(error as Error).message}`,
|
|
);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
async popOldestWorkerResponse(): Promise<RedisServiceWorkerResponseObject | null> {
|
|
const poppedResult = await this.popFromTail(WORKER_RESPONSE_REDIS_LIST);
|
|
return this.poppedResultToWorkerResponse(poppedResult);
|
|
}
|
|
|
|
async popLatestWorkerResponse(): Promise<RedisServiceWorkerResponseObject | null> {
|
|
const poppedResult = await this.popFromHead(WORKER_RESPONSE_REDIS_LIST);
|
|
return this.poppedResultToWorkerResponse(poppedResult);
|
|
}
|
|
}
|