gocron创建了多个任务实例。

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

gocron creates multiple instances of task

问题

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

  1. "github.com/jasonlvhit/gocron"

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

  1. func main() {
  2. for i := 0; i < 3; i++ {
  3. channel := make(chan string)
  4. go taskCron(channel, i)
  5. }
  6. time.Sleep(time.Second * 5)
  7. gocron.Clear()
  8. fmt.Println("stop this shit")
  9. }
  10. func task(i int) {
  11. fmt.Println("still running...", i)
  12. }
  13. func taskCron(channel chan string, i int) {
  14. gocron.Every(4).Seconds().Do(task, i)
  15. <-gocron.Start()
  16. }

运行它给了我这个输出:

  1. ——▶go run *.go
  2. still running... 0
  3. still running... 0
  4. still running... 1
  5. still running... 1
  6. still running... 2
  7. still running... 2
  8. still running... 0
  9. still running... 1
  10. still running... 2
  11. stop this

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

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

英文:

I had a problem in a script using this package:

  1. &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:

  1. func main() {
  2. for i := 0; i &lt; 3; i++ {
  3. channel := make(chan string)
  4. go taskCron(channel, i)
  5. }
  6. time.Sleep(time.Second * 5)
  7. gocron.Clear()
  8. fmt.Println(&quot;stop this shit&quot;)
  9. }
  10. func task(i int) {
  11. fmt.Println(&quot;still running...&quot;, i)
  12. }
  13. func taskCron(channel chan string, i int) {
  14. gocron.Every(4).Seconds().Do(task, i)
  15. &lt;-gocron.Start()
  16. }

running it gave me this output:

  1. ——▶go run *.go
  2. still running... 0
  3. still running... 0
  4. still running... 1
  5. still running... 1
  6. still running... 2
  7. still running... 2
  8. still running... 0
  9. still running... 1
  10. still running... 2
  11. stop this

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

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

答案1

得分: 3

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

好像是这样的

  1. <-gocron.Start()

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

  1. func main() {
  2. for i := 0; i < 3; i++ {
  3. taskCron(i)
  4. }
  5. channel2 := make(chan int)
  6. go startCron(channel2)
  7. time.Sleep(time.Second * 5)
  8. gocron.Clear()
  9. fmt.Println("停止")
  10. }
  11. func task(i int) {
  12. fmt.Println("仍在运行...", i)
  13. }
  14. func taskCron(i int) {
  15. gocron.Every(4).Seconds().Do(task, i)
  16. }
  17. func startCron(channel chan int) {
  18. <-gocron.Start()
  19. }

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

英文:

Ok apparently

  1. &lt;-gocron.Start()

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

  1. func main() {
  2. for i := 0; i &lt; 3; i++ {
  3. taskCron(i)
  4. }
  5. channel2 := make(chan int)
  6. go startCron(channel2)
  7. time.Sleep(time.Second * 5)
  8. gocron.Clear()
  9. fmt.Println(&quot;stop this&quot;)
  10. }
  11. func task(i int) {
  12. fmt.Println(&quot;still running...&quot;, i)
  13. }
  14. func taskCron(i int) {
  15. gocron.Every(4).Seconds().Do(task, i)
  16. }
  17. func startCron(channel chan int) {
  18. &lt;-gocron.Start()
  19. }

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:

确定