backups: Show notification on error during scheduled backups

- Keep a count of number of consecutive failures. Reset the count on a
successful backup.

- Keep the count for each repository.

Tests performed:

- Insert an exception into method for running a scheduled backup.

- When the FreedomBox is run in develop mode, it attempts to take backups every
3 minutes.

- It fails and shows the notification. Text is good. The error count is 1 and
error text is same as exception text. Go to backups button redirects to backup
page.

- In another 3 minutes another attempt is made and count increments this time.

- If the notification is dismissed, after another attempt is made, notification
comes back with incremented count.

- When the exception is removed and FreedomBox is restarted, schedule backup
succeeds and notification is dismissed automatically.

- When backup is deleted and exception is reintroduced, the notification comes
back with count reset to 1.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2021-01-14 17:44:26 -08:00 committed by James Valleroy
parent d4e9e7a965
commit a022eed0eb
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -117,9 +117,11 @@ def backup_by_schedule(data):
for repository in repository_module.get_repositories():
try:
repository.schedule.run_schedule()
_show_schedule_error_notification(repository, is_error=False)
except Exception as exception:
logger.exception('Error running scheduled backup: %s', exception)
# XXX: Create a notification
_show_schedule_error_notification(repository, is_error=True,
exception=exception)
def get_exported_archive_apps(path):
@ -224,3 +226,38 @@ def on_schedule_save(repository):
note.dismiss()
except KeyError:
pass
def _show_schedule_error_notification(repository, is_error, exception=None):
"""Show or hide a notification related scheduled backup operation."""
from plinth.notification import Notification
id_ = 'backups-schedule-error-' + repository.uuid
try:
note = Notification.get(id_)
error_count = note.data['error_count']
except KeyError:
error_count = 0
message = ugettext_noop(
'A scheduled backup failed. Past {error_count} attempts for backup '
'did not succeed. The latest error is: {error_message}')
data = {
'app_name': 'translate:' + ugettext_noop('Backups'),
'app_icon': 'fa-files-o',
'error_count': error_count + 1 if is_error else 0,
'error_message': str(exception)
}
title = ugettext_noop('Error During Backup')
actions_ = [{
'type': 'link',
'class': 'primary',
'text': ugettext_noop('Go to {app_name}'),
'url': 'backups:index'
}, {
'type': 'dismiss'
}]
note = Notification.update_or_create(id=id_, app_id='backups',
severity='error', title=title,
message=message, actions=actions_,
data=data, group='admin')
note.dismiss(should_dismiss=not is_error)