-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
123 lines (102 loc) · 2.63 KB
/
main.go
File metadata and controls
123 lines (102 loc) · 2.63 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"log/syslog"
"os"
"os/exec"
"text/template"
"cloud.google.com/go/pubsub"
)
var logger *log.Logger
func init() {
flag.String("f", "config.yaml", "path to config file")
}
func main() {
flag.Parse()
// Load config
config := loadConfig(flag.Lookup("f").Value.String())
// setup logger
switch config.Logging.Output {
case "syslog":
writer, err := syslog.New(syslog.LOG_INFO, "pubsub-execd")
if err != nil {
panic(err)
}
logger = log.New(writer, "", 0)
case "none":
logger = log.New(io.Discard, "", 0)
case "stderr":
logger = log.New(os.Stderr, "", log.LstdFlags)
case "stdout":
fallthrough
default:
logger = log.New(os.Stdout, "", log.LstdFlags)
}
ctx, _ := context.WithCancel(context.Background())
// run triggers
for _, trigger := range config.Triggers {
sub, err := subscribe(ctx, trigger.PubSub)
if err != nil {
panic(err)
}
go run(ctx, trigger.Run, sub)
}
// wait for termination signal
select {
case <-ctx.Done():
logger.Print("Terminating")
os.Exit(0)
}
}
func run(ctx context.Context, run RunConfig, sub *pubsub.Subscription) {
// set the max extension to the trigger timeout to avoid message re-delivery
sub.ReceiveSettings.MaxExtension = run.Timeout
// set the number of goroutines to the concurrency level
sub.ReceiveSettings.NumGoroutines = run.Concurrency
sub.ReceiveSettings.Synchronous = run.Concurrency == 1
// parser for args template
tmpl, err := template.New("args").Parse(run.Args.Expression)
if err != nil {
panic(err)
}
// register the handler to receive messages
sub.Receive(ctx, func(ctx context.Context, msg *pubsub.Message) {
// assume the message payload is a json object and parse it
var payload map[string]interface{}
err := json.Unmarshal(msg.Data, &payload)
if err != nil {
// ignore, payload wasn't a json object
payload = make(map[string]interface{})
}
// create input context for to parse the args template
input := struct {
Attributes map[string]string
Payload map[string]interface{}
}{
Attributes: msg.Attributes,
Payload: payload,
}
// parse the args template
args := new(bytes.Buffer)
tmpl.Execute(args, input)
logger.Print(fmt.Sprintf("Running command: %s %s", run.Exec, args.String()))
// run the command
cmd := exec.CommandContext(ctx, run.Exec, args.String())
cmd.Stdout = logger.Writer()
cmd.Stderr = logger.Writer()
err = cmd.Run()
if err != nil {
msg.Nack()
logger.Print("non-zero exit value for command", cmd, err)
return
}
logger.Printf("Command %s executed successfully", run.Exec)
msg.Ack()
})
}