【问题标题】:Using cron with redlock and cluster in nodejs在nodejs中使用带有redlock和集群的cron
【发布时间】:2016-12-08 02:34:38
【问题描述】:

我想每分钟运行一个任务,为此我选择了 cron。现在我正在使用集群模块运行节点,它产生了 4 个进程,但我想每分钟每个进程只运行一次 cron。

现在解决这个问题的一种方法是在 master 中运行它。但是,如果我在每个实例上启动多个带有集群的实例,那将无济于事。

所以我决定寻找锁定机制。我找到了redlock,这是示例代码:

const redlock = new Redlock([redisClient], {
    driftFactor: 0.01, // time in ms
    retryCount: 2,
    retryDelay: 200 // time in ms
});

redlock.on('clientError', function (err) {
    console.error('REDLOCK REDIS ERROR: ', err);
});

const LOCK_KEY = "GOHAN_REMINDER_LOCK";
const LOCK_KEY_TTL = 10000;


const job = new CronJob({
    cronTime: '* * * * *',
    onTick: function () {
        /*
         * Runs every minute
         */
        redlock.lock(LOCK_KEY, LOCK_KEY_TTL).then(function (lock) {

            return Bluebird.try(function () {
                const currentMilitaryTime = moment.utc().format("HH:mm");

                return ChildReminder.getRemindersByDate(currentMilitaryTime).then(function (reminders) {
                    console.log("REMINDERS FOR TIME " + currentMilitaryTime + " :", reminders);
                });
            }).then(function () {
                return lock.unlock()
                    .catch(function (err) {
                        console.error("REDLOCK UNLOCK ERROR: ", err);
                    });
            });
        });

    },
    start: false,
    timeZone: 'UTC'
});

job.start();

我得到的日志:

REMINDERS FOR TIME 06:08 : [ anonymous {
    user_id: '764418c2-a874-495c-b8c1-cc555ce9b202',
    child_id: '8c8a273f-3a84-4a66-9fa7-bb9e6c5fcf0a',
    reminder_id: 4,
    label: 'textvoice1',
    mode: 'textvoice',
    text_message: 'How you doin\'',
    voice_message: '1480399630',
    created_at: 2016-11-29T06:07:12.296Z,
    updated_at: 2016-11-29T06:07:12.296Z,
    time: '06:08',
    repeat: '1111111',
    on: true },
  anonymous {
    user_id: '764418c2-a874-495c-b8c1-cc555ce9b202',
    child_id: '8c8a273f-3a84-4a66-9fa7-bb9e6c5fcf0a',
    reminder_id: 5,
    label: 'textvoice1',
    mode: 'textvoice',
    text_message: 'How you doin\'',
    voice_message: '1480399638',
    created_at: 2016-11-29T06:07:19.385Z,
    updated_at: 2016-11-29T06:07:19.385Z,
    time: '06:08',
    repeat: '1111111',
    on: true } ]
REMINDERS FOR TIME 06:08 : [ anonymous {
    user_id: '764418c2-a874-495c-b8c1-cc555ce9b202',
    child_id: '8c8a273f-3a84-4a66-9fa7-bb9e6c5fcf0a',
    reminder_id: 4,
    label: 'textvoice1',
    mode: 'textvoice',
    text_message: 'How you doin\'',
    voice_message: '1480399630',
    created_at: 2016-11-29T06:07:12.296Z,
    updated_at: 2016-11-29T06:07:12.296Z,
    time: '06:08',
    repeat: '1111111',
    on: true },
  anonymous {
    user_id: '764418c2-a874-495c-b8c1-cc555ce9b202',
    child_id: '8c8a273f-3a84-4a66-9fa7-bb9e6c5fcf0a',
    reminder_id: 5,
    label: 'textvoice1',
    mode: 'textvoice',
    text_message: 'How you doin\'',
    voice_message: '1480399638',
    created_at: 2016-11-29T06:07:19.385Z,
    updated_at: 2016-11-29T06:07:19.385Z,
    time: '06:08',
    repeat: '1111111',
    on: true } ]
