-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
54 lines (51 loc) · 1 KB
/
main.go
File metadata and controls
54 lines (51 loc) · 1 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
package main
import (
"io"
"log"
"net/http"
"os"
"strconv"
"time"
)
func trigger(url string) {
// do post request and recover
defer func() {
if r := recover(); r != nil {
log.Println("Panic: ", r)
}
}()
resp, err := http.Post(url, "application/json", nil)
if err != nil {
log.Println("Error when do post request: ", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Error when read the response: ", err)
}
log.Println("Response: ", string(body))
}
func main() {
if len(os.Args) != 3 {
panic("Usage: ./cloudapi-daemon-trigger <url> <interval>")
}
url, interval := os.Args[1], os.Args[2]
intervalInt, err := strconv.ParseFloat(interval, 64)
if err != nil {
panic("Interval must be a number")
}
ticker := time.NewTicker(time.Duration(intervalInt*1000) * time.Millisecond)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
trigger(url)
case <-quit:
ticker.Stop()
return
}
}
}()
<-quit
}