Class: RailsBuild::Assassin

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_build.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(child_pid, options = {}) ⇒ Assassin

Returns a new instance of Assassin.



144
145
146
147
148
149
# File 'lib/rails_build.rb', line 144

def initialize(child_pid, options = {})
  @child_pid = child_pid.to_s.to_i
  @parent_pid = Process.pid
  @options = Assassin.options_for(options)
  @pid, @path = Assassin.generate(@child_pid, @options)
end

Instance Attribute Details

#child_pidObject

Returns the value of attribute child_pid.



140
141
142
# File 'lib/rails_build.rb', line 140

def child_pid
  @child_pid
end

#parent_pidObject

Returns the value of attribute parent_pid.



139
140
141
# File 'lib/rails_build.rb', line 139

def parent_pid
  @parent_pid
end

#pathObject

Returns the value of attribute path.



142
143
144
# File 'lib/rails_build.rb', line 142

def path
  @path
end

#pidObject

Returns the value of attribute pid.



141
142
143
# File 'lib/rails_build.rb', line 141

def pid
  @pid
end

Class Method Details

.ate(*args, &block) ⇒ Object



135
136
137
# File 'lib/rails_build.rb', line 135

def Assassin.ate(*args, &block)
  new(*args, &block)
end

.generate(child_pid, options = {}) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/rails_build.rb', line 155

def Assassin.generate(child_pid, options = {})
  path = File.join(Dir.tmpdir, "assassin-#{ child_pid }-#{ SecureRandom.uuid }.rb")
  script = Assassin.script_for(child_pid, options)
  IO.binwrite(path, script)
  pid = Process.spawn "ruby #{ path }"
  [pid, path]
end

.options_for(options) ⇒ Object



151
152
153
# File 'lib/rails_build.rb', line 151

def Assassin.options_for(options)
  options.inject({}){|h, kv| k,v = kv; h.update(k.to_s.to_sym => v)}
end

.script_for(child_pid, options = {}) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/rails_build.rb', line 163

def Assassin.script_for(child_pid, options = {})
  parent_pid = Process.pid
  delay = (options[: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