lib/madeleine.rb in madeleine-0.7.3 vs lib/madeleine.rb in madeleine-0.8.0.pre

- old
+ new

@@ -7,11 +7,11 @@ # Usage: # # require 'madeleine' # # madeleine = SnapshotMadeleine.new("my_example_storage") { -# SomeExampleApplication.new() +# SomeExampleApplication.new # } # # madeleine.execute_command(command) # @@ -20,22 +20,23 @@ require 'thread' require 'sync' require 'fileutils' require 'madeleine/files' require 'madeleine/sanity' + require 'madeleine/version' - MADELEINE_VERSION = "0.7.3" + MADELEINE_VERSION = Madeleine::VERSION class SnapshotMadeleine # Builds a new Madeleine instance. If there is a snapshot available # then the system will be created from that, otherwise # <tt>new_system</tt> will be used. The state of the system will # then be restored from the command logs. # # You can provide your own snapshot marshaller, for instance using - # YAML or SOAP, instead of Ruby's built-in marshaller. The + # YAML, instead of Ruby's built-in marshaller. The # <tt>snapshot_marshaller</tt> must respond to # <tt>load(stream)</tt> and <tt>dump(object, stream)</tt>. You # must use the same marshaller every time for a system. # # See: DefaultSnapshotMadeleine @@ -85,28 +86,28 @@ # The return value from the command's <tt>execute()</tt> method is returned. # # * <tt>command</tt> - The command to execute on the system. def execute_command(command) verify_command_sane(command) - @lock.synchronize { + @lock.synchronize do raise MadeleineClosedException if @closed @logger.store(command) @executer.execute(command) - } + end end # Execute a query on the prevalent system. # # Only differs from <tt>execute_command</tt> in that the command/query isn't logged, and # therefore isn't allowed to modify the system. A shared lock is held, preventing others # from modifying the system while the query is running. # # * <tt>query</tt> - The query command to execute def execute_query(query) - @lock.synchronize_shared { + @lock.synchronize_shared do @executer.execute(query) - } + end end # Take a snapshot of the current system. # # You need to regularly take a snapshot of a running system, @@ -121,26 +122,26 @@ # sleep(60 * 60 * 24) # 24 hours # madeleine.take_snapshot # end # } def take_snapshot - @lock.synchronize { + @lock.synchronize do @logger.close @snapshotter.take(@system) @logger.reset - } + end end # Close the system. # # The log file is closed and no new commands can be received # by this Madeleine. def close - @lock.synchronize { + @lock.synchronize do @logger.close @closed = true - } + end end private def verify_command_sane(command) @@ -211,33 +212,33 @@ def recover_snapshot(new_system_block) system = nil id = SnapshotFile.highest_id(@directory_name) if id > 0 snapshot_file = SnapshotFile.new(@directory_name, id).name - open(snapshot_file, "rb") {|snapshot| + open(snapshot_file, "rb") do |snapshot| system = @marshaller.load(snapshot) - } + end else system = new_system_block.call end system end def recover_logs(executer) - executer.recovery { - CommandLog.log_file_names(@directory_name, FileService.new).each {|file_name| - open(@directory_name + File::SEPARATOR + file_name, "rb") {|log| + executer.recovery do + CommandLog.log_file_names(@directory_name, FileService.new).each do |file_name| + open(@directory_name + File::SEPARATOR + file_name, "rb") do |log| recover_log(executer, log) - } - } - } + end + end + end end private def recover_log(executer, log) - while ! log.eof? + until log.eof? command = Marshal.load(log) executer.execute(command) end end end @@ -262,13 +263,14 @@ def self.log_file_names(directory_name, file_service) return [] unless file_service.exist?(directory_name) result = file_service.dir_entries(directory_name).select {|name| name =~ /^\d{#{FILE_COUNTER_SIZE}}\.command_log$/ } - result.each {|name| name.untaint } - result.sort! - result + result.each do |name| + name.untaint + end + result.sort end def initialize(path, file_service) id = self.class.highest_log(path, file_service) + 1 numbered_file = NumberedFile.new(path, "command_log", id) @@ -285,17 +287,17 @@ @file.fsync end def self.highest_log(directory_name, file_service) highest = 0 - log_file_names(directory_name, file_service).each {|file_name| + log_file_names(directory_name, file_service).each do |file_name| match = /^(\d{#{FILE_COUNTER_SIZE}})/.match(file_name) n = match[1].to_i if n > highest highest = n end - } + end highest end end class DefaultLogFactory #:nodoc: @@ -313,11 +315,11 @@ @pending_tick = nil ensure_directory_exists end def ensure_directory_exists - if ! File.exist?(@directory_name) + unless File.exist?(@directory_name) FileUtils.mkpath(@directory_name) end end def reset @@ -351,14 +353,15 @@ end private def delete_log_files - Dir.glob(@directory_name + File::SEPARATOR + "*.command_log").each {|name| + names = Dir.glob(@directory_name + File::SEPARATOR + "*.command_log") + names.each do |name| name.untaint File.delete(name) - } + end end def open_new_log @log = @log_factory.create_log(@directory_name) end @@ -368,18 +371,18 @@ def self.highest_id(directory_name) return 0 unless File.exist?(directory_name) suffix = "snapshot" highest = 0 - Dir.foreach(directory_name) {|file_name| + Dir.foreach(directory_name) do |file_name| match = /^(\d{#{FILE_COUNTER_SIZE}}\.#{suffix}$)/.match(file_name) next unless match n = match[1].to_i if n > highest highest = n end - } + end highest end def self.next(directory_name) new(directory_name, highest_id(directory_name) + 1) @@ -397,16 +400,16 @@ end def take(system) numbered_file = SnapshotFile.next(@directory_name) name = numbered_file.name - open(name + '.tmp', 'wb') {|snapshot| + open("#{name}.tmp", 'wb') do |snapshot| @marshaller.dump(system, snapshot) snapshot.flush snapshot.fsync - } - File.rename(name + '.tmp', name) + end + File.rename("#{name}.tmp", name) end end module Clock #:nodoc: class Tick #:nodoc: @@ -421,6 +424,5 @@ end end end SnapshotMadeleine = Madeleine::SnapshotMadeleine -