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
|
package scheduler
import (
"database/sql"
"log"
"strings"
"time"
"git.simponic.xyz/simponic/backup-notify/args"
"git.simponic.xyz/simponic/backup-notify/database"
"git.simponic.xyz/simponic/backup-notify/ntfy"
"github.com/go-co-op/gocron"
)
func contains(hostnames []string, hostname string) bool {
for _, h := range hostnames {
if h == hostname {
return true
}
}
return false
}
func StartScheduler(dbConn *sql.DB, argv *args.Arguments) {
scheduler := gocron.NewScheduler(time.Local)
scheduler.Every(1).Hour().Do(func() {
err := database.DeleteOldBackups(dbConn, 31)
if err != nil {
log.Println("could not delete old backups:", err)
}
})
scheduler.Every(1).Day().Do(func() {
hostnames, err := database.BackupedHostnamesInTheLastDay(dbConn)
if err != nil {
log.Println("could not list hostnames that have been backed up in the last day:", err)
}
allHostnames, err := database.ReceivedHostnames(dbConn)
if err != nil {
log.Println("could not list received hostnames:", err)
}
failedHostnames := []string{}
for _, hostname := range allHostnames {
if !contains(hostnames, hostname) {
failedHostnames = append(failedHostnames, hostname)
}
}
if len(failedHostnames) > 0 {
msg := "the following hostnames have not been backed up in the last day:\n" + strings.Join(failedHostnames, ", ")
log.Println(msg)
err := ntfy.SendMessage(msg, argv.NtfyEndpoint, argv.NtfyAlertTopics)
if err != nil {
log.Println("could not send alert:", err)
}
}
})
scheduler.StartAsync()
}
|