forked from stripe-archive/timberlake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
156 lines (126 loc) · 3.87 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"encoding/json"
"flag"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
"log"
"net/http"
"os"
"path/filepath"
"time"
)
var resourceManagerURL = flag.String("resource-manager-url", "http://localhost:8088", "The HTTP URL to access the resource manager.")
var historyServerURL = flag.String("history-server-url", "http://localhost:19888", "The HTTP URL to access the history server.")
var proxyServerURL = flag.String("proxy-server-url", "", "The HTTP URL to access the proxy server, if separate from the resource manager.")
var namenodeAddress = flag.String("namenode-address", "localhost:9000", "The host:port to access the Namenode metadata service.")
var yarnLogDir = flag.String("yarn-logs-dir", "/tmp/logs", "The HDFS path where YARN stores logs. This is the controlled by the hadoop property yarn.nodemanager.remote-app-log-dir.")
var httpTimeout = flag.Duration("http-timeout", time.Second*2, "The timeout used for connecting to YARN API. Pass values like: 2s")
var pollInterval = flag.Duration("poll-interval", time.Second*2, "How often should we poll the job APIs. Pass values like: 2s")
var jt jobTracker
var rootPath, staticPath string
func index(c web.C, w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(rootPath, "index.html"))
}
func getJobs(c web.C, w http.ResponseWriter, r *http.Request) {
// We only need the details for listing pages.
var jobs []*job
for _, j := range jt.Jobs {
jobs = append(jobs, &job{Details: j.Details, Conf: j.Conf})
}
jsonBytes, err := json.Marshal(jobs)
if err != nil {
log.Println("error:", err)
w.WriteHeader(500)
return
}
w.Write(jsonBytes)
}
func getJob(c web.C, w http.ResponseWriter, r *http.Request) {
if !jt.HasJob(c.URLParams["id"]) {
w.WriteHeader(404)
return
}
job := jt.GetJob(c.URLParams["id"])
jsonBytes, err := json.Marshal(job)
if err != nil {
log.Println("error:", err)
w.WriteHeader(500)
return
}
w.Write(jsonBytes)
}
func getLogs(c web.C, w http.ResponseWriter, r *http.Request) {
if !jt.HasJob(c.URLParams["id"]) {
w.WriteHeader(404)
return
}
lines, err := jt.FetchLogs(c.URLParams["id"])
if err != nil {
log.Println("error:", err)
w.WriteHeader(500)
return
}
jsonBytes, err := json.Marshal(lines)
if err != nil {
log.Println("error:", err)
w.WriteHeader(500)
return
}
w.Write(jsonBytes)
}
func killJob(c web.C, w http.ResponseWriter, r *http.Request) {
if !jt.HasJob(c.URLParams["id"]) {
w.WriteHeader(404)
return
}
id := c.URLParams["id"]
err := jt.KillJob(id)
if err != nil {
log.Println("error: ", err)
w.WriteHeader(500)
}
}
func init() {
binPath, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
rootPath = filepath.Join(binPath, "..")
staticPath = filepath.Join(rootPath, "static")
}
func main() {
flag.Parse()
if *proxyServerURL == "" {
proxyServerURL = resourceManagerURL
}
jt = newJobTracker(*resourceManagerURL, *historyServerURL, *proxyServerURL)
go jt.Loop()
if err := jt.TestLogsDir(); err != nil {
log.Printf("WARNING: Could not read yarn logs directory. Error message: `%s`\n", err)
log.Println("\tYou can change the path with --yarn-logs-dir=HDFS_PATH.")
log.Println("\tTo talk to HDFS, Timberlake needs to be able to access the namenode (--namenode-address) and datanodes.")
}
sse := newSSE()
go sse.Loop()
static := http.StripPrefix("/static/", http.FileServer(http.Dir(staticPath)))
log.Println("serving static files from", staticPath)
goji.Get("/static/*", static)
goji.Get("/", index)
goji.Get("/jobs/", getJobs)
goji.Get("/sse", sse)
goji.Get("/jobs/:id", getJob)
goji.Get("/jobs/:id/logs", getLogs)
goji.Post("/jobs/:id/kill", killJob)
go func() {
for job := range jt.updates {
jsonBytes, err := json.Marshal(job)
if err != nil {
log.Println("json error: ", err)
} else {
sse.events <- jsonBytes
}
}
}()
goji.Serve()
}