display/preload the logs of the last/current operation

This commit is contained in:
Régis Hanol 2014-02-13 10:41:46 -08:00
parent 50273ba815
commit b89d328de2
5 changed files with 35 additions and 7 deletions

View File

@ -2,13 +2,14 @@ require_dependency "backup_restore"
class Admin::BackupsController < Admin::AdminController class Admin::BackupsController < Admin::AdminController
skip_before_filter :check_xhr, only: [:index, :show] skip_before_filter :check_xhr, only: [:index, :show, :logs]
def index def index
respond_to do |format| respond_to do |format|
format.html do format.html do
store_preloaded("backups", MultiJson.dump(serialize_data(Backup.all, BackupSerializer))) store_preloaded("backups", MultiJson.dump(serialize_data(Backup.all, BackupSerializer)))
store_preloaded("operations_status", MultiJson.dump(BackupRestore.operations_status)) store_preloaded("operations_status", MultiJson.dump(BackupRestore.operations_status))
store_preloaded("logs", MultiJson.dump(BackupRestore.logs))
render "default/empty" render "default/empty"
end end
format.json do format.json do
@ -55,6 +56,7 @@ class Admin::BackupsController < Admin::AdminController
def logs def logs
store_preloaded("operations_status", MultiJson.dump(BackupRestore.operations_status)) store_preloaded("operations_status", MultiJson.dump(BackupRestore.operations_status))
store_preloaded("logs", MultiJson.dump(BackupRestore.logs))
render "default/empty" render "default/empty"
end end

View File

@ -7,6 +7,7 @@ module BackupRestore
DUMP_FILE = "dump.sql" DUMP_FILE = "dump.sql"
METADATA_FILE = "meta.json" METADATA_FILE = "meta.json"
LOGS_CHANNEL = "/admin/backups/logs"
def self.backup!(user_id, publish_to_message_bus = false) def self.backup!(user_id, publish_to_message_bus = false)
exporter = Export::Exporter.new(user_id, publish_to_message_bus) exporter = Export::Exporter.new(user_id, publish_to_message_bus)
@ -32,8 +33,10 @@ module BackupRestore
end end
def self.mark_as_running! 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") $redis.set(running_key, "1")
save_start_logs_message_id
end end
def self.is_operation_running? def self.is_operation_running?
@ -59,6 +62,11 @@ module BackupRestore
} }
end end
def self.logs
id = start_logs_message_id
MessageBus.backlog(LOGS_CHANNEL, id).map { |m| m.data }
end
def self.current_version def self.current_version
ActiveRecord::Migrator.current_version ActiveRecord::Migrator.current_version
end end
@ -96,6 +104,19 @@ module BackupRestore
$redis.del(shutdown_signal_key) $redis.del(shutdown_signal_key)
end 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) def self.start!(runner)
child = fork do child = fork do
begin begin

View File

@ -296,7 +296,7 @@ module Export
def publish_log(message) def publish_log(message)
return unless @publish_to_message_bus return unless @publish_to_message_bus
data = { timestamp: Time.now, operation: "backup", message: message } 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
end end

View File

@ -319,7 +319,7 @@ module Import
def publish_log(message) def publish_log(message)
return unless @publish_to_message_bus return unless @publish_to_message_bus
data = { timestamp: Time.now, operation: "restore", message: message } 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
end end

View File

@ -16,13 +16,16 @@ describe Admin::BackupsController do
context "html format" do context "html format" do
it "preloads both backups and operations_status" do it "preloads important data" do
Backup.expects(:all).returns([]) Backup.expects(:all).returns([])
subject.expects(:store_preloaded).with("backups", "[]") subject.expects(:store_preloaded).with("backups", "[]")
BackupRestore.expects(:operations_status).returns({}) BackupRestore.expects(:operations_status).returns({})
subject.expects(:store_preloaded).with("operations_status", "{}") subject.expects(:store_preloaded).with("operations_status", "{}")
BackupRestore.expects(:logs).returns([])
subject.expects(:store_preloaded).with("logs", "[]")
xhr :get, :index, format: :html xhr :get, :index, format: :html
response.should be_success response.should be_success
@ -134,15 +137,17 @@ describe Admin::BackupsController do
describe ".logs" do describe ".logs" do
it "preloads operations_status" do it "preloads important data" do
BackupRestore.expects(:operations_status).returns({}) BackupRestore.expects(:operations_status).returns({})
subject.expects(:store_preloaded).with("operations_status", "{}") subject.expects(:store_preloaded).with("operations_status", "{}")
BackupRestore.expects(:logs).returns([])
subject.expects(:store_preloaded).with("logs", "[]")
xhr :get, :logs, format: :html xhr :get, :logs, format: :html
response.should be_success response.should be_success
end end
end end
describe ".restore" do describe ".restore" do