Running a Go method using cron

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

Running a Go method using cron

问题

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

以下是我尝试做的一个基本示例。

非常感谢您的帮助!

package main

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

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

func RunEverySecond() {
	fmt.Println("----")
}
英文:

I'm trying to write a program that will continuously call a method at a certain time interval. I'm using a cron library to try and achieve this but when I run the program it just executes and finishes with out any output.

Below is a basic example of what I'm trying to do.

Assistance greatly appreciated!

package main

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

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

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

答案1

得分: 11

你可以等待操作系统发出信号,例如来自用户的CTRL-C。另外,你的cron表达式是每分钟运行一次,即只有当秒数等于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())
}
英文:

You can wait for the OS to signal you, e.g. CTRL-C from the user. Also your cron expression was for every minute, i.e. only where seconds == 1.

package main

import (
	&quot;fmt&quot;
	&quot;os&quot;
	&quot;os/signal&quot;
	&quot;time&quot;

	&quot;github.com/robfig/cron&quot;
)

func main() {
	c := cron.New()
	c.AddFunc(&quot;* * * * * *&quot;, RunEverySecond)
	go c.Start()
	sig := make(chan os.Signal)
	signal.Notify(sig, os.Interrupt, os.Kill)
	&lt;-sig

}

func RunEverySecond() {
	fmt.Printf(&quot;%v\n&quot;, time.Now())
}

答案2

得分: 6

你可以看到c.Start()在另一个goroutine中运行,所以对c.Start的调用会立即返回。因此,你的程序会在看到任何输出之前就结束了。你可以添加类似time.Sleep(1 * minute)这样的代码,或者为此目的创建一个关闭的channel(或者只是<-make(chan struct{})来永久等待)。

英文:

As you can see c.Start() runs in another goroutine, so the call to c.Start returns immediately. https://github.com/robfig/cron/blob/master/cron.go#L125

So your program finishes earlier than you see any output. You may add something like time.Sleep(1 * minute) or have a close channel for this (or just &lt;-make(chan struct{}) to wait eternally)

答案3

得分: 6

使用外部包来完成这个任务有些过度,time 包已经提供了你所需的一切:

package main

import (
	"fmt"
	"time"
)

func main() {
	go func() {
		c := time.Tick(1 * time.Second)
		for range c {
			// 注意这里故意在同一个 goroutine 中运行函数,
			// 确保只有一个函数在运行。如果可能需要很长时间并且可以安全地同时运行多个函数,
			// 可以在这里添加 "go" 关键字。
			RunEverySecond()
		}
	}()

	// 其他处理或程序的其余部分在这里。
	time.Sleep(5 * time.Second)

	// 或者永远阻塞:
	//select {}
	// 但是,如果这样做,你可以直接将上面的 for 循环放在这里,而不需要将其放入 goroutine 中。
}

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

<kbd>playground</kbd>

英文:

Using an external package for this is overkill, the time package has everything you need:

package main

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

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&#39;s safe to have several running just add &quot;go&quot; 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(&quot;----&quot;)
}

<kbd>playground</kbd>

答案4

得分: 4

或者类似这样使用同步等待组。

package main

import (
	"fmt"
	"sync"

	"github.com/robfig/cron"
)

// RunEverySecond 是一直运行的函数。
func RunEverySecond() {
	fmt.Println("----")
	//wg.Done() // 不会释放等待组。
}

func main() {
	wg := &sync.WaitGroup{}
	wg.Add(1)
	c := cron.New()
	c.AddFunc("@every 1s", RunEverySecond)
	c.Start()
	wg.Wait() // 这保证了该程序永远不会退出,以便 cron 按照 cron 间隔继续运行。
}
英文:

or something like this using sync wait group.

package main

import (
	&quot;fmt&quot;
	&quot;sync&quot;

	&quot;github.com/robfig/cron&quot;
)

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

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

答案5

得分: -1

请确保应用程序始终保持运行,即:

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

Just make sure to keep the application always running, ie:

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

huangapple
  • 本文由 发表于 2015年3月5日 13:27:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/28870660.html
匿名

发表评论

匿名网友

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

确定