miaowing / nest-schedule

A cron-like and not-cron-like job distributed scheduler for Nest.js by decorators.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Distributed scheduling using redis ?

iangregsondev opened this issue · comments

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

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));
    };
  }
}

Ah, thats nice to know. Thank you.