运行gocron的每小时调度器

huangapple go评论62阅读模式
英文:

run Hourly Scheduler using gocron

问题

如何使用gocron每小时的特定分钟运行函数?

我尝试了两种代码,一种非常混乱,一种不起作用。

混乱的代码:

	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("23:30").Do(schduler)
	if err != nil {
		fmt.Println("Error scheduling task:", err)
		return
	}
	s.StartAsync()

不起作用的代码:

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

以上是你要翻译的内容。

英文:

How to run function hourly specific minute using gocron?

i tried two code one is very dirty and one is not working.

the 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()

not working code:

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

答案1

得分: 1

使用.Every(1).Hour(),然后与.StartAt()链接,并将参数填充为任何time.Time对象,其中分钟设置为30

这样,调度程序将每小时运行30分钟。

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

// 构建在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()
英文:

Use .Every(1).Hour() and then chain it with .StartAt() and fill the argument with any time.Time object that has minutes set to 30.

With that, the scheduler will run for minutes 30 every hour.

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()

答案2

得分: 0

要在Go编程语言中使用gocron运行每小时调度程序,您可以使用以下代码:

package main

import (
    "fmt"
    "github.com/go-co-op/gocron"
)

func task() {
    fmt.Println("Running task...")
}

func main() {
    // 创建一个新的调度程序
    scheduler := gocron.NewScheduler()

    // 安排任务每小时运行
    _, err := scheduler.Every(1).Hour().Do(task)
    if err != nil {
        fmt.Println("Error scheduling task:", err)
        return
    }

    // 启动调度程序
    scheduler.Start()

    // 等待调度程序完成
    <-scheduler.Done()
}

在上面的代码中,我们导入gocron包并定义一个任务函数,该函数将由调度程序执行。我们创建一个新的调度程序,并使用Every(1).Hour().Do(task)方法安排任务每小时运行。然后,我们使用scheduler.Start()启动调度程序,并使用<-scheduler.Done()等待其完成。如果在安排任务时出现任何错误,我们将打印错误消息并退出。

您可以使用gocron提供的其他方法自定义计划,例如使用Every(2).Hours()每两小时运行任务,或使用At("10:00").Do(task)在特定时间运行任务。

英文:

To run an hourly scheduler using gocron in Go programming language, you can use the following code:

package main

import (
    &quot;fmt&quot;
    &quot;github.com/go-co-op/gocron&quot;
)

func task() {
    fmt.Println(&quot;Running task...&quot;)
}

func main() {
    // Create a new scheduler
    scheduler := gocron.NewScheduler()

    // Schedule task to run every hour
    _, err := scheduler.Every(1).Hour().Do(task)
    if err != nil {
        fmt.Println(&quot;Error scheduling task:&quot;, err)
        return
    }

    // Start the scheduler
    scheduler.Start()

    // Wait for the scheduler to finish
    &lt;-scheduler.Done()
}

In the code above, we import the gocron package and define a task function that will be executed by the scheduler. We create a new scheduler and schedule the task to run every hour using the Every(1).Hour().Do(task) method. We then start the scheduler using scheduler.Start() and wait for it to finish using <-scheduler.Done(). If there are any errors while scheduling the task, we print an error message and exit.

You can customize the schedule as needed by using other methods provided by gocron, such as Every(2).Hours() to run the task every two hours or At("10:00").Do(task) to run the task at a specific time of day.

huangapple
  • 本文由 发表于 2023年3月29日 15:01:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75874079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定