REMINDERS FOR TIME 06:08 : [ anonymous {
    user_id: '764418c2-a874-495c-b8c1-cc555ce9b202',
    child_id: '8c8a273f-3a84-4a66-9fa7-bb9e6c5fcf0a',
    reminder_id: 4,
    label: 'textvoice1',
    mode: 'textvoice',
    text_message: 'How you doin\'',
    voice_message: '1480399630',
    created_at: 2016-11-29T06:07:12.296Z,
    updated_at: 2016-11-29T06:07:12.296Z,
    time: '06:08',
    repeat: '1111111',
    on: true },
  anonymous {
    user_id: '764418c2-a874-495c-b8c1-cc555ce9b202',
    child_id: '8c8a273f-3a84-4a66-9fa7-bb9e6c5fcf0a',
    reminder_id: 5,
    label: 'textvoice1',
    mode: 'textvoice',
    text_message: 'How you doin\'',
    voice_message: '1480399638',
    created_at: 2016-11-29T06:07:19.385Z,
    updated_at: 2016-11-29T06:07:19.385Z,
    time: '06:08',
    repeat: '1111111',
    on: true } ]
REMINDERS FOR TIME 06:08 : [ anonymous {
    user_id: '764418c2-a874-495c-b8c1-cc555ce9b202',
    child_id: '8c8a273f-3a84-4a66-9fa7-bb9e6c5fcf0a',
    reminder_id: 4,
    label: 'textvoice1',
    mode: 'textvoice',
    text_message: 'How you doin\'',
    voice_message: '1480399630',
    created_at: 2016-11-29T06:07:12.296Z,
    updated_at: 2016-11-29T06:07:12.296Z,
    time: '06:08',
    repeat: '1111111',
    on: true },
  anonymous {
    user_id: '764418c2-a874-495c-b8c1-cc555ce9b202',
    child_id: '8c8a273f-3a84-4a66-9fa7-bb9e6c5fcf0a',
    reminder_id: 5,
    label: 'textvoice1',
    mode: 'textvoice',
    text_message: 'How you doin\'',
    voice_message: '1480399638',
    created_at: 2016-11-29T06:07:19.385Z,
    updated_at: 2016-11-29T06:07:19.385Z,
    time: '06:08',
    repeat: '1111111',
    on: true } ]
REMINDERS FOR TIME 06:08 : [ anonymous {
    user_id: '764418c2-a874-495c-b8c1-cc555ce9b202',
    child_id: '8c8a273f-3a84-4a66-9fa7-bb9e6c5fcf0a',
    reminder_id: 4,
    label: 'textvoice1',
    mode: 'textvoice',
    text_message: 'How you doin\'',
    voice_message: '1480399630',
    created_at: 2016-11-29T06:07:12.296Z,
    updated_at: 2016-11-29T06:07:12.296Z,
    time: '06:08',
    repeat: '1111111',
    on: true },
  anonymous {
    user_id: '764418c2-a874-495c-b8c1-cc555ce9b202',
    child_id: '8c8a273f-3a84-4a66-9fa7-bb9e6c5fcf0a',
    reminder_id: 5,
    label: 'textvoice1',
    mode: 'textvoice',
    text_message: 'How you doin\'',
    voice_message: '1480399638',
    created_at: 2016-11-29T06:07:19.385Z,
    updated_at: 2016-11-29T06:07:19.385Z,
    time: '06:08',
    repeat: '1111111',
    on: true } ]

基本上,该作业会执行 4-5 次,这是我不想要的。任何帮助,将不胜感激。谢谢。

编辑 #1:我尝试将 retryCount 更改为 0 并增加/减少 TTL,但没有得到我想要的结果。

包链接:

  1. Redlock
  2. Cron

【问题讨论】:

    标签: node.js cron node-cron


    【解决方案1】:

    似乎如果一个实例在第二个实例尝试获取它之前获得了锁并释放它,那么两者都会运行。你需要一些更强大的东西。

    我刚刚为node写了一个小模块cronivo,它使用了latestJs和redis,应该对你有用,至少它可以给你一些想法。

    【讨论】:

    • 我找到了另一种方法。我不是手动释放锁,而是取决于锁的 TTL。这样,到目前为止,我还没有遇到任何问题。你能告诉我这种方式是否有任何问题吗?
    • 如果一个实例很忙并且在 TTL 之后结束尝试执行,即使它不应该执行它也会执行。如果您的 TTL 足够大,这不太可能。我不认为这种方法很好,但它可以工作。
    • 我不介意启动另一个进程,但我不希望工作完成两次或三次……我的 ttl 是 60 秒……我想每分钟检查一次提醒需要什么被发送。因此,如果在 06:11 进程收到 06:11 的提醒,那么在 06:12 我想要 06:12 的提醒。我不想要的是 4 个进程在 06:11 收到提醒,因为那样它们将被发送 4 次。
    猜你喜欢
    • 2020-10-17
    • 2021-01-10
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多