-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
88 lines (66 loc) · 1.8 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
package main
import (
"flag"
"net/http"
"os"
"strconv"
"time"
"github.com/opensourceways/community-robot-lib/interrupts"
"github.com/opensourceways/community-robot-lib/logrusutil"
"github.com/sirupsen/logrus"
"github.com/zengchen1024/obs-worker/config"
"github.com/zengchen1024/obs-worker/controllers"
"github.com/zengchen1024/obs-worker/worker"
)
type options struct {
config string
port int
gracePeriod time.Duration
}
func gatherOptions(fs *flag.FlagSet, args ...string) options {
var o options
fs.StringVar(
&o.config, "config", "./conf/build_config.yaml",
"Path to the config file.",
)
fs.IntVar(&o.port, "port", 8080, "http server port")
fs.DurationVar(
&o.gracePeriod, "grace-period", 180*time.Second,
"On shutdown, try to handle remaining events for the specified duration.",
)
fs.Parse(args)
return o
}
func main() {
logrusutil.ComponentInit("obs-worker")
o := gatherOptions(
flag.NewFlagSet(os.Args[0], flag.ExitOnError),
os.Args[1:]...,
)
cfg, err := config.Load(o.config)
if err != nil {
logrus.WithError(err).Fatal("load config failed")
}
if err := worker.Init(&cfg.Build, o.port); err != nil {
logrus.WithError(err).Fatal("init worker")
}
defer worker.Exit()
run(&o)
}
func run(o *options) {
defer interrupts.WaitForGracefulShutdown()
register()
httpServer := &http.Server{Addr: ":" + strconv.Itoa(o.port)}
interrupts.ListenAndServe(httpServer, o.gracePeriod)
}
func register() {
c := controllers.BuildController{}
http.HandleFunc("/build", c.Build)
http.HandleFunc("/info", c.JobInfo)
http.HandleFunc("/worker", c.WorkerInfo)
http.HandleFunc("/kill", c.KillJob)
http.HandleFunc("/discard", c.DiscardJob)
http.HandleFunc("/badhost", c.SetBadHostJob)
http.HandleFunc("/sysrq", c.SetSysrqJob)
http.HandleFunc("/logfile", c.GetBuildLog)
}