forked from titanous/beam
/
worker.go
200 lines (180 loc) · 5.22 KB
/
worker.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
package beam
import (
"fmt"
"github.com/garyburd/redigo/redis"
"net"
"os"
"path"
)
type Connector interface {
Connect() (net.Conn, error)
}
type Worker struct {
pool *redis.Pool
Prefix string // The prefix for all redis keys
handlers map[string]JobHandler
}
// NewWorker initializes a new beam worker.
func NewWorker(transport Connector, prefix string) *Worker {
return &Worker{
pool: newConnectionPool(transport, 10),
Prefix: prefix,
handlers: make(map[string]JobHandler),
}
}
// RegisterJob exposes the function <h> as a remote job to be invoked by clients
// under the name <name>.
func (w *Worker) RegisterJob(name string, h JobHandler) {
w.handlers[name] = h
}
// ServeJob is the server's default job handler. It is called every time a new job is created.
// It looks up a handler registered at <name>, and calls it with the same arguments. If no handler
// is registered, it returns an error.
func (w *Worker) ServeJob(name string, args []string, env map[string]string, streams Streamer, db DB) (err error) {
defer func() {
Debugf("Job returned: %s(%s) = %s", name, args, err)
}()
h, exists := w.handlers[name]
if !exists {
return fmt.Errorf("No such job: %s", name)
}
Debugf("Calling job: %s(%s)", name, args)
err = h(name, args, env, streams, db)
return
}
// A JobHandler is a function which can be invoked as a job by beam clients.
// The API for invoking jobs resembles that of unix processes:
// - A job is invoked under a certain <name>.
// - It may receive arguments as a string array (<args>).
// - It may receive an environment as a map of key-value pairs (<env>).
// - It may read from, and write to, streams of binary data. (<streams>).
// - It returns value which can either indicate "success" or a variety of error conditions.
//
// Additionally, a job may modify the server's database, which is shared with all other jobs.
// This is similar to how multiple unix processes share access to the same filesystem.
//
type JobHandler func(name string, args []string, env map[string]string, streams Streamer, db DB) error
// Work runs an infinite loop, watching its database for new requests, starting job as requested,
// moving stream data back and forth, and updating job status as it changes.
func (w *Worker) Work() error {
conn := w.pool.Get()
defer conn.Close()
for {
Debugf("Waiting for job")
// Get the list of current jobs
// Wait for next start event
vals, err := redis.Values(conn.Do("BLPOP", w.KeyPath("start"), "0"))
if err != nil {
return err
}
var id string
if _, err := redis.Scan(vals[1:], &id); err != nil {
return err
}
Debugf("Received instruction to start job %s", id)
// Acquire lock on the job
acquired, err := redis.Bool(conn.Do("SETNX", w.KeyPath(id), "me"))
if err != nil {
return err
}
Debugf("Acquiring lock for job %s... -> %s", id, acquired)
// FIXME: set a dead man's switch with TTL & a periodic refresh
if acquired {
Debugf("Spawning goroutine for job %s", id)
go func(id string) {
if err := w.startJob(id); err != nil {
fmt.Fprintf(os.Stderr, "Error starting job %s: %s\n", id, err)
}
}(id)
}
}
}
// startJob is called in its own goroutine for each job acquired by the worker.
func (w *Worker) startJob(id string) error {
conn := w.pool.Get()
Debugf("Connected")
defer conn.Close()
// Get job name
name, err := redis.String(conn.Do("LINDEX", w.KeyPath(), id))
if err != nil {
return err
}
Debugf("Job name = %s", name)
// Get all arguments
argsVals, err := redis.Values(conn.Do("LRANGE", w.KeyPath(id, "args"), "0", "-1"))
if err != nil {
return err
}
args := make([]string, len(argsVals))
for i := range args {
if _, err := redis.Scan(argsVals[i:], &args[i]); err != nil {
return err
}
}
Debugf("Job arguments = %v", args)
// Get env
envVals, err := redis.Values(conn.Do("HGETALL", w.KeyPath(id, "env")))
if err != nil {
return err
}
env := make(map[string]string)
for len(envVals) > 0 {
var (
k, v string
)
envVals, err = redis.Scan(envVals, &k, &v)
if err != nil {
return err
}
env[k] = v
}
Debugf("Job env = %v", env)
streams := NewStreamer(w.pool, w.KeyPath(id, "streams", "in"), w.KeyPath(id, "streams", "out"))
// Start worker read event loop
go func() {
conn := w.pool.Get()
defer conn.Close()
for {
msg, err := popMessage(conn, streams.(*streamer).ReadKey)
if err != nil {
panic(err)
}
if msg.Id == "x" {
if err := streams.Close(); err != nil {
panic(err)
}
}
if msg.Id[0] == '-' {
if err := streams.CloseStream(msg.Id[1:]); err != nil {
panic(err)
}
}
if len(msg.Body) > 0 {
if err := streams.WriteMessage(msg); err != nil {
panic(err)
}
}
}
}()
err = w.ServeJob(name, args, env, streams, w)
Debugf("ServeJob complete")
var status string
if err == nil {
status = ""
} else {
status = err.Error()
}
// Set the status and notify the client
if _, err := conn.Do("SET", w.KeyPath(id, "status"), status); err != nil {
return err
}
Debugf("Sending job completed")
if _, err := conn.Do("RPUSH", w.KeyPath(id, "wait"), status); err != nil {
return err
}
return nil
}
func (w *Worker) KeyPath(parts ...string) string {
parts = append([]string{w.Prefix}, parts...)
return path.Join(parts...)
}