Distributed scheduling using redis ?
iangregsondev opened this issue · comments
Ian Gregson commented
Hi,
Great module, loving it. I saw that you have a distributed lock class, i was wondering if you had planned some kind of integration with redis so that only 1 node would run the scheduled job or something similar ?
Thanks in advance
Miaow commented
Thanks for using Nest Schedule, it not provides redis lock now and you can use a external module, such as redis-lock:
import { createClient } from 'redis';
import * as RedisLock from 'redis-lock';
const redis = createClient();
const lock = RedisLock(redis);
@Injectable()
export class DistributedScheduleService extends NestDistributedSchedule {
constructor() {
super();
}
async tryLock(method: string): Promise<TryRelease> {
console.log('try apply lock: ', method);
const done: Function = await lock('lock_' + method);
console.log('applied lock: ', method);
return () => {
console.log('try release lock: ', method);
done(() => console.log('released lock: ', method));
};
}
}
Ian Gregson commented
Ah, thats nice to know. Thank you.