-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
67 lines (60 loc) · 1.88 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
package aws
import (
"context"
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"github.com/hill-daniel/drizzle"
"github.com/pkg/errors"
"io/ioutil"
"log"
"os"
)
const (
tmpDirPattern = "_drizzle"
)
// LambdaHandler implements an AWS Lambda handler for an incoming SQSEvent Message.
type LambdaHandler struct {
job drizzle.Builder
workDir string
}
// NewLambdaHandler creates a new LambdaHandler.
func NewLambdaHandler(job drizzle.Builder, workDir string) *LambdaHandler {
return &LambdaHandler{
job: job,
workDir: workDir,
}
}
// Handle handles incoming SQSEvents.
func (lh *LambdaHandler) Handle(ctx context.Context, sqsEvent *events.SQSEvent) error {
for _, message := range sqsEvent.Records {
repository := &drizzle.Repository{}
if err := json.Unmarshal([]byte(message.Body), repository); err != nil {
log.Println(errors.Wrap(err, "failed to unmarshal json from SQS event body"))
return nil
}
if err := lh.runPipeline(ctx, *repository); err != nil {
log.Println(errors.Wrapf(err, "failed to run pipeline for repository %q with id %s", repository.FullName, repository.ID))
return nil
}
// Do not return error, we want the sqs message to be processed, since to fix an error
// we have to commit stuff again anyway, which will trigger the pipeline again.
}
return nil
}
func (lh *LambdaHandler) runPipeline(ctx context.Context, repository drizzle.Repository) error {
pipelineDir, err := ioutil.TempDir(lh.workDir, tmpDirPattern)
if err != nil {
return errors.Wrapf(err, "failed to create temporary directory for pipeline")
}
defer func() {
err := os.RemoveAll(pipelineDir)
if err != nil {
log.Println(errors.Wrap(err, "failed to cleanup temporary pipeline directory"))
}
}()
if err = lh.job.Build(ctx, repository, pipelineDir); err != nil {
return errors.Wrapf(err, "failed to execute job")
}
log.Println("Pipeline executed!")
return nil
}