-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
177 lines (146 loc) · 4.05 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
bytes2 "bytes"
"container/ring"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
)
const TailedOutputBufferSize = 8
const RetryStartApplicationAfter = 3 * time.Second
const SlackWebHookEnvVarName = "FIDO_SLACK_WEBHOOK"
type ProcessExecutionReport struct {
Uptime time.Duration
ExitReason *ExitReason
TotalLinesOutputted int
TailedOutput [TailedOutputBufferSize]string
}
type SlackMessage struct {
Text string `json:"text"`
}
func main() {
args := os.Args[1:]
webHookUrl, isPresent := os.LookupEnv(SlackWebHookEnvVarName)
if !isPresent {
panic(fmt.Sprintf("Missing %s env, cannot report to Slack", SlackWebHookEnvVarName))
}
for {
slackStartupMessage := fmt.Sprintf(
"%s %s",
args[0],
strings.Join(args[1:], " "),
)
sendSlackMessage(
webHookUrl,
SlackMessage{Text: fmt.Sprintf("*Running Command: %s*", slackStartupMessage)},
)
result, err := watchProcess(args[0], args[1:])
if err != nil {
log.Printf("Failed to watch process: %v\n", err)
} else {
sendReportToSlack(
webHookUrl,
result,
RetryStartApplicationAfter,
)
}
log.Println("Waiting 3 seconds before restarting process")
time.Sleep(RetryStartApplicationAfter)
}
}
func watchProcess(cmd string, args []string) (*ProcessExecutionReport, error) {
outputBuf := ring.New(TailedOutputBufferSize)
totalLinesOutputted := 0
startTime := time.Now()
process := NewProcess(".", cmd, args)
err := process.Start()
if err != nil {
return nil, err
}
for output := range process.Output {
totalLinesOutputted++
outputBuf = outputBuf.Next()
outputBuf.Value = output.Message
// Print to stdout for context
fmt.Println(output.Message)
}
runDuration := time.Since(startTime)
report := createReport(
runDuration,
process.ExitReason,
totalLinesOutputted,
outputBuf,
)
return report, nil
}
func createReport(
runDuration time.Duration,
exitReason *ExitReason,
totalLinesOutputted int,
outputBuf *ring.Ring,
) *ProcessExecutionReport {
// Collect the last couple of lines for the report
outputTailLength := minInt(totalLinesOutputted, outputBuf.Len())
var tailedOutput [TailedOutputBufferSize]string
for i := 0; i < outputTailLength; i++ {
prevLine := outputBuf.Value
if prevLine == nil {
break
}
tailedOutput[i] = prevLine.(string)
outputBuf = outputBuf.Prev()
}
return &ProcessExecutionReport{
Uptime: runDuration,
ExitReason: exitReason,
TotalLinesOutputted: totalLinesOutputted,
TailedOutput: tailedOutput,
}
}
func sendReportToSlack(slackEndpoint string, results *ProcessExecutionReport, willRetryAfter time.Duration) {
var buffer bytes2.Buffer
if results.ExitReason != nil {
buffer.WriteString(fmt.Sprintf("Process terminated with exit code *%d* after *%s*\n", results.ExitReason.ExitCode, results.Uptime))
} else {
buffer.WriteString(fmt.Sprintf("Process terminated with unknown exit code *%s*\n", results.Uptime))
}
if results.TotalLinesOutputted != 0 {
buffer.WriteString("_Application output before termination:_\n")
buffer.WriteString("```")
if results.TotalLinesOutputted > TailedOutputBufferSize {
buffer.WriteString(fmt.Sprintf("... +%d Lines Truncated ...\n", results.TotalLinesOutputted-TailedOutputBufferSize))
}
for _, v := range results.TailedOutput {
if len(v) > 0 {
buffer.WriteString(v)
buffer.WriteRune('\n')
}
}
buffer.WriteString("```\n")
} else {
buffer.WriteString("Application terminated without any output.\n")
}
buffer.WriteString(fmt.Sprintf("I will try to restart the service in *%.0fs*\n", willRetryAfter.Seconds()))
msg := SlackMessage{
Text: buffer.String(),
}
sendSlackMessage(slackEndpoint, msg)
}
func sendSlackMessage(slackEndpoint string, msg SlackMessage) {
bytes, _ := json.Marshal(msg)
resp, _ := http.Post(slackEndpoint, "application/json", bytes2.NewBuffer(bytes))
defer resp.Body.Close()
r, _ := ioutil.ReadAll(resp.Body)
log.Printf("Slack response: %s\n", string(r))
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}