【问题标题】:Running a Go method using cron使用 cron 运行 Go 方法
【发布时间】:2015-05-06 09:32:54
【问题描述】:

我正在尝试编写一个程序,该程序将在某个时间间隔内连续调用一个方法。我正在使用 cron 库来尝试实现这一点,但是当我运行程序时,它只是执行并完成,没有任何输出。

下面是我正在尝试做的一个基本示例。

非常感谢您的帮助!

package main

import (
    "fmt"
    "github.com/robfig/cron"
)

func main() {
    c := cron.New()
    c.AddFunc("1 * * * * *", RunEverySecond)
    c.Start()
}

func RunEverySecond() {
    fmt.Println("----")
}

【问题讨论】:

标签: go cron


【解决方案1】:

或类似的使用同步等待组。

package main

import (
    "fmt"
    "sync"

    "github.com/robfig/cron"
)

// RunEverySecond is to run all the time.
func RunEverySecond() {
    fmt.Println("----")
    //wg.Done() // Does not release the waitgroup.
}

func main() {
    wg := &sync.WaitGroup{}
    wg.Add(1)
    c := cron.New()
    c.AddFunc("@every 1s", RunEverySecond)
    c.Start()
    wg.Wait() // This guarantees this program never exits so cron can keep running as per the cron interval.
}

【讨论】:

    【解决方案2】:

    只要确保应用程序始终运行,即:

    func main() {
        cronJob := cron.New()
        cronJob.Start()
        cronJob.AddFunc("* * * * * ?", PushConfigs)
        for {
        }
    }
    

    【讨论】:

      【解决方案3】:

      为此使用外部包是多余的,time 包有你需要的一切:

      package main
      
      import (
          "fmt"
          "time"
      )
      
      func main() {
          go func() {
              c := time.Tick(1 * time.Second)
              for range c {
                  // Note this purposfully runs the function
                  // in the same goroutine so we make sure there is
                  // only ever one. If it might take a long time and
                  // it's safe to have several running just add "go" here.
                  RunEverySecond()
              }
          }()
      
          // Other processing or the rest of your program here.
          time.Sleep(5 * time.Second)
      
          // Or to block forever:
          //select {}
          // However, if doing that you could just stick the above for loop
          // right here without dropping it into a goroutine.
      }
      
      func RunEverySecond() {
          fmt.Println("----")
      }
      

      playground

      【讨论】:

        【解决方案4】:

        您可以等待操作系统向您发出信号,例如来自用户的 CTRL-C。此外,您的 cron 表达式是每分钟的,即仅在 seconds == 1 处。

        package main
        
        import (
            "fmt"
            "os"
            "os/signal"
            "time"
        
            "github.com/robfig/cron"
        )
        
        func main() {
            c := cron.New()
            c.AddFunc("* * * * * *", RunEverySecond)
            go c.Start()
            sig := make(chan os.Signal)
            signal.Notify(sig, os.Interrupt, os.Kill)
            <-sig
        
        }
        
        func RunEverySecond() {
            fmt.Printf("%v\n", time.Now())
        }
        

        【讨论】:

          【解决方案5】:

          如您所见,c.Start() 在另一个 goroutine 中运行,因此对 c.Start 的调用立即返回。 https://github.com/robfig/cron/blob/master/cron.go#L125

          因此,您的程序在您看到任何输出之前完成。您可以添加 time.Sleep(1 * minute) 之类的内容或为此设置一个关闭频道(或者只是 &lt;-make(chan struct{}) 永久等待)

          【讨论】:

          • select{} 是永久阻止的最简单方法。
          猜你喜欢
          • 2021-07-05
          • 1970-01-01
          • 2010-10-23
          • 2019-09-06
          • 2013-03-11
          • 2012-06-03
          • 2012-09-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多