Skip to content

Commit

Permalink
Clean up error handling when a backup is already running
Browse files Browse the repository at this point in the history
When a backup is running, a somewhat cryptic error was displayed,
and a mail message was sent as a failure. However, this can be a
normal situation depending on scheduling of backups.

The backup log now has: "Backup already running and will be skipped",
and the email subject ends with "(skipped)" rather than "(FAILURE)",
making it easier to filter on the messages if desired.

This fixes issue #17.
  • Loading branch information
jeffaco committed Sep 20, 2018
1 parent d117b7a commit a43771e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ E-Mail subjects from `duplicacy-util` will be of the following format:
| --------------- | -------------------------------------------------------------------------- |
| Success | `duplicacy-util: Backup results for configuration <config-name> (success)` |
| Failure | `duplicacy-util: Backup results for configuration <config-name> (FAILURE)` |
| Already running | `duplicacy-util: Backup results for configuration <config-name> (skipped)` |
| Start | `duplicacy-util: Backup started for configuration <config-name>` |

You can filter on the subject line to direct the E-Mail appropriately
Expand Down
16 changes: 12 additions & 4 deletions duplicacy-util.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,17 @@ func main() {

returnStatus, err := processArguments()
if err != nil {
// Notify that the backup process has failed
logError(nil, fmt.Sprintf("Error: %s", err))
notifyOfFailure()
switch returnStatus {
case 6200:
// Notify that the backup process has been skipped
logError(nil, fmt.Sprintf("Warning: %s", err))
notifyOfFailure(fmt.Sprintf("duplicacy-util: Backup results for configuration %s (skipped)", cmdConfig))

default:
// Notify that the backup process has failed
logError(nil, fmt.Sprintf("Error: %s", err))
notifyOfFailure("")
}
}

os.Exit(returnStatus)
Expand Down Expand Up @@ -230,7 +238,7 @@ func obtainLock() (int, error) {

if !locked {
// do not have exclusive lock
return 200, errors.New("Unable to obtain lock using lockfile: " + lockfile)
return 6200, errors.New("Backup already running and will be skipped")
}

// flock doesn't remove the lock file when done, so let's do it ourselves
Expand Down
7 changes: 5 additions & 2 deletions emailNotifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ func (notifier EmailNotifier) NotifyOfSuccess() error {
}

// NotifyOfFailure is triggered when a failure occurred during backup
func (notifier EmailNotifier) NotifyOfFailure() error {
subject := fmt.Sprintf("duplicacy-util: Backup results for configuration %s (FAILURE)", cmdConfig)
func (notifier EmailNotifier) NotifyOfFailure(subject string) error {
if len(subject) == 0 {
subject = fmt.Sprintf("duplicacy-util: Backup results for configuration %s (FAILURE)", cmdConfig)
}

return notifier.email(subject, htmlGenerateBody(), mailBody)
}

Expand Down
2 changes: 1 addition & 1 deletion notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ package main
type Notifier interface {
NotifyOfStart() error
NotifyOfSuccess() error
NotifyOfFailure() error
NotifyOfFailure(string) error
}
9 changes: 6 additions & 3 deletions notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ func notifyOfSuccess() {
}
}

func notifyOfFailure() {
// notifyOfFailure takes a subject argument:
// If zero length, subject will be chosen by default
// If specified, will override the default subject
func notifyOfFailure(subject string) {
for _, notifier := range onFailureNotifiers {
_ = notifier.NotifyOfFailure()
_ = notifier.NotifyOfFailure(subject)
}
}

Expand Down Expand Up @@ -72,7 +75,7 @@ func testNotifications() error {

notifyOfStart()
notifyOfSuccess()
notifyOfFailure()
notifyOfFailure("")

return nil
}

0 comments on commit a43771e

Please sign in to comment.