0

I want to use the go-micro v4 framework to write a scheduled task. But I can't find a suitable example in the official document. Does anyone provide me with an example for my reference?

FamousMai
  • 1
  • 1

1 Answers1

0

I've also been searching for the answer to this within a go-micro v4 framework. Turns out I was looking in the wrong place. In the service invocation, I simply added a new chrono task (https://itnext.io/how-to-schedule-tasks-using-chrono-in-golang-16a39b14a79a) before the service is started.

    // Set up timed collection of rates
    taskScheduler := chrono.NewDefaultTaskScheduler()

    _, err := taskScheduler.ScheduleAtFixedRate(func(ctx context.Context) {
      if err := collection.GetLatestRates(); err != nil {
        fmt.Print("Rates collection task has returned an error")
      }
      
    }, 600 * time.Second)

    if err == nil {
      fmt.Print("Task has been scheduled successfully.")
    }
BillyBob
  • 1
  • 2