lib/bluepill/process_journal.rb in bluepill-0.0.68 vs lib/bluepill/process_journal.rb in bluepill-0.0.69

- old
+ new

@@ -1,22 +1,22 @@ require 'bluepill/system' module Bluepill module ProcessJournal - extend self + module_function class << self attr_reader :logger attr_reader :journal_base_dir def logger=(new_logger) @logger ||= new_logger end def base_dir=(base_dir) - @journal_base_dir ||= File.join(base_dir, "journals") - FileUtils.mkdir_p(@journal_base_dir) unless File.exists?(@journal_base_dir) + @journal_base_dir ||= File.join(base_dir, 'journals') + FileUtils.mkdir_p(@journal_base_dir) unless File.exist?(@journal_base_dir) FileUtils.chmod(0777, @journal_base_dir) end end def skip_pid?(pid) @@ -37,11 +37,11 @@ yield rescue Errno::EEXIST times += 1 logger.debug("Waiting for lock #{name}") sleep 1 - unless times >= 10 + if times < 10 retry else logger.info("Timeout waiting for lock #{name}") raise "Timeout waiting for lock #{name}" end @@ -63,39 +63,36 @@ File.join(@journal_base_dir, ".bluepill_pgids_journal.#{journal_name}") end def pid_journal(filename) logger.debug("pid journal file: #{filename}") - result = File.open(filename, 'r').readlines.map(&:to_i).reject {|pid| skip_pid?(pid)} + result = File.open(filename, 'r').readlines.map(&:to_i).reject { |pid| skip_pid?(pid) } logger.debug("pid journal = #{result.join(' ')}") result rescue Errno::ENOENT [] end def pgid_journal(filename) logger.debug("pgid journal file: #{filename}") - result = File.open(filename, 'r').readlines.map(&:to_i).reject {|pgid| skip_pgid?(pgid)} + result = File.open(filename, 'r').readlines.map(&:to_i).reject { |pgid| skip_pgid?(pgid) } logger.debug("pgid journal = #{result.join(' ')}") result rescue Errno::ENOENT [] end def clear_atomic_fs_lock(name) - if File.directory?(name) - Dir.rmdir(name) - logger.debug("Cleared lock #{name}") - end + return unless File.directory?(name) + Dir.rmdir(name) + logger.debug("Cleared lock #{name}") end def kill_all_from_all_journals - Dir[".bluepill_pids_journal.*"].map { |x| - x.sub(/^\.bluepill_pids_journal\./,"") - }.reject { |y| - y =~ /\.lock$/ - }.each do |journal_name| + pids = Dir['.bluepill_pids_journal.*'].map { |p| p.sub(/^\.bluepill_pids_journal\./, '') } + pids.reject! { |p| p =~ /\.lock$/ } + pids.each do |journal_name| kill_all_from_journal(journal_name) end end def kill_all_from_journal(journal_name) @@ -175,17 +172,17 @@ return end filename = pgid_journal_filename(journal_name) acquire_atomic_fs_lock(filename) do - unless pgid_journal(filename).include?(pgid) + if pgid_journal(filename).include?(pgid) + logger.debug("Skipping duplicate pgid #{pgid} already in journal #{journal_name}") + else logger.debug("Saving pgid #{pgid} to process journal #{journal_name}") File.open(filename, 'a+', 0600) { |f| f.puts(pgid) } logger.info("Saved pgid #{pgid} to journal #{journal_name}") logger.debug("Journal now = #{File.open(filename, 'r').read}") - else - logger.debug("Skipping duplicate pgid #{pgid} already in journal #{journal_name}") end end end def append_pid_to_journal(journal_name, pid) @@ -198,16 +195,16 @@ return end filename = pid_journal_filename(journal_name) acquire_atomic_fs_lock(filename) do - unless pid_journal(filename).include?(pid) + if pid_journal(filename).include?(pid) + logger.debug("Skipping duplicate pid #{pid} already in journal #{journal_name}") + else logger.debug("Saving pid #{pid} to process journal #{journal_name}") File.open(filename, 'a+', 0600) { |f| f.puts(pid) } logger.info("Saved pid #{pid} to journal #{journal_name}") logger.debug("Journal now = #{File.open(filename, 'r').read}") - else - logger.debug("Skipping duplicate pid #{pid} already in journal #{journal_name}") end end end end end