robfig / cron

a cron library for go
MIT License
13.1k stars 1.62k forks source link

not working #390

Closed zoujunrong closed 3 years ago

zoujunrong commented 3 years ago

// "github.com/robfig/cron/v3"

func main() {
  c := cron.New()
  c.AddFunc("* * * * * *", func() {
    fmt.Println("working")
  })
  c.Start()

  select {}
}

use the latest version v3.0.1 and 3.0.0 is also not working, my useage was wrong?

AdmiralDollBug commented 3 years ago

It also happens on me. All of the jobs configured in format of cron expression does not work. cron version: v3.0.1 go version: 1.15.2

fujiawei-dev commented 3 years ago

Me too...

Alanthur commented 3 years ago

The example was wrong. You should take 5 fields for AddFunc funtion. correct: c.AddFunc("30 3 * * *", func() { fmt.Println("working") }) wrong: c.AddFunc("0 30 * * * *", func() { fmt.Println("working") })

AdmiralDollBug commented 3 years ago

I read the code again, and found that the cron.New function without parameter creates a "standardParser", which does not support the "Second" field

var standardParser = NewParser(
    Minute | Hour | Dom | Month | Dow | Descriptor,
)

...

func New(opts ...Option) *Cron {
    c := &Cron{
        entries:   nil,
        chain:     NewChain(),
        add:       make(chan *Entry),
        stop:      make(chan struct{}),
        snapshot:  make(chan chan []Entry),
        remove:    make(chan EntryID),
        running:   false,
        runningMu: sync.Mutex{},
        logger:    DefaultLogger,
        location:  time.Local,
        parser:    standardParser,
    }
    for _, opt := range opts {
        opt(c)
    }
    return c
}

To support "Second" field, create the cron object like this:

cron.New(cron.WithSeconds())

BTW,the doc on go.pkg.dev needs to be updated because it's misleading now...

zoujunrong commented 3 years ago

The example was wrong. You should take 5 fields for AddFunc funtion. correct: c.AddFunc("30 3 * * *", func() { fmt.Println("working") }) wrong: c.AddFunc("0 30 * * * *", func() { fmt.Println("working") })

c.AddFunc(" ", func() { fmt.Println("working") })
also not working.

danielMensah commented 3 years ago

Has this issue been solve? having issues just running c.AddFunc("* * * * *", func() { fmt.Println("working") })

grosenberry commented 2 years ago

Has this issue been solve? having issues just running c.AddFunc("* * * * *", func() { fmt.Println("working") })

I was not able to get this line to work successfully, the only way I've found to get this to work is to use the above suggestion cron.New(cron.WithSeconds()) with the 6 field notation and it began working perfectly.

Hopefully this helps anyone landing on the page. I'm on v3.0.1.

zoujunrong commented 2 years ago

这是来自QQ邮箱的假期自动回复邮件。   我已收到您的邮件,尽快给您回复,谢谢!