-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
56 lines (43 loc) · 961 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
53
54
55
56
package main
import (
"bytes"
"github.com/formapro/crony/cfg"
"github.com/robfig/cron"
"log"
"net/http"
"os"
"os/signal"
"sync"
)
func main() {
var wg sync.WaitGroup
config := cfg.Load()
c := cron.New()
for _, task := range config.Tasks {
err := c.AddFunc(task.Cron, func() {
wg.Add(1)
defer wg.Done()
buf := bytes.NewBuffer([]byte{})
resp, err := http.Post(task.Url, "text/html", buf)
if err != nil {
log.Printf("Run %s %s %s - %s\n", task.Name, task.Cron, task.Url, err)
return
}
log.Printf("Run %s %s %s - response status %d\n", task.Name, task.Cron, task.Url, resp.StatusCode)
})
if err != nil {
log.Printf("Failed to add cron func for %s %s %s\n", task.Name, task.Cron, task.Url)
}
}
wg.Add(1)
go func() {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, os.Kill)
<-signals
c.Stop()
wg.Done()
}()
c.Start()
log.Println("Crony is working")
wg.Wait()
}