From a022eed0eb3901470813e828c3521efa681dd79b Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 14 Jan 2021 17:44:26 -0800 Subject: [PATCH] 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 Reviewed-by: James Valleroy --- plinth/modules/backups/__init__.py | 39 +++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/plinth/modules/backups/__init__.py b/plinth/modules/backups/__init__.py index e9143341e..f3e512bf0 100644 --- a/plinth/modules/backups/__init__.py +++ b/plinth/modules/backups/__init__.py @@ -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)