Skip to content

Commit b779606

Browse files
committed
added uptime alarms
1 parent da9e12a commit b779606

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

backup/backup.go

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,49 @@ package backup
22

33
import (
44
"context"
5+
"fmt"
56
"io"
67
"monodb-backup/notify"
78
"os"
89
"runtime"
910
"strconv"
1011
"strings"
12+
"sync"
1113
"time"
1214
)
1315

14-
// var didItWork bool = true // this variable is for determining if the app should send a notification after a failed backup to inform that it works now
15-
// func tWorksNow(message string, worked bool) {
16-
// oldOnlyOnError := params.Notify.Webhook.OnlyOnError
17-
// if !didItWork && worked && oldOnlyOnError {
18-
// params.Notify.Webhook.OnlyOnError = false
19-
// // notify.SendAlarm(message, false)
20-
// params.Notify.Webhook.OnlyOnError = oldOnlyOnError
21-
// }
22-
// didItWork = worked
23-
// }
16+
var (
17+
appStartTime time.Time
18+
currentDB string
19+
currentDBStartTime time.Time
20+
mu sync.Mutex // Mutex to protect access to currentDB and currentDBStartTime
21+
)
22+
23+
func init() {
24+
appStartTime = time.Now()
25+
}
26+
27+
func SendHourlyUptimeStatus() {
28+
mu.Lock()
29+
db := currentDB
30+
dbStart := currentDBStartTime
31+
mu.Unlock()
32+
33+
totalUptime := time.Since(appStartTime).Round(time.Second)
34+
message := fmt.Sprintf("Uptime: %s. ", totalUptime)
35+
36+
if db != "" {
37+
dbUptime := time.Since(dbStart).Round(time.Second)
38+
message += fmt.Sprintf("Currently backing up: %s (started %s ago).", db, dbUptime)
39+
} else {
40+
message += "Currently idle."
41+
}
42+
43+
logger.Info("Hourly Status: ", message)
44+
if totalUptime.Hours() > float64(params.Notify.UptimeStartLimit) {
45+
notify.SendAlarm(message, true)
46+
}
47+
}
2448

2549
func getDBList() (dbList []string) {
2650
switch params.Database {
@@ -66,7 +90,10 @@ func dumpDB(db string, dst string) (string, string, error) {
6690

6791
func Backup() {
6892
logger.Info("monodb-backup job started.")
69-
// notify.SendAlarm("Database backup started.", false)
93+
mu.Lock()
94+
currentDB = ""
95+
mu.Unlock()
96+
7097
streamable := (params.Database == "" || params.Database == "postgresql" || (params.Database == "mysql" && !params.BackupAsTables)) && params.ArchivePass == ""
7198

7299
dateNow = rightNow{
@@ -99,11 +126,23 @@ func Backup() {
99126

100127
if streamable && (params.BackupType.Type == "minio" || params.BackupType.Type == "s3") {
101128
for _, db := range params.Databases {
129+
mu.Lock()
130+
currentDB = db
131+
currentDBStartTime = time.Now()
132+
mu.Unlock()
102133
uploadWhileDumping(db)
103134
}
135+
mu.Lock()
136+
currentDB = ""
137+
mu.Unlock()
138+
logger.Info("monodb-backup streamable job finished.")
104139
return
105140
}
106141
for _, db := range params.Databases {
142+
mu.Lock()
143+
currentDB = db
144+
currentDBStartTime = time.Now()
145+
mu.Unlock()
107146
var dst string
108147
if runtime.GOOS == "windows" {
109148
dst = strings.TrimSuffix(params.BackupDestination, "/") + db
@@ -147,9 +186,13 @@ func Backup() {
147186
}
148187
}
149188
}
189+
mu.Lock()
190+
currentDB = ""
191+
mu.Unlock()
150192
if params.Database == "mssql" {
151193
mssqlDB.Close()
152194
}
195+
logger.Info("monodb-backup non-streamable job finished.")
153196
}
154197

155198
func uploadWhileDumping(db string) {

config/params.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ type Params struct {
2424
RunEveryCron string
2525
BackupType BackupType
2626
Notify struct {
27-
Email struct {
27+
UptimeAlarm bool
28+
UptimeStartLimit int
29+
Email struct {
2830
Enabled bool
2931
OnlyOnError bool
3032
InsecureSkipVerify bool

main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"monodb-backup/config"
99
"monodb-backup/notify"
1010
"runtime"
11+
"time"
1112

1213
"github.com/robfig/cron"
1314
)
@@ -40,6 +41,18 @@ func main() {
4041

4142
logger.Info("monodb-backup started.")
4243

44+
if config.Parameters.Notify.UptimeAlarm {
45+
ticker := time.NewTicker(1 * time.Hour)
46+
defer ticker.Stop()
47+
48+
go func() {
49+
for range ticker.C {
50+
backup.SendHourlyUptimeStatus()
51+
}
52+
}()
53+
54+
}
55+
4356
if config.Parameters.RunEveryCron != "" {
4457
c := cron.New()
4558
c.AddFunc(config.Parameters.RunEveryCron, initBackup)

0 commit comments

Comments
 (0)