Class: Assassin
- Inherits:
-
Object
- Object
- Assassin
- Defined in:
- lib/assassin.rb
Constant Summary collapse
- Version =
'1.4.2'
Instance Attribute Summary collapse
-
#child_pid ⇒ Object
Returns the value of attribute child_pid.
-
#parent_pid ⇒ Object
Returns the value of attribute parent_pid.
-
#path ⇒ Object
Returns the value of attribute path.
-
#pid ⇒ Object
Returns the value of attribute pid.
Class Method Summary collapse
- .at_exit_kill(*args, &block) ⇒ Object
- .ate(*args, &block) ⇒ Object
- .description ⇒ Object
- .generate(child_pid, options = {}) ⇒ Object
- .options_for(options) ⇒ Object
- .script_for(child_pid, options = {}) ⇒ Object
- .version ⇒ Object
Instance Method Summary collapse
-
#initialize(child_pid, options = {}) ⇒ Assassin
constructor
A new instance of Assassin.
Constructor Details
#initialize(child_pid, options = {}) ⇒ Assassin
Returns a new instance of Assassin.
28 29 30 31 32 33 |
# File 'lib/assassin.rb', line 28 def initialize(child_pid, = {}) @child_pid = child_pid.to_s.to_i @parent_pid = Process.pid @options = Assassin.() @pid, @path = Assassin.generate(@child_pid, @options) end |
Instance Attribute Details
#child_pid ⇒ Object
Returns the value of attribute child_pid.
24 25 26 |
# File 'lib/assassin.rb', line 24 def child_pid @child_pid end |
#parent_pid ⇒ Object
Returns the value of attribute parent_pid.
23 24 25 |
# File 'lib/assassin.rb', line 23 def parent_pid @parent_pid end |
#path ⇒ Object
Returns the value of attribute path.
26 27 28 |
# File 'lib/assassin.rb', line 26 def path @path end |
#pid ⇒ Object
Returns the value of attribute pid.
25 26 27 |
# File 'lib/assassin.rb', line 25 def pid @pid end |
Class Method Details
.at_exit_kill(*args, &block) ⇒ Object
15 16 17 |
# File 'lib/assassin.rb', line 15 def Assassin.at_exit_kill(*args, &block) new(*args, &block) end |
.ate(*args, &block) ⇒ Object
19 20 21 |
# File 'lib/assassin.rb', line 19 def Assassin.ate(*args, &block) new(*args, &block) end |
.description ⇒ Object
11 12 13 |
# File 'lib/assassin.rb', line 11 def Assassin.description 'no zombies ever, not even on `exit!` or `kill -9`' end |
.generate(child_pid, options = {}) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/assassin.rb', line 39 def Assassin.generate(child_pid, = {}) path = File.join(Dir.tmpdir, "assassin-#{ child_pid }-#{ SecureRandom.uuid }.rb") script = Assassin.script_for(child_pid, ) IO.binwrite(path, script) pid = Process.spawn "ruby #{ path }" [pid, path] end |
.options_for(options) ⇒ Object
35 36 37 |
# File 'lib/assassin.rb', line 35 def Assassin.() .inject({}){|h, kv| k,v = kv; h.update(k.to_s.to_sym => v)} end |
.script_for(child_pid, options = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/assassin.rb', line 47 def Assassin.script_for(child_pid, = {}) parent_pid = Process.pid delay = ([:delay] || 0.42).to_f script = <<-__ Process.daemon require 'fileutils' at_exit{ FileUtils.rm_f(__FILE__) } parent_pid = #{ parent_pid } child_pid = #{ child_pid } delay = #{ delay } m = 24*60*60 n = 42 m.times do begin Process.kill(0, parent_pid) rescue Object => e sleep(delay) if e.is_a?(Errno::ESRCH) n.times do begin Process.kill(15, child_pid) rescue nil sleep(rand + rand) Process.kill(9, child_pid) rescue nil sleep(rand + rand) Process.kill(0, child_pid) rescue Errno::ESRCH break end end end exit end sleep(1) end __ return script end |