-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (79 loc) · 1.81 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"runtime"
"runtime/debug"
"time"
"djinn-ci.com/build"
"djinn-ci.com/config"
"djinn-ci.com/errors"
"djinn-ci.com/version"
)
func main() {
var (
configfile string
limit int64
showversion bool
)
fs := flag.CommandLine
fs.Int64Var(&limit, "limit", 1<<30, "the limit in bytes after which old artifacts should be removed")
fs.StringVar(&configfile, "config", "djinn-curator.conf", "the config file to use")
fs.BoolVar(&showversion, "version", false, "show the version and exit")
fs.Parse(os.Args[1:])
if showversion {
fmt.Printf("%s %s %s/%s\n", os.Args[0], version.Build, runtime.GOOS, runtime.GOARCH)
return
}
f, err := os.Open(configfile)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", os.Args[0], errors.Cause(err))
os.Exit(1)
}
defer f.Close()
cfg, err := config.DecodeCurator(f.Name(), f)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", os.Args[0], errors.Cause(err))
os.Exit(1)
}
pidfile := cfg.Pidfile()
db := cfg.DB()
log := cfg.Log()
defer func() {
db.Close()
log.Close()
if pidfile != nil {
if err := os.RemoveAll(pidfile.Name()); err != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", os.Args[0], err)
os.Exit(1)
}
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
t := time.NewTicker(time.Minute)
artifacts := cfg.Artifacts()
loop:
for {
select {
case <-t.C:
func() {
defer func() {
v := recover()
if e, ok := v.(error); ok {
log.Error.Println(e.Error() + "\n" + string(debug.Stack()))
}
}()
curator := build.NewCurator(db, artifacts, limit)
if err := curator.Invoke(log); err != nil {
log.Error.Println(err)
}
}()
case sig := <-c:
log.Info.Println("signal:", sig, "received, shutting down")
break loop
}
}
}