Run hourly scheduler using gocron

王林
Release: 2024-02-10 15:20:21
forward
845 people have browsed it

使用 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()
Copy after login

Code that doesn’t work:

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

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()
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template