forked from odise/go-cron
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
75 lines (57 loc) · 1.4 KB
/
main.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
68
69
70
71
72
73
74
75
package main
import (
"flag"
"github.com/prodrigestivill/go-cron/gocron"
"log"
"os"
"os/signal"
"syscall"
)
var build string
var version string
func main() {
flagArgs, execArgs := splitArgs()
os.Args = flagArgs
var (
help = flag.Bool("h", false, "display usage")
port = flag.String("p", "18080", "bind healthcheck to a specific port, set to 0 to not open HTTP port at all")
schedule = flag.String("s", "* * * * *", "schedule the task the cron style")
initrun = flag.Bool("i", false, "run one first time on start after initialization")
)
flag.Parse()
if *help {
println("Usage of", os.Args[0], "(build", build, ")")
println(os.Args[0], " [ OPTIONS ] -- [ COMMAND ]")
flag.PrintDefaults()
os.Exit(1)
}
log.Println("Running version:", version)
c, wg := gocron.Create(*schedule, execArgs[0], execArgs[1:len(execArgs)])
go gocron.Start(c)
if *port != "0" {
go gocron.Http_server(*port)
}
if *initrun {
go gocron.RunJobs(c)
}
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
println(<-ch)
gocron.Stop(c, wg)
}
func splitArgs() (flagArgs []string, execArgs []string) {
split := len(os.Args)
for idx, e := range os.Args {
if e == "--" {
split = idx
break
}
}
flagArgs = os.Args[0:split]
if split < len(os.Args) {
execArgs = os.Args[split+1 : len(os.Args)]
} else {
execArgs = []string{}
}
return flagArgs, execArgs
}