-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestScheduler.go
112 lines (94 loc) · 2.09 KB
/
TestScheduler.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
// TestScheduler.go
package autositespeed
import (
"html/template"
"net/http"
"github.com/bamzi/jobrunner"
)
type testSchedulerConf struct {
Schedule string `json:"schedule"`
WebConsoleEndPoint string `json:"webConsoleEndPoint"`
}
type scheduledJob interface {
Run()
}
type testScheduler struct {
conf *testSchedulerConf
}
var statusTmplText string = `
<html>
<head>
<style>
body {
margin: 30px 0 0 0;
font-size: 11px;
font-family: sans-serif;
color: #345;
}
h1 {
font-size: 24px;
text-align: center;
padding: 10 0 30px;
}
table {
/*max-width: 80%;*/
margin: 0 auto;
border-collapse: collapse;
border: none;
}
table td, table th {
min-width: 25px;
width: auto;
padding: 15px 20px;
border: none;
}
table tr:nth-child(odd) {
background-color: #f0f0f0;
}
table tr:nth-child(1) {
background-color: #345;
color: white;
}
th {
text-align: left;
}
</style>
</head>
<body>
<h1>AutoSiteSpeed Status Console</h1>
<table>
<tr><th>ID</th><th>Name</th><th>Status</th><th>Last run</th><th>Next run</th><th>Latency</th></tr>
{{range .}}
<tr>
<td>{{.Id}}</td>
<td>{{.JobRunner.Name}}</td>
<td>{{.JobRunner.Status}}</td>
<td>{{if not .Prev.IsZero}}{{.Prev.Format "2006-01-02 15:04:05"}}{{end}}</td>
<td>{{if not .Next.IsZero}}{{.Next.Format "2006-01-02 15:04:05"}}{{end}}</td>
<td>{{.JobRunner.Latency}}</td>
</tr>
{{end}}
</table>
</body>
`
var statusTmpl *template.Template = nil
func init() {
if statusTmpl == nil {
statusTmpl, _ = template.New("status").Parse(statusTmplText)
}
}
func newTestScheduler(conf *testSchedulerConf) *testScheduler {
return &testScheduler{
conf: conf,
}
}
func (ts *testScheduler) Start(job scheduledJob) {
jobrunner.Start()
jobrunner.Now(job)
jobrunner.Schedule(ts.conf.Schedule, job)
http.HandleFunc("/status", ts.statusHandler)
go http.ListenAndServe(ts.conf.WebConsoleEndPoint, nil)
}
func (ts *testScheduler) statusHandler(w http.ResponseWriter, r *http.Request) {
statusTmpl.Execute(w, jobrunner.StatusPage())
}