This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
/
main.go
79 lines (69 loc) · 1.86 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
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"time"
cloudevents "github.com/cloudevents/sdk-go/v1"
"github.com/kelseyhightower/envconfig"
)
func main() {
client, err := cloudevents.NewDefaultClient()
if err != nil {
panic(err)
}
r := Receiver{}
if err := envconfig.Process("", &r); err != nil {
panic(err)
}
fmt.Printf("Subject to match: %q.\n", r.Subject)
// Create a timer
duration, _ := strconv.Atoi(r.Time)
timer := time.NewTimer(time.Second * time.Duration(duration))
defer timer.Stop()
go func() {
<-timer.C
// Write the termination message if time out
fmt.Printf("time out to wait for event with subject %q.\n", r.Subject)
if err := r.writeTerminationMessage(map[string]interface{}{
"success": false,
}); err != nil {
fmt.Printf("failed to write termination message, %s.\n", err.Error())
}
os.Exit(0)
}()
if err := client.StartReceiver(context.Background(), r.Receive); err != nil {
log.Fatal(err)
}
}
type Receiver struct {
Subject string `envconfig:"SUBJECT" required:"true"`
Time string `envconfig:"TIME" required:"true"`
}
func (r *Receiver) Receive(event cloudevents.Event) {
eventSubject := event.Context.GetSubject()
fmt.Printf(event.Context.String())
if eventSubject == r.Subject {
fmt.Printf("subject matches, %q.\n", r.Subject)
// Write the termination message if the subject successfully matches
if err := r.writeTerminationMessage(map[string]interface{}{
"success": true,
}); err != nil {
fmt.Printf("failed to write termination message, %s.\n", err.Error())
}
os.Exit(0)
} else {
fmt.Printf("subject doesn't match, %q != %q.\n", eventSubject, r.Subject)
}
}
func (r *Receiver) writeTerminationMessage(result interface{}) error {
b, err := json.Marshal(result)
if err != nil {
return err
}
return ioutil.WriteFile("/dev/termination-log", b, 0644)
}