GOLANG中的CRON JOB

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

CRON JOB in GOLANG

问题

我正在使用Cron包https://github.com/jasonlvhit/gocron/blob/master/gocron.go

import (
	"fmt"
	"time"

	"github.com/claudiu/gocron"
)

func task() {
	fmt.Println("我正在运行任务。", time.Now())
}
func vijay() {
	fmt.Println("我正在运行vijay。", time.Now())
}

func main() {
	go test()

	gocron.Start()
	s := gocron.NewScheduler()
	gocron.Every(5).Seconds().Do(task)
	gocron.Every(10).Seconds().Do(vijay)

	<-s.Start()

}
func test() {
	time.Sleep(20 * time.Second)
	gocron.Clear()
	fmt.Println("所有任务已移除")
}

我的问题是在移除所有任务后,我的程序仍然在执行。

我想在移除所有任务后中断执行。

请帮助我,我找不到如何做到这一点的方法,我尝试过更改PKG源代码,但找不到方法。

谢谢大家。

英文:

I AM USING IN CRON PKG https://github.com/jasonlvhit/gocron/blob/master/gocron.go

import (
	&quot;fmt&quot;
	&quot;time&quot;

	&quot;github.com/claudiu/gocron&quot;
)

func task() {
	fmt.Println(&quot;I am runnning task.&quot;, time.Now())
}
func vijay() {
	fmt.Println(&quot;I am runnning vijay.&quot;, time.Now())
}

func main() {
	go test()

	gocron.Start()
	s := gocron.NewScheduler()
	gocron.Every(5).Seconds().Do(task)
	gocron.Every(10).Seconds().Do(vijay)

	&lt;-s.Start()

}
func test() {
	time.Sleep(20 * time.Second)
	gocron.Clear()
	fmt.Println(&quot;All task removed&quot;)
}

My problem is after removing all job, my program is still executing

i want to break the exection after removing all jobs

please help me out ,i am not able to find out how to do it ,
i tried to change the PKG source code also but not able to find out the way to do it

thank you all

答案1

得分: 8

首先,你正在创建一个新的调度器,并等待它,但是在运行作业时使用默认调度器。

接下来,你正在阻塞在Start()方法返回的通道上。关闭该通道以解除接收操作的阻塞。如果你不立即从main函数退出,这也将退出cron程序的主循环。

func main() {
    ch := gocron.Start()
    go test(ch)
    gocron.Every(5).Seconds().Do(task)
    gocron.Every(10).Seconds().Do(vijay)

    <-ch
}
func test(stop chan bool) {
    time.Sleep(20 * time.Second)
    gocron.Clear()
    fmt.Println("All task removed")
    close(stop)
}

实际上,这与以下代码是相同的:

func main() {
    gocron.Start()
    gocron.Every(5).Seconds().Do(task)
    gocron.Every(10).Seconds().Do(vijay)

    time.Sleep(20 * time.Second)

    gocron.Clear()
    fmt.Println("All task removed")
}

如果你立即退出,调用Clear()然后停止调度器并不重要,你可以直接退出程序。

英文:

First, you're creating a new scheduler, and waiting on it, but using the default scheduler to run your jobs.

Next, you're blocking on the channel returned by the Start() method. Close that channel to unblock the receive operation. This will also exit the main loop in the cron program if you aren't immediately exiting from main.

func main() {
    ch := gocron.Start()
    go test(ch)
    gocron.Every(5).Seconds().Do(task)
    gocron.Every(10).Seconds().Do(vijay)

    &lt;-ch
}
func test(stop chan bool) {
    time.Sleep(20 * time.Second)
    gocron.Clear()
    fmt.Println(&quot;All task removed&quot;)
    close(stop)
}

which effectively is the same as

func main() {
    gocron.Start()
    gocron.Every(5).Seconds().Do(task)
    gocron.Every(10).Seconds().Do(vijay)

    time.Sleep(20 * time.Second)

    gocron.Clear()
    fmt.Println(&quot;All task removed&quot;)
}

If you're exiting immediately, it doesn't really matter if you call Clear() first and then stop the scheduler, you can simply exit the program.

答案2

得分: 2

JimB权利。但我不知道为什么你要使用gocron方法和s方法。这个示例运行良好:

package main

import (
	"fmt"
	"time"

	"github.com/claudiu/gocron"
)

func task() {
	fmt.Println("I am runnning task.", time.Now())
}
func vijay() {
	fmt.Println("I am runnning vijay.", time.Now())
}

func main() {
	s := gocron.NewScheduler()
	s.Every(2).Seconds().Do(task)
	s.Every(4).Seconds().Do(vijay)

	sc := s.Start() // 保持通道
	go test(s, sc)  // 等待
	<-sc            // 如果通道关闭,将发生这种情况
}
func test(s *gocron.Scheduler, sc chan bool) {
	time.Sleep(8 * time.Second)
	s.Clear()
	fmt.Println("All task removed")
	close(sc) // 关闭通道
}

请注意,这是一个关于使用gocron库的示例代码。它创建了一个调度器s,并使用Every方法设置任务的执行频率。然后,它使用Do方法将任务函数添加到调度器中。在main函数中,调度器启动并返回一个通道sc,然后使用go关键字在后台运行test函数。test函数等待8秒钟,然后清除调度器中的所有任务,并关闭通道sc。最后,<-sc语句等待通道关闭,以确保所有任务完成后程序退出。

英文:

JimB rights. But I don't know why do you use gocron methods and the s methods. This example works fine:
<!-- lang:go -->

package main

import (
	&quot;fmt&quot;
	&quot;time&quot;

	&quot;github.com/claudiu/gocron&quot;
)

func task() {
	fmt.Println(&quot;I am runnning task.&quot;, time.Now())
}
func vijay() {
	fmt.Println(&quot;I am runnning vijay.&quot;, time.Now())
}

func main() {
	s := gocron.NewScheduler()
	s.Every(2).Seconds().Do(task)
	s.Every(4).Seconds().Do(vijay)

	sc := s.Start() // keep the channel
    go test(s, sc)  // wait
    &lt;-sc            // it will happens if the channel is closed
}
func test(s *gocron.Scheduler, sc chan bool) {
	time.Sleep(8 * time.Second)
	s.Clear()
	fmt.Println(&quot;All task removed&quot;)
	close(sc) // close the channel
}

huangapple
  • 本文由 发表于 2015年12月24日 21:56:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/34453894.html
匿名

发表评论

匿名网友

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

确定