FEATURE: send the backup/restore logs when operation fails

This commit is contained in:
Régis Hanol 2014-03-24 19:34:16 +01:00
parent 9454ad1ef9
commit 0def4747da
3 changed files with 60 additions and 20 deletions

View file

@ -1154,10 +1154,32 @@ en:
subject_template: "Export completed successfully" subject_template: "Export completed successfully"
text_body_template: "The export was successful." 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: import_succeeded:
subject_template: "Import completed successfully" subject_template: "Import completed successfully"
text_body_template: "The import was successful." 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: email_error_notification:
subject_template: "Error parsing email" subject_template: "Error parsing email"
text_body_template: | text_body_template: |

View file

@ -44,8 +44,6 @@ module Export
after_create_hook after_create_hook
remove_old remove_old
notify_user
rescue SystemExit rescue SystemExit
log "Backup process was cancelled!" log "Backup process was cancelled!"
rescue Exception => ex rescue Exception => ex
@ -55,6 +53,7 @@ module Export
@success = true @success = true
"#{@archive_basename}.tar.gz" "#{@archive_basename}.tar.gz"
ensure ensure
notify_user
clean_up clean_up
@success ? log("[SUCCESS]") : log("[FAILED]") @success ? log("[SUCCESS]") : log("[FAILED]")
end end
@ -79,6 +78,7 @@ module Export
@meta_filename = File.join(@tmp_directory, BackupRestore::METADATA_FILE) @meta_filename = File.join(@tmp_directory, BackupRestore::METADATA_FILE)
@archive_directory = File.join(Rails.root, "public", "backups", @current_db) @archive_directory = File.join(Rails.root, "public", "backups", @current_db)
@archive_basename = File.join(@archive_directory, "#{SiteSetting.title.parameterize}-#{@timestamp}") @archive_basename = File.join(@archive_directory, "#{SiteSetting.title.parameterize}-#{@timestamp}")
@logs = []
end end
def listen_for_shutdown_signal def listen_for_shutdown_signal
@ -256,17 +256,21 @@ module Export
backup.after_create_hook backup.after_create_hook
end 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 def remove_old
log "Removing old backups..." log "Removing old backups..."
Backup.remove_old Backup.remove_old
end 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 def clean_up
log "Cleaning stuff up..." log "Cleaning stuff up..."
remove_tmp_directory remove_tmp_directory
@ -306,6 +310,7 @@ module Export
def log(message) def log(message)
puts(message) rescue nil puts(message) rescue nil
publish_log(message) rescue nil publish_log(message) rescue nil
save_log(message)
end end
def publish_log(message) def publish_log(message)
@ -314,6 +319,10 @@ module Export
MessageBus.publish(BackupRestore::LOGS_CHANNEL, data, user_ids: [@user_id]) MessageBus.publish(BackupRestore::LOGS_CHANNEL, data, user_ids: [@user_id])
end end
def save_log(message)
@logs << "[#{Time.now}] #{message}"
end
end end
end end

View file

@ -48,8 +48,6 @@ module Import
reconnect_database reconnect_database
extract_uploads extract_uploads
notify_user
rescue SystemExit rescue SystemExit
log "Restore process was cancelled!" log "Restore process was cancelled!"
rollback rollback
@ -60,6 +58,7 @@ module Import
else else
@success = true @success = true
ensure ensure
notify_user
clean_up clean_up
@success ? log("[SUCCESS]") : log("[FAILED]") @success ? log("[SUCCESS]") : log("[FAILED]")
end end
@ -95,6 +94,7 @@ module Import
@tar_filename = @archive_filename[0...-3] @tar_filename = @archive_filename[0...-3]
@meta_filename = File.join(@tmp_directory, BackupRestore::METADATA_FILE) @meta_filename = File.join(@tmp_directory, BackupRestore::METADATA_FILE)
@dump_filename = File.join(@tmp_directory, BackupRestore::DUMP_FILE) @dump_filename = File.join(@tmp_directory, BackupRestore::DUMP_FILE)
@logs << []
end end
def listen_for_shutdown_signal def listen_for_shutdown_signal
@ -256,16 +256,6 @@ module Import
end end
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 def rollback
log "Trying to rollback..." log "Trying to rollback..."
if BackupRestore.can_rollback? if BackupRestore.can_rollback?
@ -276,6 +266,20 @@ module Import
end end
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 def clean_up
log "Cleaning stuff up..." log "Cleaning stuff up..."
remove_tmp_directory remove_tmp_directory
@ -315,6 +319,7 @@ module Import
def log(message) def log(message)
puts(message) rescue nil puts(message) rescue nil
publish_log(message) rescue nil publish_log(message) rescue nil
save_log(message)
end end
def publish_log(message) def publish_log(message)
@ -323,6 +328,10 @@ module Import
MessageBus.publish(BackupRestore::LOGS_CHANNEL, data, user_ids: [@user_id]) MessageBus.publish(BackupRestore::LOGS_CHANNEL, data, user_ids: [@user_id])
end end
def save_log(message)
@logs << "[#{Time.now}] #{message}"
end
end end
end end