-
Notifications
You must be signed in to change notification settings - Fork 58
/
go-cron.go
69 lines (49 loc) · 1.16 KB
/
go-cron.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import "os"
import "os/exec"
import "strings"
import "sync"
import "os/signal"
import "syscall"
import "github.com/robfig/cron"
func execute(command string, args []string)() {
println("executing:", command, strings.Join(args, " "))
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
cmd.Wait()
}
func create() (cr *cron.Cron, wgr *sync.WaitGroup) {
var schedule string = os.Args[1]
var command string = os.Args[2]
var args []string = os.Args[3:len(os.Args)]
wg := &sync.WaitGroup{}
c := cron.New()
println("new cron:", schedule)
c.AddFunc(schedule, func() {
wg.Add(1)
execute(command, args)
wg.Done()
})
return c, wg
}
func start(c *cron.Cron, wg *sync.WaitGroup) {
c.Start()
}
func stop(c *cron.Cron, wg *sync.WaitGroup) {
println("Stopping")
c.Stop()
println("Waiting")
wg.Wait()
println("Exiting")
os.Exit(0)
}
func main() {
c, wg := create()
go start(c, wg)
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
println(<-ch)
stop(c, wg)
}