-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
360 lines (328 loc) · 8.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//go:generate go run assets_generate.go
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"sort"
"strings"
"sync"
"time"
_ "github.com/denisenkom/go-mssqldb"
_ "github.com/go-sql-driver/mysql"
_ "github.com/godror/godror"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/gorilla/mux"
"github.com/jmoiron/sqlx"
"github.com/oxplot/runsheet/data"
"github.com/oxplot/runsheet/runsheet"
)
const (
DefaultQueryTimeout = 3000 // in milliseconds
MaxConnectionLifetime = time.Second * 15
)
type Config struct {
Listen string
QueryTimeout int // in milliseconds
ConnectionURL string
RunsheetsSQL string
}
type Runsheet struct {
Id string `db:"id" json:"id"`
Name *string `db:"name" json:"name"`
Description *string `db:"description" json:"description"`
ConnectionUrl string `db:"connection_url" json:"-"`
TasksSql string `db:"tasks_sql" json:"-"`
UpdateSql string `db:"update_sql" json:"-"`
StartTime *string `db:"start_time" json:"startTime"`
QueryTimeout *uint `db:"query_timeout" json:"-"`
Tasks []*runsheet.Task `db:"-" json:"-"`
Json []byte `db:"-" json:"-"`
Err string `db:"-" json:"error"`
}
var (
runsheets = struct {
Lock sync.Mutex
Sheets map[string]*Runsheet
Json []byte
Err error
StatusUpdates map[string]map[string]runsheet.TaskStatus
}{
Sheets: make(map[string]*Runsheet),
Json: []byte("{}"),
StatusUpdates: make(map[string]map[string]runsheet.TaskStatus),
}
configPath = flag.String("config", "", "path to config file - by default, config is loaded from various paths")
config = Config{
Listen: ":8080",
QueryTimeout: DefaultQueryTimeout,
}
connections = struct {
mu sync.Mutex
Conns map[string]*sqlx.DB
}{
Conns: make(map[string]*sqlx.DB),
}
)
func getConnFor(connUrl string) (*sqlx.DB, error) {
connections.mu.Lock()
defer connections.mu.Unlock()
conn := connections.Conns[connUrl]
if conn != nil {
return conn, nil
}
parts := strings.SplitN(connUrl, "://", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("connection url '%s' must be of form driver://...", connUrl)
}
conn, err := sqlx.Open(parts[0], parts[1])
if err != nil {
return nil, err
}
conn.SetConnMaxLifetime(MaxConnectionLifetime)
connections.Conns[connUrl] = conn
return conn, nil
}
func getSheetJson(sheet *Runsheet) ([]byte, error) {
type TaskWithDeps struct {
*runsheet.Task
Dependees []string `json:"dependees"`
Dependers []string `json:"dependers"`
}
tasksWithDeps := make([]TaskWithDeps, 0, len(sheet.Tasks))
dependees := runsheet.DependeesFor(sheet.Tasks)
for _, t := range sheet.Tasks {
twd := TaskWithDeps{Task: t}
for _, d := range t.Dependers {
twd.Dependers = append(twd.Dependers, d.Id)
}
if twd.Dependers == nil {
twd.Dependers = make([]string, 0) // so we get [] instead of null
}
sort.Slice(twd.Dependers, func(i, j int) bool {
return twd.Dependers[i] < twd.Dependers[j]
})
for _, d := range dependees[t] {
twd.Dependees = append(twd.Dependees, d.Id)
}
if twd.Dependees == nil {
twd.Dependees = make([]string, 0) // so we get [] instead of null
}
sort.Slice(twd.Dependees, func(i, j int) bool {
return twd.Dependees[i] < twd.Dependees[j]
})
tasksWithDeps = append(tasksWithDeps, twd)
}
json, err := json.Marshal(struct {
*Runsheet
Tasks []TaskWithDeps `json:"tasks"`
}{sheet, tasksWithDeps})
if err != nil {
return nil, err
}
return json, nil
}
func loadRunsheets() (map[string]*Runsheet, error) {
rows := []*Runsheet{}
if err := func() error {
conn, err := getConnFor(config.ConnectionURL)
if err != nil {
return err
}
ctx, _ := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(config.QueryTimeout))
if err := conn.SelectContext(ctx, &rows, config.RunsheetsSQL); err != nil {
return err
}
return nil
}(); err != nil {
return nil, err
}
sheetIdMap := make(map[string]bool)
for _, r := range rows {
if r.Id == "" {
return nil, fmt.Errorf("runsheet id cannot be blank")
}
if sheetIdMap[r.Id] {
return nil, fmt.Errorf("runsheet id '%s' must be unique", r.Id)
}
sheetIdMap[r.Id] = true
}
sheets := make(map[string]*Runsheet)
done := make(chan bool)
for _, r := range rows {
if r.QueryTimeout == nil {
r.QueryTimeout = new(uint)
*r.QueryTimeout = DefaultQueryTimeout
}
sheets[r.Id] = r
go func(r *Runsheet) {
defer func() { done <- true }()
defer func() {
json, err := getSheetJson(r)
if err != nil {
json = []byte("{error: \"fatal JSON encoder error - contact developer\"}")
}
r.Json = json
}()
conn, err := getConnFor(r.ConnectionUrl)
if err != nil {
r.Err = err.Error()
return
}
r.Tasks, err = runsheet.LoadTasks(conn, r.TasksSql, time.Millisecond*time.Duration(*r.QueryTimeout))
if err != nil {
r.Err = err.Error()
return
}
if err := runsheet.ValidateTasks(r.Tasks); err != nil {
r.Err = err.Error()
return
}
runsheet.ScheduleTasks(r.Tasks)
}(r)
}
for range rows {
<-done
}
return sheets, nil
}
func processUpdates() {
runsheets.Lock.Lock()
sheets := runsheets.Sheets
updates := runsheets.StatusUpdates
runsheets.StatusUpdates = make(map[string]map[string]runsheet.TaskStatus)
runsheets.Lock.Unlock()
for sheetId, taskStatuses := range updates {
sheet := sheets[sheetId]
if sheet == nil {
continue
}
func(sheet *Runsheet) {
conn, err := getConnFor(sheet.ConnectionUrl)
if err != nil {
return
}
for taskId, status := range taskStatuses {
sql := strings.Replace(sheet.UpdateSql, "{{status}}", string(status), -1)
sql = strings.Replace(sql, "{{task}}", taskId, -1)
ctx, _ := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(*sheet.QueryTimeout))
if _, err = conn.ExecContext(ctx, sql); err != nil {
log.Print(err)
}
}
}(sheet)
}
}
func updateRunsheets() {
var sheetsMap map[string]*Runsheet
sheetsMap, err := loadRunsheets()
var sheets []*Runsheet
if err == nil {
sheets = make([]*Runsheet, 0, len(sheetsMap))
for _, r := range sheetsMap {
sheets = append(sheets, r)
}
sort.Slice(sheets, func(i, j int) bool {
return sheets[i].Id < sheets[j].Id
})
}
var errStr string
if err != nil {
errStr = err.Error()
}
json, err := json.Marshal(struct {
Sheets []*Runsheet `json:"sheets"`
Err string `json:"error"`
}{sheets, errStr})
if err != nil {
json = []byte("{error: \"fatal JSON encoder error - contact developer\"}")
}
runsheets.Lock.Lock()
runsheets.Sheets = sheetsMap
runsheets.Json = json
runsheets.Lock.Unlock()
}
func runsheetLoop() {
for {
processUpdates()
updateRunsheets()
time.Sleep(time.Second)
}
}
func spaHandler(w http.ResponseWriter, r *http.Request) {
w.Write(data.SpaHtml)
}
func runsheetsHandler(w http.ResponseWriter, r *http.Request) {
var json []byte
runsheets.Lock.Lock()
json = runsheets.Json
runsheets.Lock.Unlock()
w.Header().Add("Content-Type", "application/json")
w.Write(json)
}
func tasksEPHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
runsheets.Lock.Lock()
sheet := runsheets.Sheets[vars["sheet"]]
runsheets.Lock.Unlock()
if sheet == nil {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Add("Content-Type", "application/json")
w.Write(sheet.Json)
}
func taskHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
spaHandler(w, r)
return
}
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
vars := mux.Vars(r)
r.ParseForm()
func() {
runsheets.Lock.Lock()
defer runsheets.Lock.Unlock()
if _, ok := runsheets.Sheets[vars["sheet"]]; !ok {
return
}
ts := runsheets.StatusUpdates[vars["sheet"]]
if ts == nil {
ts = make(map[string]runsheet.TaskStatus)
runsheets.StatusUpdates[vars["sheet"]] = ts
}
ts[vars["task"]] = runsheet.TaskStatus(r.Form.Get("status"))
}()
}
func main() {
flag.Parse()
if *configPath == "" {
log.Fatal("config path must be set with -config")
}
cfgBytes, err := ioutil.ReadFile(*configPath)
if err != nil {
log.Fatalf("failed reading config - %s", err)
}
if err := json.Unmarshal(cfgBytes, &config); err != nil {
log.Fatalf("failed reading config - %s", err)
}
go runsheetLoop()
router := mux.NewRouter()
for _, p := range []string{"", "guide", "sheet/{sheet}"} {
router.HandleFunc("/"+p, spaHandler)
}
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(data.StaticAssets)))
router.HandleFunc("/sheets", runsheetsHandler)
router.HandleFunc("/sheet/{sheet}/tasks", tasksEPHandler)
router.HandleFunc("/sheet/{sheet}/task/{task}", taskHandler)
log.Printf("listening on http://%s/", config.Listen)
panic(http.ListenAndServe(config.Listen, router))
}