From 0def4747da9e086e0dc0e0a4d1759fdac4bd9f1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 24 Mar 2014 19:34:16 +0100 Subject: [PATCH] FEATURE: send the backup/restore logs when operation fails --- config/locales/server.en.yml | 22 ++++++++++++++++++++++ lib/export/exporter.rb | 25 +++++++++++++++++-------- lib/import/importer.rb | 33 +++++++++++++++++++++------------ 3 files changed, 60 insertions(+), 20 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index f21cc6bf5..29a1155ff 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1154,10 +1154,32 @@ en: subject_template: "Export completed successfully" text_body_template: "The export was successful." + export_failed: + subject_template: "Export failed" + text_body_template: | + The export has failed. + + Here's the log: + + ``` + %{logs} + ``` + import_succeeded: subject_template: "Import completed successfully" text_body_template: "The import was successful." + import_failed: + subject_template: "Import failed" + text_body_template: | + The import has failed. + + Here's the log: + + ``` + %{logs} + ``` + email_error_notification: subject_template: "Error parsing email" text_body_template: | diff --git a/lib/export/exporter.rb b/lib/export/exporter.rb index 4f3fba09b..0a3e87250 100644 --- a/lib/export/exporter.rb +++ b/lib/export/exporter.rb @@ -44,8 +44,6 @@ module Export after_create_hook remove_old - - notify_user rescue SystemExit log "Backup process was cancelled!" rescue Exception => ex @@ -55,6 +53,7 @@ module Export @success = true "#{@archive_basename}.tar.gz" ensure + notify_user clean_up @success ? log("[SUCCESS]") : log("[FAILED]") end @@ -79,6 +78,7 @@ module Export @meta_filename = File.join(@tmp_directory, BackupRestore::METADATA_FILE) @archive_directory = File.join(Rails.root, "public", "backups", @current_db) @archive_basename = File.join(@archive_directory, "#{SiteSetting.title.parameterize}-#{@timestamp}") + @logs = [] end def listen_for_shutdown_signal @@ -256,17 +256,21 @@ module Export backup.after_create_hook end - def notify_user - log "Notifying '#{@user.username}' of the success of the backup..." - # NOTE: will only notify if @user != Discourse.site_contact_user - SystemMessage.create(@user, :export_succeeded) - end - def remove_old log "Removing old backups..." Backup.remove_old end + def notify_user + log "Notifying '#{@user.username}' of the end of the backup..." + # NOTE: will only notify if @user != Discourse.site_contact_user + if @success + SystemMessage.create(@user, :export_succeeded) + else + SystemMessage.create(@user, :export_failed, logs: @logs.join("\n")) + end + end + def clean_up log "Cleaning stuff up..." remove_tmp_directory @@ -306,6 +310,7 @@ module Export def log(message) puts(message) rescue nil publish_log(message) rescue nil + save_log(message) end def publish_log(message) @@ -314,6 +319,10 @@ module Export MessageBus.publish(BackupRestore::LOGS_CHANNEL, data, user_ids: [@user_id]) end + def save_log(message) + @logs << "[#{Time.now}] #{message}" + end + end end diff --git a/lib/import/importer.rb b/lib/import/importer.rb index 1eb509b2f..652c682f1 100644 --- a/lib/import/importer.rb +++ b/lib/import/importer.rb @@ -48,8 +48,6 @@ module Import reconnect_database extract_uploads - - notify_user rescue SystemExit log "Restore process was cancelled!" rollback @@ -60,6 +58,7 @@ module Import else @success = true ensure + notify_user clean_up @success ? log("[SUCCESS]") : log("[FAILED]") end @@ -95,6 +94,7 @@ module Import @tar_filename = @archive_filename[0...-3] @meta_filename = File.join(@tmp_directory, BackupRestore::METADATA_FILE) @dump_filename = File.join(@tmp_directory, BackupRestore::DUMP_FILE) + @logs << [] end def listen_for_shutdown_signal @@ -256,16 +256,6 @@ module Import end end - def notify_user - if user = User.where(email: @user_info[:email]).first - log "Notifying '#{user.username}' of the success of the restore..." - # NOTE: will only notify if user != Discourse.site_contact_user - SystemMessage.create(user, :import_succeeded) - else - log "Could not send notification to '#{@user_info[:username]}' (#{@user_info[:email]}), because the user does not exists..." - end - end - def rollback log "Trying to rollback..." if BackupRestore.can_rollback? @@ -276,6 +266,20 @@ module Import end end + def notify_user + if user = User.where(email: @user_info[:email]).first + log "Notifying '#{user.username}' of the end of the restore..." + # NOTE: will only notify if user != Discourse.site_contact_user + if @success + SystemMessage.create(user, :import_succeeded) + else + SystemMessage.create(user, :import_failed, logs: @logs.join("\n")) + end + else + log "Could not send notification to '#{@user_info[:username]}' (#{@user_info[:email]}), because the user does not exists..." + end + end + def clean_up log "Cleaning stuff up..." remove_tmp_directory @@ -315,6 +319,7 @@ module Import def log(message) puts(message) rescue nil publish_log(message) rescue nil + save_log(message) end def publish_log(message) @@ -323,6 +328,10 @@ module Import MessageBus.publish(BackupRestore::LOGS_CHANNEL, data, user_ids: [@user_id]) end + def save_log(message) + @logs << "[#{Time.now}] #{message}" + end + end end