-
Notifications
You must be signed in to change notification settings - Fork 0
/
JobScheduler.go
116 lines (97 loc) · 2.15 KB
/
JobScheduler.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
// AlertScheduler.go
package graphitenotifier
import (
"html/template"
"net/http"
"github.com/bamzi/jobrunner"
)
type jobSchedulerConf struct {
Schedule string `json:"schedule"`
WebConsoleEndPoint string `json:"webConsoleEndPoint"`
}
type scheduledJob interface {
Schedule() string
Run()
}
type jobScheduler struct {
conf *jobSchedulerConf
}
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 newJobScheduler(conf *jobSchedulerConf) *jobScheduler {
return &jobScheduler{
conf: conf,
}
}
func (as *jobScheduler) Init() {
jobrunner.Start()
http.HandleFunc("/status", as.statusHandler)
go http.ListenAndServe(as.conf.WebConsoleEndPoint, nil)
}
func (as *jobScheduler) ScheduleJob(job scheduledJob) {
jobrunner.Now(job)
jobrunner.Schedule(job.Schedule(), job)
}
func (ts *jobScheduler) statusHandler(w http.ResponseWriter, r *http.Request) {
statusTmpl.Execute(w, jobrunner.StatusPage())
}