/
handler.go
80 lines (68 loc) · 2.2 KB
/
handler.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
package locker
import (
"net/http"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
)
type (
// EntityFactory creates a new entity instance for use by the lock handler
EntityFactory func() Lockable
// TaskHandler is the signature of the task handler
TaskHandler func(c context.Context, r *http.Request, key *datastore.Key, entity Lockable) error
)
// Handle wraps a task handler with task / lock processing
func (l *Locker) Handle(handler TaskHandler, factory EntityFactory) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
// ensure request is a task request
if r.Method != "POST" || r.Header.Get("X-Appengine-TaskName") == "" {
log.Warningf(c, "non task request")
w.WriteHeader(http.StatusBadRequest)
return
}
// use the same queue name for any tasks scheduled by this handler
queue := r.Header.Get("X-Appengine-QueueName")
c = WithQueue(c, queue)
key, seq, err := l.Parse(c, r)
if err != nil {
log.Warningf(c, "parse failed: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
entity := factory()
err = l.Aquire(c, key, entity, seq)
if err != nil {
log.Warningf(c, "lock failed: %v", err)
// if we have a lock error, it provides the http response to use
if lerr, ok := err.(Error); ok {
w.WriteHeader(lerr.Response)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
return
}
// TODO: explore having handler return something to indicate
// if the task needs to continue with the next seq or be completed
err = handler(c, r, key, entity)
if err != nil {
log.Warningf(c, "handler failed: %v", err)
// clear the lock to allow the next retry
if err := l.clearLock(c, key, entity); err != nil {
log.Warningf(c, "clearLock failed: %v", err)
// if we have a lock error, it provides the http response to use
if lerr, ok := err.(Error); ok {
w.WriteHeader(lerr.Response)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
} else {
w.WriteHeader(http.StatusInternalServerError)
}
return
}
w.WriteHeader(http.StatusOK)
}
return http.HandlerFunc(fn)
}