-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (59 loc) · 1.49 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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"github.com/rochacon/docker-cron/Godeps/_workspace/src/github.com/robfig/cron"
)
// DOCKER_HOST points to the Docker daemon socket
var DOCKER_HOST string
func main() {
jobsDir := flag.String("jobs", "jobs", "Directory containing job definitions.")
flag.Parse()
// Get Docker host from environment
DOCKER_HOST = os.Getenv("DOCKER_HOST")
if DOCKER_HOST == "" {
DOCKER_HOST = "unix:///var/run/docker.sock"
}
// Get jobs descriptions filenames
jobs, err := filepath.Glob(fmt.Sprintf("%s/*.json", *jobsDir))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Setup cron
cr := cron.New()
registerJobs(cr, jobs)
cr.Start()
// Wait SIGINT or SIGKILL to quit app
fmt.Println("Started, press CTRL+C or send a SIGKILL to quit.")
exit := make(chan os.Signal, 1)
signal.Notify(exit, os.Interrupt, os.Kill)
<-exit
cr.Stop()
// TODO clean running jobs.
}
// registerJobs load jobs from disk
func registerJobs(cr *cron.Cron, jobs []string) {
for _, filename := range jobs {
fp, err := os.Open(filename)
if err != nil {
fmt.Printf("Error opening job %q: %q\n", filename, err)
os.Exit(4)
}
defer fp.Close()
j, err := NewJob(fp)
if err != nil {
fmt.Printf("Error parsing job from %q: %q\n", filename, err)
os.Exit(2)
}
fmt.Printf("Registering job %q for %q\n", j.Id, j.Interval)
err = cr.AddJob(j.Interval, j)
if err != nil {
fmt.Printf("Error registering job %q: %q\n", j.Id, err)
os.Exit(3)
}
}
}