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.



200
201
202
# File 'lib/spiderfw/http/server.rb', line 200

def initialize(actions)
    @actions = actions
end

Instance Attribute Details

#child_pidObject (readonly)

Returns the value of attribute child_pid.



198
199
200
# File 'lib/spiderfw/http/server.rb', line 198

def child_pid
  @child_pid
end

Instance Method Details

#monitor_fsObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/spiderfw/http/server.rb', line 263

def monitor_fs
    require 'fssm' if RUBY_VERSION =~ /1.8/
    require 'listen' if RUBY_VERSION > "1.8.7"
    spawner = self
    action = 'spawn'
    return Thread.new do
        #VECCHIA VERSIONE CON FSSM PER RUBY 1.8.7
        if RUBY_VERSION =~ /1.8/
            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', spawner.child_pid
                        spawner.spawn(action)
                    }
                    delete { |base, relative|
                        Spider.logger.debug("#{relative} deleted, restarting")
                        Process.kill 'KILL', spawner.child_pid
                        spawner.spawn(action)
                    }
                    create { |base, relative|
                        Spider.logger.debug("#{relative} created, restarting")
                        Process.kill 'KILL', 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
        else
            listener = Listen.to(Spider.paths[:apps], { :only => /\.rb/ } ) { |modified, added, removed|
                
                unless modified.blank?
                    Spider.logger.debug("#{modified.first} updated, restarting")
                    Process.kill 'KILL', spawner.child_pid
                    spawner.spawn(action)
                end
                unless added.blank?
                    Spider.logger.debug("#{added.first} created, restarting")
                    Process.kill 'KILL', spawner.child_pid
                    spawner.spawn(action)
                end
                unless removed.blank?
                    Spider.logger.debug("#{removed.first} deleted, restarting")
                    Process.kill 'KILL', spawner.child_pid
                    spawner.spawn(action)
                end
            }
            listener.start
            sleep
        end

    end
    
end

#run(action = nil) ⇒ Object



204
205
206
207
208
# File 'lib/spiderfw/http/server.rb', line 204

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

#spawn(action) ⇒ Object



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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/spiderfw/http/server.rb', line 210

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 = Proc.new{
                #QUESTO LOG FA COMPARIRE UN MESSAGGIO DI ERRORE IN DEVEL
                #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
        Signal.trap("TERM") do
            exit
        end
        Signal.trap("INT") do
            exit
        end
        Signal.trap("HUP") do
            exit
        end
        # 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