Skip to content

Commit

Permalink
fix(redis): use version for naming loaded lua scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Oct 18, 2024
1 parent 9307fef commit fe73f6d
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 60 deletions.
11 changes: 7 additions & 4 deletions src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,11 +710,13 @@ export class Job<
token,
delay,
);
(<any>multi).moveToDelayed(args);
this.scripts.execCommand(multi, 'moveToDelayed', args);
command = 'moveToDelayed';
} else {
// Retry immediately
(<any>multi).retryJob(
this.scripts.execCommand(
multi,
'retryJob',
this.scripts.retryJobArgs(this.id, this.opts.lifo, token),
);
command = 'retryJob';
Expand All @@ -732,7 +734,8 @@ export class Job<
token,
fetchNext,
);
(<any>multi).moveToFinished(args);

this.scripts.execCommand(multi, 'moveToFinished', args);
finishedOn = args[this.scripts.moveToFinishedKeys.length + 1] as number;
command = 'moveToFinished';
}
Expand Down Expand Up @@ -1287,7 +1290,7 @@ export class Job<
err?.message,
);

(<any>multi).saveStacktrace(args);
this.scripts.execCommand(multi, 'saveStacktrace', args);
}
}

Expand Down
15 changes: 11 additions & 4 deletions src/classes/redis-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isRedisCluster,
isRedisInstance,
isRedisVersionLowerThan,
readPackageJson,
} from '../utils';
import * as scripts from '../scripts';

Expand Down Expand Up @@ -195,13 +196,17 @@ export class RedisConnection extends EventEmitter {
return this.initializing;
}

protected loadCommands(providedScripts?: Record<string, RawCommand>): void {
protected loadCommands(
version: string,
providedScripts?: Record<string, RawCommand>,
): void {
const finalScripts =
providedScripts || (scripts as Record<string, RawCommand>);
for (const property in finalScripts as Record<string, RawCommand>) {
// Only define the command if not already defined
if (!(<any>this._client)[finalScripts[property].name]) {
(<any>this._client).defineCommand(finalScripts[property].name, {
const commandName = `${finalScripts[property].name}:${version}`;
if (!(<any>this._client)[commandName]) {
(<any>this._client).defineCommand(commandName, {
numberOfKeys: finalScripts[property].keys,
lua: finalScripts[property].content,
});
Expand All @@ -223,9 +228,11 @@ export class RedisConnection extends EventEmitter {

this._client.on('ready', this.handleClientReady);

const { version } = readPackageJson();

await RedisConnection.waitUntilReady(this._client);

this.loadCommands();
this.loadCommands(version);

if (this._client['status'] !== 'end') {
this.version = await this.getRedisVersion();
Expand Down
Loading

0 comments on commit fe73f6d

Please sign in to comment.