summaryrefslogtreecommitdiff
path: root/scheduler/scheduler.go
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-04-21 21:14:58 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-04-21 21:14:58 -0700
commit9d96d1a9422b30dd6caa42cca2a561970735f160 (patch)
tree3a548002293bf8a79514134437f718484a10193b /scheduler/scheduler.go
parentbef72712c271e7628342521985d68f418dcf0343 (diff)
downloadbackup-notify-9d96d1a9422b30dd6caa42cca2a561970735f160.tar.gz
backup-notify-9d96d1a9422b30dd6caa42cca2a561970735f160.zip
notify when we don't receive a thing in the last 24 hours
Diffstat (limited to 'scheduler/scheduler.go')
-rw-r--r--scheduler/scheduler.go51
1 files changed, 49 insertions, 2 deletions
diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go
index 3ade349..43b7abd 100644
--- a/scheduler/scheduler.go
+++ b/scheduler/scheduler.go
@@ -2,16 +2,63 @@ 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 StartScheduler(dbConn *sql.DB) {
+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() {
- database.DeleteOldBackups(dbConn, 31)
+ 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()
}