-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (42 loc) · 978 Bytes
/
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
package main
import (
"context"
"encoding/json"
"log"
"os"
"strconv"
"cloud.google.com/go/pubsub"
)
func main() {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, os.Getenv("SLOTH_JOB_NOTIFIER_PROJECT"))
if err != nil {
log.Fatalf("failed to create pubsub client: %s", err)
}
topic := client.Topic(os.Getenv("SLOTH_JOB_NOTIFIER_TOPIC"))
if len(os.Args) < 2 {
log.Fatal("missing job id")
}
id, err := strconv.Atoi(os.Args[1])
if err != nil {
log.Fatalf("failed to parse job id '%s': %s", os.Args[1], err)
}
msg, err := json.Marshal(map[string]any{
"id": id,
})
if err != nil {
log.Fatalf("failed to marshal message: %s", err)
}
res := topic.Publish(ctx, &pubsub.Message{
Attributes: map[string]string{
"event": "refresh",
"namespace": os.Getenv("SLOTH_JOB_NOTIFIER_NAMESPACE"),
},
Data: msg,
})
_, err = res.Get(ctx)
if err != nil {
log.Fatalf("failed to publish message: %s", err)
}
topic.Stop()
}