Cron is running twice every tick for the below code snippet. I want it to run once for every tick
func CronServ(ctx context.Context, config config.Config, ns *noti.NotificationService) { log.Ctx(ctx).Info("Starting cron") c := cron.New() if config.Notification.Enabled { if _, err := c.AddFunc(config.Notification.CronExpression, func() { e := Notify(ns) if e != nil { log.Ctx(ctx).Errorf("Notification failed: %v", e) } }); err != nil { log.Ctx(ctx).Errorf("Failed to add cron job: %v", err) } } c.Start() // Wait for context cancellation to gracefully stop the cron service <-ctx.Done() log.Ctx(ctx).Info("Shutting down cron") c.Stop() }
My cron expression is * * * * *
I am not able to find out why is it triggering Notify method twice every minute? Do I need to change the cron expression to */1 * * * *?
If I add the expression as 0 * * * * * it is failed to parse the expression and getting the below error
expected exactly 5 fields, found 6: [0 * * * * *]
func TestExample(t *testing.T) {
c := New()
c.AddFunc("* * * * *", func() {
t.Logf("hit at %v", time.Now())
})
c.Start()
defer c.Stop()
time.Sleep(time.Minute * 3)
}
produce log as expect:
=== RUN TestExample
cron_test.go:721: hit at 2025-10-24 20:29:00.0005735 +0800 CST m=+54.421884401
cron_test.go:721: hit at 2025-10-24 20:30:00.0004796 +0800 CST m=+114.421790501
cron_test.go:721: hit at 2025-10-24 20:31:00.0006541 +0800 CST m=+174.421965001
--- PASS: TestExample (180.00s)
PASS