-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask_handler.go
93 lines (77 loc) · 2.26 KB
/
task_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
81
82
83
84
85
86
87
88
89
90
91
92
93
package mapper
import (
"net/http"
"github.com/captaincodeman/datastore-locker"
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
)
type (
// interface that all our entities implement
taskEntity interface {
locker.Lockable
getCommon() *common
}
// childTask is any non-job task that needs to check if job is still active
childTask interface {
jobID() string
setJob(job *job)
}
// taskHandler is a custom handler type to avoid repetition
taskHandler func(c context.Context, config Config, key *datastore.Key, entity taskEntity) error
)
func jobFactory() locker.Lockable {
return new(job)
}
func iteratorFactory() locker.Lockable {
return new(iterator)
}
func namespaceFactory() locker.Lockable {
return new(namespace)
}
func shardFactory() locker.Lockable {
return new(shard)
}
// convert the locker.TaskHandler
func (m *mapper) handlerAdapter(handler taskHandler, factory locker.EntityFactory) http.Handler {
h := func(c context.Context, r *http.Request, key *datastore.Key, entity locker.Lockable) error {
tentity := entity.(taskEntity)
common := tentity.getCommon()
common.id = key.StringID()
// determine if we're a job task (so have already loaded the job)
// or a sub task (in which case we need to load it) so that we can
// abort if the flag has been set or set the job so that the handler
// can access the jobSpec and Query (should we pass though through?)
var j *job
child, isChild := entity.(childTask)
if isChild {
jobID := child.jobID()
if m.config.LogVerbose {
log.Infof(c, "owning job %s", jobID)
}
key := datastore.NewKey(c, m.config.DatastorePrefix+jobKind, jobID, 0, nil)
j = new(job)
if err := datastore.Get(c, key, j); err != nil {
// we need the job so error if we couldn't load it
return err
}
common.job = j
} else {
j = entity.(*job)
}
if j.Abort {
// abort means we don't process anything
// the task is marked complete and drains
return nil
}
// call the actual handler
if m.config.LogVerbose {
log.Infof(c, "calling handler")
}
return handler(c, *m.config, key, tentity)
}
fn := func(w http.ResponseWriter, r *http.Request) {
m.locker.Handle(h, factory).ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}