Home  >  Article  >  Backend Development  >  Run hourly scheduler using gocron

Run hourly scheduler using gocron

王林
王林forward
2024-02-10 15:20:21808browse

使用 gocron 运行每小时调度程序

php Xiaobian Apple introduces you to gocron, which is a powerful scheduler that allows you to run tasks every hour. It is a simple and easy-to-use tool that can implement scheduled task scheduling without writing cumbersome code. Using gocron, you can easily set up tasks to be executed every hour. Whether it is data backup, log cleaning or other important scheduled tasks, they can be executed accurately and reliably. gocron also provides a friendly graphical interface, allowing you to easily manage and monitor your scheduled tasks. Whether you are an individual developer or an enterprise administrator, gocron is an indispensable tool for you.

Question content

How to use gocron to run a function at specific minutes every hour?

I tried two codes, one is dirty and the other doesn't work.

Dirty code:

loc, _ := time.loadlocation("asia/seoul")
    s := gocron.newscheduler(loc)

    _, err = s.every(1).day().at("0:30").do(schduler)
    _, err = s.every(1).day().at("1:30").do(schduler)
    _, err = s.every(1).day().at("2:30").do(schduler)
    _, err = s.every(1).day().at("3:30").do(schduler)
    _, err = s.every(1).day().at("4:30").do(schduler)
    _, err = s.every(1).day().at("5:30").do(schduler)
    _, err = s.every(1).day().at("6:30").do(schduler)
    _, err = s.every(1).day().at("7:30").do(schduler)
    _, err = s.every(1).day().at("8:30").do(schduler)
    _, err = s.every(1).day().at("9:30").do(schduler)
    _, err = s.every(1).day().at("10:30").do(schduler)
    _, err = s.every(1).day().at("11:30").do(schduler)
    _, err = s.every(1).day().at("12:30").do(schduler)
    _, err = s.every(1).day().at("13:30").do(schduler)
    _, err = s.every(1).day().at("14:30").do(schduler)
    _, err = s.every(1).day().at("15:30").do(schduler)
    _, err = s.every(1).day().at("16:30").do(schduler)
    _, err = s.every(1).day().at("17:30").do(schduler)
    _, err = s.every(1).day().at("18:30").do(schduler)
    _, err = s.every(1).day().at("19:30").do(schduler)
    _, err = s.every(1).day().at("20:30").do(schduler)
    _, err = s.every(1).day().at("21:30").do(schduler)
    _, err = s.every(1).day().at("22:30").do(schduler)
    _, err = s.every(1).day().at("23:30").do(schduler)
    if err != nil {
        fmt.println("error scheduling task:", err)
        return
    }
    s.startasync()

Code that doesn’t work:

_, err = s.Every(1).Hour().Minute(30).Do(runHourlyScheduler)

Workaround

Use .every(1).hour() and then chain it with .startat() and use Any time.time object that sets the minutes to 30 populates the argument.

This way, the scheduler will run for a few minutes every hour 30.

loc, _ := time.LoadLocation("Asia/Seoul")
s := gocron.NewScheduler(loc)

// construct start at in minute 30
now := time.Now()
nextSchedule := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 30, 0, 0, now.Location())

_, err := s.Every(1).Hour().StartAt(nextSchedule).Do(schduler)
if err != nil {
    fmt.Println("Error scheduling task:", err)
    return
}

s.StartAsync()

The above is the detailed content of Run hourly scheduler using gocron. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete