jasonlvhit / gocron

A Golang Job Scheduling Package.
BSD 2-Clause "Simplified" License
3.47k stars 345 forks source link

Lock function, still run multiple times on multiple server instances #160

Open g-graziano opened 4 years ago

g-graziano commented 4 years ago

i used lock implementation on redis, but it still run cron multiple times on multiple instances, i have 4 instances and it still run 4 times

locker := p.schedulerLockerRepo
gocron.SetLocker(locker)
go p.paymentDailyReminderWorker()

func paymentDailyReminderWorker() {
    reminderScheduler := gocron.NewScheduler()
    if err := reminderScheduler.Every(1).Day().At(c.config.DailyReminderClock).Lock().Do(c.processPaymentReminder); err == nil {
        c.logger.Info().Str("reminder_at", c.config.DailyReminderClock).Msg("payment daily reminder worker started")
        <-reminderScheduler.Start()
    }
}
arjunmahishi commented 4 years ago

Hey @g-graziano, This project is now being maintained in this repo. You might want to check that out and see if this issue is still occurring. If it is, we can track this issue there.

rtywei commented 3 years ago

This problem does exist, and the result of lock acquisition is not judged in the implementation.

func (j *Job) run() ([]reflect.Value, error) {
    if j.lock {
        if locker == nil {
            return nil, fmt.Errorf("trying to lock %s with nil locker", j.jobFunc)
        }
        key := getFunctionKey(j.jobFunc)
                 **// This result is not judged`**
        locker.Lock(key)
                **// This result is not judged`**
        defer locker.Unlock(key)
    }
    result, err := callJobFuncWithParams(j.funcs[j.jobFunc], j.fparams[j.jobFunc])
    if err != nil {
        return nil, err
    }
    return result, nil
}
JohnRoesler commented 3 years ago

hi @rtywei as @arjunmahishi mentioned - this repo is no longer maintained. We are maintaining it over at https://github.com/go-co-op/gocron but we have since removed the locker mechanism as it was not working and we have yet to come up with a good implementation.