Home > Enterprise >  scheduler fetch data every midnight
scheduler fetch data every midnight

Time:12-21

how to add a scheduler with a specific time so that the application can synchronize data at a specified time

addCron, _ := time.LoadLocation("Asia/Jakarta")
scheduler := cron.New(cron.WithLocation(addCron))

defer scheduler.Stop()

scheduler.AddFunc("10 00 * * 1-6", ExecuteRoutine)

// start scheduler
go scheduler.Start()
done := make(chan bool)

// trigger shutdown
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
<-sig
done <- true

function and endpoint that will collect automatically at the midnight

httpRouter.Post("/master/upsertIntoServices", services.UpsertIntoServices)

how to add that endpoint into a function ExecuteRoutine, so it can do a cron job every midnight

CodePudding user response:

This is a simple example, to scheduler jobs at midnight from your code

addCron, _ := time.LoadLocation("Asia/Jakarta")
scheduler := cron.New(cron.WithLocation(addCron))

defer scheduler.Stop()

scheduler.AddFunc("@midnight", ExecuteRoutine)

// start scheduler
scheduler.Start()

You can discover, more information here in the doc

Only a few reminders:

  • You don't need to run a goroutine when will call the command start.
  • you do need to stop your scheduler at some point in your logic
  •  Tags:  
  • go
  • Related