mirror of
https://github.com/discourse/discourse.git
synced 2025-03-23 01:45:41 +08:00
FEATURE: Trigger backup_failed
event on backup failure (#17955)
This also adds an optional `ticket` parameter to `Backuper` which allows identifying the backup in `backup_complete` and `backup_failed` events. Both events contain the logs as payload and moving some methods around ensures that all errors are included in the logs.
This commit is contained in:
parent
83a975a28b
commit
8626268366
@ -14,14 +14,15 @@ module BackupRestore
|
|||||||
@publish_to_message_bus = opts[:publish_to_message_bus] || false
|
@publish_to_message_bus = opts[:publish_to_message_bus] || false
|
||||||
@with_uploads = opts[:with_uploads].nil? ? include_uploads? : opts[:with_uploads]
|
@with_uploads = opts[:with_uploads].nil? ? include_uploads? : opts[:with_uploads]
|
||||||
@filename_override = opts[:filename]
|
@filename_override = opts[:filename]
|
||||||
|
@ticket = opts[:ticket]
|
||||||
ensure_no_operation_is_running
|
|
||||||
ensure_we_have_a_user
|
|
||||||
|
|
||||||
initialize_state
|
initialize_state
|
||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
|
ensure_no_operation_is_running
|
||||||
|
ensure_we_have_a_user
|
||||||
|
|
||||||
log "[STARTED]"
|
log "[STARTED]"
|
||||||
log "'#{@user.username}' has started the backup!"
|
log "'#{@user.username}' has started the backup!"
|
||||||
|
|
||||||
@ -55,8 +56,7 @@ module BackupRestore
|
|||||||
clean_up
|
clean_up
|
||||||
notify_user
|
notify_user
|
||||||
log "Finished!"
|
log "Finished!"
|
||||||
|
publish_completion(@success)
|
||||||
@success ? log("[SUCCESS]") : log("[FAILED]")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
@ -66,7 +66,6 @@ module BackupRestore
|
|||||||
end
|
end
|
||||||
|
|
||||||
def ensure_we_have_a_user
|
def ensure_we_have_a_user
|
||||||
@user = User.find_by(id: @user_id)
|
|
||||||
raise Discourse::InvalidParameters.new(:user_id) unless @user
|
raise Discourse::InvalidParameters.new(:user_id) unless @user
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -76,6 +75,8 @@ module BackupRestore
|
|||||||
|
|
||||||
def initialize_state
|
def initialize_state
|
||||||
@success = false
|
@success = false
|
||||||
|
@user = User.find_by(id: @user_id)
|
||||||
|
@logs = []
|
||||||
@store = BackupRestore::BackupStore.create
|
@store = BackupRestore::BackupStore.create
|
||||||
@current_db = RailsMultisite::ConnectionManagement.current_db
|
@current_db = RailsMultisite::ConnectionManagement.current_db
|
||||||
@timestamp = Time.now.strftime("%Y-%m-%d-%H%M%S")
|
@timestamp = Time.now.strftime("%Y-%m-%d-%H%M%S")
|
||||||
@ -91,8 +92,6 @@ module BackupRestore
|
|||||||
else
|
else
|
||||||
"#{File.basename(@archive_basename)}.sql.gz"
|
"#{File.basename(@archive_basename)}.sql.gz"
|
||||||
end
|
end
|
||||||
|
|
||||||
@logs = []
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def listen_for_shutdown_signal
|
def listen_for_shutdown_signal
|
||||||
@ -387,7 +386,7 @@ module BackupRestore
|
|||||||
|
|
||||||
def log(message, ex = nil)
|
def log(message, ex = nil)
|
||||||
timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
|
timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
|
||||||
puts(message)
|
puts(message) if !Rails.env.test?
|
||||||
publish_log(message, timestamp)
|
publish_log(message, timestamp)
|
||||||
save_log(message, timestamp)
|
save_log(message, timestamp)
|
||||||
Rails.logger.error("#{ex}\n" + ex.backtrace.join("\n")) if ex
|
Rails.logger.error("#{ex}\n" + ex.backtrace.join("\n")) if ex
|
||||||
@ -403,6 +402,15 @@ module BackupRestore
|
|||||||
@logs << "[#{timestamp}] #{message}"
|
@logs << "[#{timestamp}] #{message}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def publish_completion(success)
|
||||||
|
if success
|
||||||
|
log("[SUCCESS]")
|
||||||
|
DiscourseEvent.trigger(:backup_complete, logs: @logs, ticket: @ticket)
|
||||||
|
else
|
||||||
|
log("[FAILED]")
|
||||||
|
DiscourseEvent.trigger(:backup_failed, logs: @logs, ticket: @ticket)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user