-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
56 lines (51 loc) · 1.04 KB
/
main.go
File metadata and controls
56 lines (51 loc) · 1.04 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
package main
import (
"log"
"os"
"time"
)
func main() {
log.SetOutput(os.Stdout)
log.Printf("[INFO] Starting service")
queue, exists := os.LookupEnv("AWS_SQS_QUEUE")
if !exists {
log.Printf("[ERROR] AWS_SQS_QUEUE must be set")
os.Exit(0)
}
if option, set := os.LookupEnv("SERVER"); set {
if option == "enabled" {
go ListenAndServe()
}
}
multiply := time.Duration(1)
for {
result, err := RelayHook(queue)
if err != nil {
serviceAvailable = false
log.Printf("[ERROR] %s", err)
}
serviceAvailable = true
if result {
multiply = time.Duration(1)
} else if multiply < 1024 {
multiply = multiply * 2
log.Printf("[INFO] Sleeping for %d seconds", multiply)
}
time.Sleep(time.Second * multiply)
}
}
func RelayHook(queue string) (bool, error) {
message, err := ReadHookFromSqs(queue)
if err != nil {
return false, err
}
if message == nil {
return false, nil
}
err = RelayRequestHook(message)
if err != nil {
return true, err
}
DeleteMessage(message.QueueUrl, message.ReceiptHandle)
return true, nil
}