gocron创建了多个任务实例。

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

gocron creates multiple instances of task

问题

我在使用这个包时遇到了一个问题:

	"github.com/jasonlvhit/gocron"

在我找不到错误的情况下,我编写了这个小测试脚本,结果发现执行的cron作业是预期的两倍:

func main() {
	for i := 0; i < 3; i++ {
		channel := make(chan string)
		go taskCron(channel, i)
	}

	time.Sleep(time.Second * 5)
	gocron.Clear()
	fmt.Println("stop this shit")
}

func task(i int) {
	fmt.Println("still running...", i)
}

func taskCron(channel chan string, i int) {
	gocron.Every(4).Seconds().Do(task, i)
	<-gocron.Start()
}

运行它给了我这个输出:

——▶go run *.go
still running... 0
still running... 0
still running... 1
still running... 1
still running... 2
still running... 2
still running... 0
still running... 1
still running... 2
stop this

有人知道如何创建动态数量的gocron作业而不重复它们吗?

谢谢 gocron创建了多个任务实例。

英文:

I had a problem in a script using this package:

	&quot;github.com/jasonlvhit/gocron&quot;

I wrote this little testscript after I couldn't find a mistake and it resulted that there were twice as many cronjobs executed as intended:

func main() {
	for i := 0; i &lt; 3; i++ {
		channel := make(chan string)
		go taskCron(channel, i)
	}

	time.Sleep(time.Second * 5)
	gocron.Clear()
	fmt.Println(&quot;stop this shit&quot;)
}

func task(i int) {
	fmt.Println(&quot;still running...&quot;, i)
}

func taskCron(channel chan string, i int) {
	gocron.Every(4).Seconds().Do(task, i)
	&lt;-gocron.Start()
}

running it gave me this output:

——▶go run *.go
still running... 0
still running... 0
still running... 1
still running... 1
still running... 2
still running... 2
still running... 0
still running... 1
still running... 2
stop this

Does anyone know how I can create a dynamic amount of gocron jobs without duplicating them?

Thanks gocron创建了多个任务实例。

答案1

得分: 3

好的,以下是翻译好的内容:

好像是这样的

<-gocron.Start()

会重新启动已经启动的作业,所以为了解决我的问题,我必须将脚本更改为以下内容:

func main() {
	for i := 0; i < 3; i++ {
		taskCron(i)
	}
	channel2 := make(chan int)
	go startCron(channel2)

	time.Sleep(time.Second * 5)
	gocron.Clear()
	fmt.Println("停止")
}

func task(i int) {
	fmt.Println("仍在运行...", i)
}

func taskCron(i int) {
	gocron.Every(4).Seconds().Do(task, i)
}

func startCron(channel chan int) {
	<-gocron.Start()
}

希望这对于遇到相同问题的人有所帮助!

英文:

Ok apparently

&lt;-gocron.Start()

will start jobs that were already started again so to fix my issue, I had to change the script to this:

func main() {
	for i := 0; i &lt; 3; i++ {
		taskCron(i)
	}
	channel2 := make(chan int)
	go startCron(channel2)

	time.Sleep(time.Second * 5)
	gocron.Clear()
	fmt.Println(&quot;stop this&quot;)
}

func task(i int) {
	fmt.Println(&quot;still running...&quot;, i)
}

func taskCron(i int) {
	gocron.Every(4).Seconds().Do(task, i)
}

func startCron(channel chan int) {
	&lt;-gocron.Start()
}

I hope this helps anybody who had the same Problem!

huangapple
  • 本文由 发表于 2017年6月25日 19:52:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/44746083.html
匿名

发表评论

匿名网友

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

确定