-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
executable file
·92 lines (75 loc) · 2.31 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
/*
K8S Dispatcher Service
// register SQS url
curl -X PUT http://localhost:8000/sqs?url=https://sqs.us-east-1.amazonaws.com/440721843528/mySQS
// start the service/server
curl -X PUT http://localhost:8000/server?start=true
// register new job
curl -X POST http://localhost:8000/job -d '{"Name":"TEST3", "Pattern":"s3://test/*","Image":"quay.io/cdis/indexs3client:master", "ImageConfig":{}}'
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/golang/glog"
"github.com/uc-cdis/ssjdispatcher/handlers"
)
func usage() {
fmt.Fprintf(os.Stderr, "usage: ssjdispatcher -stderrthreshold=[INFO|WARN|FATAL] -log_dir=[string]\n")
flag.PrintDefaults()
os.Exit(2)
}
func init() {
flag.Usage = usage
if err := flag.Set("logtostderr", "true"); err != nil {
log.Fatalf("Failed to set flag: %v", err)
}
if err := flag.Set("stderrthreshold", "INFO"); err != nil {
log.Fatalf("Failed to set flag: %v", err)
}
}
func main() {
// NOTE: This next line is key you have to call flag.Parse() for the command line
// options or "flags" that are defined in the glog module to be picked up.
flag.Parse()
jsonBytes, err := handlers.ReadFile(handlers.LookupCredFile())
if err != nil {
glog.Errorln("Can not read credential file!")
os.Exit(1)
}
var sqsURL string
if sqs, err := handlers.GetValueFromJSON(jsonBytes, []string{"SQS", "url"}); err != nil {
glog.Errorln("Can not read SQS url from credential file!")
os.Exit(1)
} else {
sqsURL = sqs.(string)
}
jobInterfaces, _ := handlers.GetValueFromJSON(jsonBytes, []string{"JOBS"})
b, err := json.Marshal(jobInterfaces)
if err != nil {
glog.Info("There is no jobs configured in json credential file")
}
jobConfigs := make([]handlers.JobConfig, 0)
if err := json.Unmarshal(b, &jobConfigs); err != nil {
log.Fatalf("Failed to unmarshal JSON: %v", err)
}
if err := handlers.CheckIndexingJobsImageConfig(jobConfigs); err != nil {
glog.Error(err)
os.Exit(1)
}
// start an SQSHandler instance
SQSHandler := handlers.NewSQSHandler(sqsURL)
if err := SQSHandler.StartServer(); err != nil {
glog.Errorf("Can not start the server. Detail %s", err)
}
SQSHandler.JobConfigs = jobConfigs
SQSHandler.RegisterSQSHandler()
handlers.RegisterJob()
handlers.RegisterSystem()
log.Fatal(http.ListenAndServe("0.0.0.0:8000", nil))
glog.Flush()
}