Class: Spider::HTTP::Spawner

Inherits:
Object
  • Object
show all
Defined in:
lib/spiderfw/http/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actions) ⇒ Spawner

Returns a new instance of Spawner.



189
190
191
# File 'lib/spiderfw/http/server.rb', line 189

def initialize(actions)
    @actions = actions
end

Instance Attribute Details

#child_pidObject (readonly)

Returns the value of attribute child_pid.



187
188
189
# File 'lib/spiderfw/http/server.rb', line 187

def child_pid
  @child_pid
end

Instance Method Details

#monitor_fsObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/spiderfw/http/server.rb', line 242

def monitor_fs
    require 'fssm'
    spawner = self
    action = 'spawn'
    return Thread.new do
        fsm_exclude = ['var', 'tmp']
        FSSM.monitor do
            #Spider.logger.debug("Monitoring #{Spider.paths[:apps]} for changes")
            path Spider.paths[:apps] do 
                glob '**/*.rb'

                update { |base, relative| 
                    Spider.logger.debug("#{relative} updated, restarting")
                    Process.kill 'KILL', Thread.current[:spawner_child_pid]
                    spawner.spawn(action)
                }
                delete { |base, relative|
                    Spider.logger.debug("#{relative} deleted, restarting")
                    Process.kill 'KILL', Thread.current[:spawner_child_pid]
                    spawner.spawn(action)
                }
                create { |base, relative|
                    Spider.logger.debug("#{relative} created, restarting")
                    Process.kill 'KILL', Thread.current[:spawner_child_pid]
                    spawner.spawn(action)
                }
            end
            # path Spider.paths[:root] do
            #     glob '**/*.shtml'
            #     
            #     update { |base, relative|
            #         puts "Changed #{base}, #{relative}"
            #         Spider::Template.cache.invalidate(File.join('ROOT', relative))
            #     }
            # end

            # path '/some/other/directory/' do
            #   update {|base, relative|}
            #   delete {|base, relative|}
            #   create {|base, relative|}
            # end
        end
    end
    
end

#run(action = nil) ⇒ Object



193
194
195
196
197
# File 'lib/spiderfw/http/server.rb', line 193

def run(action=nil)
    @monitor_thread = monitor_fs
    spawn(action)
    @monitor_thread.join
end

#spawn(action) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/spiderfw/http/server.rb', line 199

def spawn(action)
    rd, wr = IO.pipe
    if pid = fork
        # Spawner
        Spider.logger.debug("Spawner forked")
        @child_pid = pid

        unless @already_forked
            Spider.main_process_startup
            exit_spawner = lambda{ 
                Spider.logger.debug "Spawner exiting" 
                Process.kill 'KILL', @monitor_thread[:spawner_child_pid]
            }
            Spider.on_main_process_shutdown(&exit_spawner)
            $SPIDER_SCRIPT ||= $0
            $0 = 'spider-spawner'
        end
        @already_forked = true

        wr.close
        @monitor_thread[:spawner_child_pid] = pid
        # TODO
        # msg = rd.read(5)
        # rd.close
        # spawn(msg) unless msg.blank?

    else
        # Child
        $SPIDER_SPAWNED = true
        trap('TERM'){ }
        trap('INT'){ }
        rd.close
        Spider.spawner = wr
        return unless @actions[action]
        begin
            @actions[action].call
        rescue Exception => exc
            Spider.logger.debug(exc)
            Process.kill 'KILL', Process.pid
        end
    end
end