-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (84 loc) · 2.16 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
cloudevents "github.com/cloudevents/sdk-go"
"github.com/graphql-services/go-saga/graphqlorm"
handler "github.com/jakubknejzlik/cloudevents-lambda-handler"
"github.com/novacloudcz/graphql-orm/events"
)
type ExportMeta struct {
AnswerIDs []string
RowNames []string
}
func receiver(e cloudevents.Event) error {
ctx := context.Background()
var ormEvent events.Event
err := e.DataAs(&ormEvent)
if err != nil {
return err
}
fmt.Println("event", ormEvent.Entity, ormEvent.Type, ormEvent.ChangedColumns())
ormURL := os.Getenv("GRAPHQL_ORM_URL")
if ormURL == "" {
return fmt.Errorf("Missing required GRAPHQL_ORM_URL envvar")
}
ormClient := graphqlorm.NewClient(ormURL)
err = updateExportState(ctx, ormClient, ormEvent, "PROCESSING", nil)
if err != nil {
return err
}
var metadata string
err = ormEvent.Change("metadata").NewValueAs(&metadata)
if err != nil {
return err
}
var meta ExportMeta
err = json.Unmarshal([]byte(metadata), &meta)
if err != nil {
return err
}
fileID, err := handleExport(ctx, ormClient, meta)
if err != nil {
fmt.Println("error processing", err)
err = updateExport(ctx, ormClient, ormEvent, map[string]string{
"state": "ERROR",
"errorDescription": err.Error(),
})
if err != nil {
return err
}
return nil
}
return updateExportState(ctx, ormClient, ormEvent, "COMPLETED", &fileID)
}
func updateExportState(ctx context.Context, c *graphqlorm.ORMClient, ormEvent events.Event, state string, fileID *string) (err error) {
input := map[string]string{
"state": state,
}
if fileID != nil {
input["fileId"] = *fileID
}
return updateExport(ctx, c, ormEvent, input)
}
func updateExport(ctx context.Context, c *graphqlorm.ORMClient, ormEvent events.Event, input map[string]string) (err error) {
_, err = c.UpdateEntity(ctx, graphqlorm.UpdateEntityOptions{
Entity: ormEvent.Entity,
EntityID: ormEvent.EntityID,
Input: input,
})
return
}
func main() {
h := handler.NewCloudEventsLambdaHandler(receiver)
h.Start()
}
func getenv(name, defaultValue string) string {
v := os.Getenv(name)
if v == "" {
v = defaultValue
}
return v
}