diff --git a/app/controllers/admin/backups_controller.rb b/app/controllers/admin/backups_controller.rb index dae617b0426..fc068a92ead 100644 --- a/app/controllers/admin/backups_controller.rb +++ b/app/controllers/admin/backups_controller.rb @@ -2,13 +2,14 @@ require_dependency "backup_restore" class Admin::BackupsController < Admin::AdminController - skip_before_filter :check_xhr, only: [:index, :show] + skip_before_filter :check_xhr, only: [:index, :show, :logs] def index respond_to do |format| format.html do store_preloaded("backups", MultiJson.dump(serialize_data(Backup.all, BackupSerializer))) store_preloaded("operations_status", MultiJson.dump(BackupRestore.operations_status)) + store_preloaded("logs", MultiJson.dump(BackupRestore.logs)) render "default/empty" end format.json do @@ -55,6 +56,7 @@ class Admin::BackupsController < Admin::AdminController def logs store_preloaded("operations_status", MultiJson.dump(BackupRestore.operations_status)) + store_preloaded("logs", MultiJson.dump(BackupRestore.logs)) render "default/empty" end diff --git a/lib/backup_restore.rb b/lib/backup_restore.rb index 72cfbc4cb3f..5d452168625 100644 --- a/lib/backup_restore.rb +++ b/lib/backup_restore.rb @@ -7,6 +7,7 @@ module BackupRestore DUMP_FILE = "dump.sql" METADATA_FILE = "meta.json" + LOGS_CHANNEL = "/admin/backups/logs" def self.backup!(user_id, publish_to_message_bus = false) exporter = Export::Exporter.new(user_id, publish_to_message_bus) @@ -32,8 +33,10 @@ module BackupRestore end def self.mark_as_running! - # TODO: should acquire a lock and raise an exception if already running! + # TODO: for more safety, it should acquire a lock + # and raise an exception if already running! $redis.set(running_key, "1") + save_start_logs_message_id end def self.is_operation_running? @@ -59,6 +62,11 @@ module BackupRestore } end + def self.logs + id = start_logs_message_id + MessageBus.backlog(LOGS_CHANNEL, id).map { |m| m.data } + end + def self.current_version ActiveRecord::Migrator.current_version end @@ -96,6 +104,19 @@ module BackupRestore $redis.del(shutdown_signal_key) end + def self.save_start_logs_message_id + id = MessageBus.last_id(LOGS_CHANNEL) + $redis.set(start_logs_message_id_key, id) + end + + def self.start_logs_message_id + $redis.get(start_logs_message_id_key).to_i + end + + def self.start_logs_message_id_key + "start_logs_message_id" + end + def self.start!(runner) child = fork do begin diff --git a/lib/export/exporter.rb b/lib/export/exporter.rb index d4cc446c402..b0ac9394905 100644 --- a/lib/export/exporter.rb +++ b/lib/export/exporter.rb @@ -296,7 +296,7 @@ module Export def publish_log(message) return unless @publish_to_message_bus data = { timestamp: Time.now, operation: "backup", message: message } - MessageBus.publish("/admin/backups/logs", data, user_ids: [@user_id]) + MessageBus.publish(BackupRestore::LOGS_CHANNEL, data, user_ids: [@user_id]) end end diff --git a/lib/import/importer.rb b/lib/import/importer.rb index d7689508a13..611edda2130 100644 --- a/lib/import/importer.rb +++ b/lib/import/importer.rb @@ -319,7 +319,7 @@ module Import def publish_log(message) return unless @publish_to_message_bus data = { timestamp: Time.now, operation: "restore", message: message } - MessageBus.publish("/admin/backups/logs", data, user_ids: [@user_id]) + MessageBus.publish(BackupRestore::LOGS_CHANNEL, data, user_ids: [@user_id]) end end diff --git a/spec/controllers/admin/backups_controller_spec.rb b/spec/controllers/admin/backups_controller_spec.rb index 936fb9756db..b6b6b85da09 100644 --- a/spec/controllers/admin/backups_controller_spec.rb +++ b/spec/controllers/admin/backups_controller_spec.rb @@ -16,13 +16,16 @@ describe Admin::BackupsController do context "html format" do - it "preloads both backups and operations_status" do + it "preloads important data" do Backup.expects(:all).returns([]) subject.expects(:store_preloaded).with("backups", "[]") BackupRestore.expects(:operations_status).returns({}) subject.expects(:store_preloaded).with("operations_status", "{}") + BackupRestore.expects(:logs).returns([]) + subject.expects(:store_preloaded).with("logs", "[]") + xhr :get, :index, format: :html response.should be_success @@ -134,15 +137,17 @@ describe Admin::BackupsController do describe ".logs" do - it "preloads operations_status" do + it "preloads important data" do BackupRestore.expects(:operations_status).returns({}) subject.expects(:store_preloaded).with("operations_status", "{}") + BackupRestore.expects(:logs).returns([]) + subject.expects(:store_preloaded).with("logs", "[]") + xhr :get, :logs, format: :html response.should be_success end - end describe ".restore" do