Skip to content

Commit

Permalink
refactor(do_backup): Move logs to helper functions & enhance messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejsika committed Oct 2, 2021
1 parent 8c910b5 commit 9af949f
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 48 deletions.
60 changes: 12 additions & 48 deletions do_backup/do_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,44 +47,26 @@ func DoBackup(configPath, extraName string) {

for _, b := range config.Backups {
// Backup source
log.Info().
Str("phase", "backup-start").
Str("id", b.ID).
Msg("Start backing up " + b.ID + " ...")
logBackupStart(b)
data, err := b.Source.Backup()
if err != nil {
bl.SaveEvent(b.ID, "---", err)
log.Warn().
Str("phase", "backup-finish-err").
Str("id", b.ID).
Msg("Backup failed " + b.ID + ": " + err.Error())
logBackupFailed(b, err)
continue
}
log.Info().
Str("phase", "backup-finish-ok").
Str("id", b.ID).
Msg("Finish backing up " + b.ID)
logBackupDone(b)

// Process Backup's Middlewares
var errBackupMiddleware error = nil
for _, m := range b.Middlewares {
log.Info().
Str("phase", "backup-middleware-start").
Str("id", b.ID).
Msg("Start backup middleware")
logBackupMiddlewareStart(b, m)
data, errBackupMiddleware = m.Process(data)
if errBackupMiddleware != nil {
bl.SaveEvent(b.ID, "---", errBackupMiddleware)
log.Warn().
Str("phase", "backup-middleware-err").
Str("id", b.ID).
Msg("Backup middleware failed: " + errBackupMiddleware.Error())
logBackupMiddlewareFailed(b, m, err)
continue
}
log.Info().
Str("phase", "backup-middleware-ok").
Str("id", b.ID).
Msg("Finish backup middleware " + b.ID)
logBackupMiddlewareDone(b, m)
}

if errBackupMiddleware != nil {
Expand All @@ -98,47 +80,29 @@ func DoBackup(configPath, extraName string) {
// Process Targets's Middlewares
var errTargetMiddleware error = nil
for _, m := range t.Middlewares {
log.Info().
Str("phase", "target-middleware-start").
Str("id", t.ID).
Msg("Start target middleware")
logTargetMiddlewareStart(b, t, m)
targetData, errTargetMiddleware = m.Process(targetData)
if errTargetMiddleware != nil {
bl.SaveEvent(b.ID, t.ID, errTargetMiddleware)
log.Warn().
Str("phase", "target-middleware-err").
Str("id", t.ID).
Msg("Target middleware failed: " + errTargetMiddleware.Error())
logTargetMiddlewareFailed(b, t, m, errTargetMiddleware)
continue
}
log.Info().
Str("phase", "target-middleware-ok").
Str("id", t.ID).
Msg("Finish target middleware " + t.ID)
logTargetMiddlewareDone(b, t, m)
}
if errTargetMiddleware != nil {
continue
}

// Save backup to target
log.Info().
Str("phase", "save-start").
Str("id", t.ID).
Msg("Start save " + t.ID)
logTargetStart(b, t)
err = t.Save(targetData)
if err != nil {
bl.SaveEvent(b.ID, t.ID, err)
log.Warn().
Str("phase", "save-err").
Str("id", t.ID).
Msg("Save " + t.ID + " failed: " + err.Error())
logTargetFailed(b, t, err)
continue
}
bl.SaveEvent(b.ID, t.ID, err)
log.Info().
Str("phase", "save-ok").
Str("id", t.ID).
Msg("Finish save " + t.ID)
logTargetDone(b, t)
}
}

Expand Down
118 changes: 118 additions & 0 deletions do_backup/log_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package do_backup

import (
"github.com/rs/zerolog/log"
"github.com/sikalabs/tergum/backup"
"github.com/sikalabs/tergum/backup/middleware"
"github.com/sikalabs/tergum/backup/target"
)

const PHASE_START = "START"
const PHASE_DONE = "DONE"
const PHASE_FAILED = "FAILED"

func getMiddlewareID(m middleware.Middleware) string {
if m.Gzip != nil {
return "gzip"
}
return ""
}

func metaLog(
b *backup.Backup,
t *target.Target,
m *middleware.Middleware,
method string,
phase string,
message string,
) {
backup_id := ""
scope := ""
if b != nil {
backup_id = b.ID
scope = backup_id
}
target_id := ""
if t != nil {
target_id = t.ID
scope = backup_id + "/" + target_id
}
middleware_id := ""
if m != nil {
middleware_id = getMiddlewareID(*m)
scope = scope + "+" + middleware_id
}

message_space := ""
if message != "" {
message_space = " "
}

log.Info().
Str("method", method).
Str("phase", phase).
Str("backup_id", backup_id).
Str("target_id", target_id).
Str("middleware_id", middleware_id).
Msg(phase + "/" + method + "(" + scope + ")" + message_space + message)
}

func logBackupStart(b backup.Backup) {
metaLog(&b, nil, nil, "BACKUP", PHASE_START,
"")
}

func logBackupDone(b backup.Backup) {
metaLog(&b, nil, nil, "BACKUP", PHASE_DONE,
"Backup "+b.ID+" finished.")
}

func logBackupFailed(b backup.Backup, err error) {
metaLog(&b, nil, nil, "BACKUP", PHASE_FAILED,
"Backup "+b.ID+" failed: "+err.Error())
}

func logTargetStart(b backup.Backup, t target.Target) {
metaLog(&b, &t, nil, "TARGET", PHASE_START,
"")
}

func logTargetDone(b backup.Backup, t target.Target) {
metaLog(&b, &t, nil, "TARGET", PHASE_DONE,
"Target "+b.ID+" finished.")
}

func logTargetFailed(b backup.Backup, t target.Target, err error) {
metaLog(&b, &t, nil, "TARGET", PHASE_FAILED,
"Backup "+b.ID+" failed: "+err.Error())
}

func logTargetMiddlewareStart(b backup.Backup, t target.Target, m middleware.Middleware) {
metaLog(&b, &t, &m, "TARGET_MIDDLEWARE", PHASE_START,
"")
}

func logTargetMiddlewareDone(b backup.Backup, t target.Target, m middleware.Middleware) {
metaLog(&b, &t, &m, "TARGET_MIDDLEWARE", PHASE_DONE,
"")
}

func logTargetMiddlewareFailed(b backup.Backup, t target.Target, m middleware.Middleware, err error) {
metaLog(&b, &t, &m, "TARGET_MIDDLEWARE", PHASE_FAILED,
"Backup "+b.ID+" failed: "+err.Error())
}

func logBackupMiddlewareStart(b backup.Backup, m middleware.Middleware) {
metaLog(&b, nil, &m, "BACKUP_MIDDLEWARE", PHASE_START,
"")
}

func logBackupMiddlewareDone(b backup.Backup, m middleware.Middleware) {
metaLog(&b, nil, &m, "BACKUP_MIDDLEWARE", PHASE_DONE,
"")
}

func logBackupMiddlewareFailed(b backup.Backup, m middleware.Middleware, err error) {
metaLog(&b, nil, &m, "BACKUP_MIDDLEWARE", PHASE_FAILED,
"Backup "+b.ID+" failed: "+err.Error())
}

0 comments on commit 9af949f

Please sign in to comment.