-
Notifications
You must be signed in to change notification settings - Fork 31
/
job.go
81 lines (64 loc) · 1.71 KB
/
job.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
package job
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"github.com/aws/aws-lambda-go/events"
)
var jobs map[string]NewJob
// LogJobName is LogJob's unique identifier.
const LogJobName = "log"
// Job is the interfce implemented by jobs.
type Job interface {
Do(context.Context) (context.Context, error)
}
// NewJob is a function definition for functions that return initialized Jobs.
type NewJob = func() Job
// LogJob is a job that simply prints a log message.
type LogJob struct{}
// NewLogJob returns a LogJob.
func NewLogJob() Job {
return LogJob{}
}
// Do implements Job.Do by printing a log message.
func (j LogJob) Do(ctx context.Context) (context.Context, error) {
log.Println("job complete")
return ctx, nil
}
// Message models a job message sent to the queue.
type Message struct {
Job string `json:"job"`
}
// NewMessage returns an initialized Message.
func NewMessage(job string) Message {
return Message{Job: job}
}
// String implementes Stringer by marshaling the message to JSON.
func (m Message) String() string {
b, err := json.Marshal(m)
if err != nil {
panic(fmt.Errorf("error encoding message: %v", err))
}
return string(b)
}
// Do parses the SQS message and invokes Job.Do.
func Do(ctx context.Context, message events.SQSMessage) (context.Context, error) {
var m Message
if err := json.Unmarshal([]byte(message.Body), &m); err != nil {
return ctx, fmt.Errorf("error parsing message: %v", err)
}
if m.Job == "" {
return ctx, errors.New("message invalid: 'job' required")
}
newJob, ok := jobs[m.Job]
if !ok {
return ctx, fmt.Errorf("job invalid: %q", m.Job)
}
return newJob().Do(ctx)
}
func init() {
jobs = make(map[string]NewJob, 1)
jobs[LogJobName] = NewLogJob
}