我在使用此软件包的脚本中遇到问题:
"github.com/jasonlvhit/gocron"
我找不到错误后写了这个小小的testscript,结果是执行cronjobs的次数是预期的两倍:
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.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("stop this") } func task(i int) { fmt.Println("still running...", i) } func taskCron(i int) { gocron.Every(4).Seconds().Do(task, i) } func startCron(channel chan int) { <-gocron.Start() }
我希望这对遇到相同问题的任何人有所帮助!