Module: Bluepill::ProcessJournal
Class Attribute Summary collapse
-
.logger ⇒ Object
Returns the value of attribute logger.
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
.logger ⇒ Object
Returns the value of attribute logger.
8 9 10 |
# File 'lib/bluepill/process_journal.rb', line 8 def logger @logger 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
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/bluepill/process_journal.rb', line 25 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
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/bluepill/process_journal.rb', line 165 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
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/bluepill/process_journal.rb', line 184 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
45 46 47 48 49 |
# File 'lib/bluepill/process_journal.rb', line 45 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
77 78 79 80 81 82 |
# File 'lib/bluepill/process_journal.rb', line 77 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
84 85 86 87 88 89 90 91 92 |
# File 'lib/bluepill/process_journal.rb', line 84 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
94 95 96 97 |
# File 'lib/bluepill/process_journal.rb', line 94 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
99 100 101 102 103 104 105 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 |
# File 'lib/bluepill/process_journal.rb', line 99 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
132 133 134 135 136 137 138 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 |
# File 'lib/bluepill/process_journal.rb', line 132 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
68 69 70 71 72 73 74 75 |
# File 'lib/bluepill/process_journal.rb', line 68 def pgid_journal(filename) logger.debug("pgid journal PWD=#{Dir.pwd}") 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
55 56 57 |
# File 'lib/bluepill/process_journal.rb', line 55 def pgid_journal_filename(journal_name) ".bluepill_pgids_journal.#{journal_name}" end |
#pid_journal(filename) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/bluepill/process_journal.rb', line 59 def pid_journal(filename) logger.debug("pid journal PWD=#{Dir.pwd}") 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
51 52 53 |
# File 'lib/bluepill/process_journal.rb', line 51 def pid_journal_filename(journal_name) ".bluepill_pids_journal.#{journal_name}" end |
#skip_pgid?(pgid) ⇒ Boolean
19 20 21 |
# File 'lib/bluepill/process_journal.rb', line 19 def skip_pgid?(pgid) !pgid.is_a?(Integer) || pgid <= 1 end |
#skip_pid?(pid) ⇒ Boolean
15 16 17 |
# File 'lib/bluepill/process_journal.rb', line 15 def skip_pid?(pid) !pid.is_a?(Integer) || pid <= 1 end |