-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
310 lines (259 loc) · 8.29 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
package main // import "fknsrs.biz/p/jobserver/cmd/jobserverd"
import (
"bytes"
"database/sql"
"fmt"
"net"
"os"
"time"
"fknsrs.biz/p/jobserver/internal/protocol"
"github.com/Sirupsen/logrus"
_ "github.com/mattn/go-sqlite3"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
createTableQuery = `create table if not exists "jobs" ("id" text primary key, "queue" text not null, "priority" float not null, "hold_until" integer not null, "ttr" integer, "content" text not null)`
fetchJobQuery = `select "queue", "priority", "hold_until", "ttr", "content" from "jobs" where "id" = ?`
putJobQuery = `insert into "jobs" ("id", "queue", "priority", "hold_until", "ttr", "content") values (?, ?, ?, ?, ?, ?)`
getTopJobQuery = `select "id", "queue", "priority", "hold_until", "ttr", "content" from "jobs" where "queue" = ? and "hold_until" < ? order by "priority" desc limit 1`
reserveJobQuery = `update "jobs" set "hold_until" = ? + "ttr" where "id" = ?`
updateJobQuery = `update "jobs" set "priority" = ?, "hold_until" = ?, "ttr" = ? where "id" = ?`
deleteJobQuery = `delete from "jobs" where "queue" = ? and "id" = ?`
listQueuesQuery = `select distinct "queue" from "jobs"`
queueStatsQuery = `select "queue", count(1) as "count" from "jobs" group by "queue"`
)
func withTx(db *sql.DB, f func(tx *sql.Tx) error) error {
tx, terr := db.Begin()
if terr != nil {
return terr
}
done := false
defer func() {
if !done {
tx.Rollback()
}
}()
if err := f(tx); err != nil {
rerr := tx.Rollback()
done = true
return rerr
}
cerr := tx.Commit()
done = true
return cerr
}
func maybePanic(err error) {
if err != nil {
panic(err)
}
}
var (
app = kingpin.New("jobserverd", "Job server using SQLite as a backend.")
dbPath = app.Flag("db_path", "Path to SQLite database.").Default("jobs.db").Envar("DB_PATH").String()
addr = app.Flag("addr", "Address to listen on.").Default(":2097").Envar("ADDR").String()
logLevel = app.Flag("log_level", "Log level").Default("info").Envar("LOG_LEVEL").Enum("debug", "info", "warn", "error")
)
func main() {
kingpin.MustParse(app.Parse(os.Args[1:]))
ll, lerr := logrus.ParseLevel(*logLevel)
if lerr != nil {
panic(lerr)
}
logrus.SetLevel(ll)
logrus.WithFields(logrus.Fields{
"db_path": *dbPath,
"addr": *addr,
"log_level": *logLevel,
}).Info("starting up")
logrus.WithField("db_path", *dbPath).Debug("opening database")
db, dberr := sql.Open("sqlite3", *dbPath)
if dberr != nil {
panic(dberr)
}
defer db.Close()
logrus.Debug("opened database")
logrus.Debug("ensuring tables exist")
if _, err := db.Exec(createTableQuery); err != nil {
panic(err)
}
logrus.Debug("tables created")
logrus.Debug("opening listening socket")
s, serr := net.ListenPacket("udp4", *addr)
if serr != nil {
panic(serr)
}
logrus.Info("listening")
snum := 1
for {
logrus.Debug("waiting for incoming message")
b := make([]byte, protocol.MessageSize)
n, r, err := s.ReadFrom(b)
if err != nil {
panic(err)
}
before := time.Now()
mnum := snum
snum++
l := logrus.WithField("seq", mnum)
l.WithFields(logrus.Fields{
"size": n,
"remote": r.String(),
}).Debug("got message")
func() {
defer func() {
l := l.WithField("measure#duration", time.Now().Sub(before).Seconds()*1000)
if e := recover(); e != nil {
if err, ok := e.(error); ok {
l.WithField("error", err.Error()).Error("error processing message")
} else {
l.WithField("error", e).Error("error processing message")
}
} else {
l.Debug("processed message successfully")
}
}()
m, err := protocol.Parse(bytes.TrimSpace(b[0:n]))
if err != nil {
panic(err)
}
l = l.WithField("message_key", m.GetKey())
l.WithField("message_type", fmt.Sprintf("%T", m)).Debug("processing message")
switch m := m.(type) {
case *protocol.PingMessage:
if _, err := s.WriteTo(m.Serialise(), r); err != nil {
panic(err)
}
case *protocol.JobMessage:
maybePanic(withTx(db, func(tx *sql.Tx) error {
if m.HoldUntil == 0 {
m.HoldUntil = time.Now().Unix()
}
if m.TTR == 0 {
m.TTR = uint64(time.Hour / time.Second)
}
var queue, content string
var priority float64
var holdUntil int64
var ttr uint64
var found bool
if err := tx.QueryRow(fetchJobQuery, m.ID).Scan(&queue, &priority, &holdUntil, &ttr, &content); err != nil && err != sql.ErrNoRows {
return err
} else if err == nil {
found = true
}
if found == false {
if _, err := tx.Exec(putJobQuery, m.ID, m.Queue, m.Priority, m.HoldUntil, m.TTR, m.Content); err != nil {
return err
}
d := protocol.Serialise(&protocol.SuccessMessage{Key: m.Key})
if _, err := s.WriteTo(d, r); err != nil {
return err
}
l.WithFields(logrus.Fields{
"queue": m.Queue,
"job_id": m.ID,
"measure#duration_ms": time.Now().Sub(before).Seconds() * 1000,
}).Info("created job")
} else {
if m.HoldUntil > holdUntil {
m.HoldUntil = holdUntil
}
if _, err := tx.Exec(updateJobQuery, m.Priority, m.HoldUntil, m.TTR, m.ID); err != nil {
return err
}
d := protocol.Serialise(&protocol.SuccessMessage{Key: m.Key})
if _, err := s.WriteTo(d, r); err != nil {
return err
}
l.WithFields(logrus.Fields{
"queue": m.Queue,
"job_id": m.ID,
"measure#duration_ms": time.Now().Sub(before).Seconds() * 1000,
}).Info("updated job")
}
return nil
}))
case *protocol.ReserveMessage:
maybePanic(withTx(db, func(tx *sql.Tx) error {
var id, queue, content string
var priority float64
var holdUntil int64
var ttr uint64
if err := tx.QueryRow(getTopJobQuery, m.Queue, time.Now().Unix()).Scan(&id, &queue, &priority, &holdUntil, &ttr, &content); err != nil {
if err == sql.ErrNoRows {
d := protocol.Serialise(&protocol.ErrorMessage{Key: m.Key, Reason: "empty"})
if _, werr := s.WriteTo(d, r); werr != nil {
return werr
}
return nil
}
return err
}
if _, err := tx.Exec(reserveJobQuery, time.Now().Unix(), id); err != nil {
return err
}
d := protocol.Serialise(&protocol.JobMessage{Key: m.Key, ID: id, Queue: queue, Priority: priority, HoldUntil: holdUntil, TTR: ttr, Content: content})
if _, err := s.WriteTo(d, r); err != nil {
return err
}
l.WithFields(logrus.Fields{
"queue": m.Queue,
"job_id": id,
"measure#duration_ms": time.Now().Sub(before).Seconds() * 1000,
}).Info("dispatched job")
return nil
}))
case *protocol.PeekMessage:
maybePanic(withTx(db, func(tx *sql.Tx) error {
var id, queue, content string
var priority float64
var holdUntil int64
var ttr uint64
if err := tx.QueryRow(getTopJobQuery, m.Queue, time.Now().Unix()).Scan(&id, &queue, &priority, &holdUntil, &ttr, &content); err != nil {
if err == sql.ErrNoRows {
d := protocol.Serialise(&protocol.ErrorMessage{Key: m.Key, Reason: "empty"})
if _, werr := s.WriteTo(d, r); werr != nil {
return werr
}
return nil
}
return err
}
d := protocol.Serialise(&protocol.JobMessage{Key: m.Key, ID: id, Queue: queue, Priority: priority, HoldUntil: holdUntil, TTR: ttr, Content: content})
if _, err := s.WriteTo(d, r); err != nil {
return err
}
return nil
}))
case *protocol.DeleteMessage:
maybePanic(withTx(db, func(tx *sql.Tx) error {
qr, err := db.Exec(deleteJobQuery, m.Queue, m.ID)
if err != nil {
return err
}
n, err := qr.RowsAffected()
if err != nil {
return err
}
if n == 0 {
d := protocol.Serialise(&protocol.ErrorMessage{Key: m.Key, Reason: "not found"})
if _, err := s.WriteTo(d, r); err != nil {
return err
}
} else {
d := protocol.Serialise(&protocol.SuccessMessage{Key: m.Key})
if _, err := s.WriteTo(d, r); err != nil {
return err
}
}
l.WithFields(logrus.Fields{
"queue": m.Queue,
"job_id": m.ID,
"measure#duration_ms": time.Now().Sub(before).Seconds() * 1000,
}).Info("deleted job")
return nil
}))
}
}()
}
}