Module: Bluepill::ProcessJournal
Class Attribute Summary collapse
-
.journal_base_dir ⇒ Object
readonly
Returns the value of attribute journal_base_dir.
-
.logger ⇒ Object
Returns the value of attribute logger.
Class Method Summary collapse
Instance Method Summary collapse
-
#acquire_atomic_fs_lock(name) ⇒ Object
atomic operation on POSIX filesystems, since f.flock(File::LOCK_SH) is not available on all platforms.
- #append_pgid_to_journal(journal_name, pgid) ⇒ Object
- #append_pid_to_journal(journal_name, pid) ⇒ Object
- #clear_all_atomic_fs_locks ⇒ Object
- #clear_atomic_fs_lock(name) ⇒ Object
- #kill_all_from_all_journals ⇒ Object
- #kill_all_from_journal(journal_name) ⇒ Object
- #kill_all_pgids_from_journal(journal_name) ⇒ Object
- #kill_all_pids_from_journal(journal_name) ⇒ Object
- #pgid_journal(filename) ⇒ Object
- #pgid_journal_filename(journal_name) ⇒ Object
- #pid_journal(filename) ⇒ Object
- #pid_journal_filename(journal_name) ⇒ Object
- #skip_pgid?(pgid) ⇒ Boolean
- #skip_pid?(pid) ⇒ Boolean
Class Attribute Details
.journal_base_dir ⇒ Object (readonly)
Returns the value of attribute journal_base_dir.
9 10 11 |
# File 'lib/bluepill/process_journal.rb', line 9 def journal_base_dir @journal_base_dir end |
.logger ⇒ Object
Returns the value of attribute logger.
8 9 10 |
# File 'lib/bluepill/process_journal.rb', line 8 def logger @logger end |
Class Method Details
.base_dir=(base_dir) ⇒ Object
15 16 17 18 19 |
# File 'lib/bluepill/process_journal.rb', line 15 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) FileUtils.chmod(0777, @journal_base_dir) end |
Instance Method Details
#acquire_atomic_fs_lock(name) ⇒ Object
atomic operation on POSIX filesystems, since f.flock(File::LOCK_SH) is not available on all platforms
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/bluepill/process_journal.rb', line 32 def acquire_atomic_fs_lock(name) times = 0 name += '.lock' Dir.mkdir name, 0700 logger.debug("Acquired lock #{name}") yield rescue Errno::EEXIST times += 1 logger.debug("Waiting for lock #{name}") sleep 1 unless times >= 10 retry else logger.info("Timeout waiting for lock #{name}") raise "Timeout waiting for lock #{name}" end ensure clear_atomic_fs_lock(name) end |
#append_pgid_to_journal(journal_name, pgid) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/bluepill/process_journal.rb', line 172 def append_pgid_to_journal(journal_name, pgid) if skip_pgid?(pgid) logger.debug("Skipping invalid pgid #{pgid} (class #{pgid.class})") return end filename = pgid_journal_filename(journal_name) acquire_atomic_fs_lock(filename) do unless pgid_journal(filename).include?(pgid) 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 |
#append_pid_to_journal(journal_name, pid) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/bluepill/process_journal.rb', line 191 def append_pid_to_journal(journal_name, pid) begin append_pgid_to_journal(journal_name, ::Process.getpgid(pid)) rescue Errno::ESRCH end if skip_pid?(pid) logger.debug("Skipping invalid pid #{pid} (class #{pid.class})") return end filename = pid_journal_filename(journal_name) acquire_atomic_fs_lock(filename) do unless pid_journal(filename).include?(pid) 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 |
#clear_all_atomic_fs_locks ⇒ Object
52 53 54 55 56 |
# File 'lib/bluepill/process_journal.rb', line 52 def clear_all_atomic_fs_locks Dir['.*.lock'].each do |f| System.delete_if_exists(f) if File.directory?(f) end end |
#clear_atomic_fs_lock(name) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/bluepill/process_journal.rb', line 84 def clear_atomic_fs_lock(name) if File.directory?(name) Dir.rmdir(name) logger.debug("Cleared lock #{name}") end end |
#kill_all_from_all_journals ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/bluepill/process_journal.rb', line 91 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| kill_all_from_journal(journal_name) end end |
#kill_all_from_journal(journal_name) ⇒ Object
101 102 103 104 |
# File 'lib/bluepill/process_journal.rb', line 101 def kill_all_from_journal(journal_name) kill_all_pids_from_journal(journal_name) kill_all_pgids_from_journal(journal_name) end |
#kill_all_pgids_from_journal(journal_name) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/bluepill/process_journal.rb', line 106 def kill_all_pgids_from_journal(journal_name) filename = pgid_journal_filename(journal_name) j = pgid_journal(filename) if j.length > 0 acquire_atomic_fs_lock(filename) do j.each do |pgid| begin ::Process.kill('TERM', -pgid) logger.info("Termed old process group #{pgid}") rescue Errno::ESRCH logger.debug("Unable to term missing process group #{pgid}") end end if j.select { |pgid| System.pid_alive?(pgid) }.length > 1 sleep(1) j.each do |pgid| begin ::Process.kill('KILL', -pgid) logger.info("Killed old process group #{pgid}") rescue Errno::ESRCH logger.debug("Unable to kill missing process group #{pgid}") end end end System.delete_if_exists(filename) # reset journal logger.debug('Journal cleanup completed') end else logger.debug('No previous process journal - Skipping cleanup') end end |
#kill_all_pids_from_journal(journal_name) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/bluepill/process_journal.rb', line 139 def kill_all_pids_from_journal(journal_name) filename = pid_journal_filename(journal_name) j = pid_journal(filename) if j.length > 0 acquire_atomic_fs_lock(filename) do j.each do |pid| begin ::Process.kill('TERM', pid) logger.info("Termed old process #{pid}") rescue Errno::ESRCH logger.debug("Unable to term missing process #{pid}") end end if j.select { |pid| System.pid_alive?(pid) }.length > 1 sleep(1) j.each do |pid| begin ::Process.kill('KILL', pid) logger.info("Killed old process #{pid}") rescue Errno::ESRCH logger.debug("Unable to kill missing process #{pid}") end end end System.delete_if_exists(filename) # reset journal logger.debug('Journal cleanup completed') end else logger.debug('No previous process journal - Skipping cleanup') end end |
#pgid_journal(filename) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/bluepill/process_journal.rb', line 75 def pgid_journal(filename) logger.debug("pgid journal file: #{filename}") 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 |
#pgid_journal_filename(journal_name) ⇒ Object
62 63 64 |
# File 'lib/bluepill/process_journal.rb', line 62 def pgid_journal_filename(journal_name) File.join(@journal_base_dir, ".bluepill_pgids_journal.#{journal_name}") end |
#pid_journal(filename) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/bluepill/process_journal.rb', line 66 def pid_journal(filename) logger.debug("pid journal file: #{filename}") 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 |
#pid_journal_filename(journal_name) ⇒ Object
58 59 60 |
# File 'lib/bluepill/process_journal.rb', line 58 def pid_journal_filename(journal_name) File.join(@journal_base_dir, ".bluepill_pids_journal.#{journal_name}") end |
#skip_pgid?(pgid) ⇒ Boolean
26 27 28 |
# File 'lib/bluepill/process_journal.rb', line 26 def skip_pgid?(pgid) !pgid.is_a?(Integer) || pgid <= 1 end |
#skip_pid?(pid) ⇒ Boolean
22 23 24 |
# File 'lib/bluepill/process_journal.rb', line 22 def skip_pid?(pid) !pid.is_a?(Integer) || pid <= 1 end |