Class: Spider::HTTP::Spawner
Instance Attribute Summary collapse
-
#child_pid ⇒ Object
readonly
Returns the value of attribute child_pid.
Instance Method Summary collapse
-
#initialize(actions) ⇒ Spawner
constructor
A new instance of Spawner.
- #monitor_fs ⇒ Object
- #run(action = nil) ⇒ Object
- #spawn(action) ⇒ Object
Constructor Details
#initialize(actions) ⇒ Spawner
Returns a new instance of Spawner.
145 146 147 |
# File 'lib/spiderfw/http/server.rb', line 145 def initialize(actions) @actions = actions end |
Instance Attribute Details
#child_pid ⇒ Object (readonly)
Returns the value of attribute child_pid.
143 144 145 |
# File 'lib/spiderfw/http/server.rb', line 143 def child_pid @child_pid end |
Instance Method Details
#monitor_fs ⇒ Object
194 195 196 197 198 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 |
# File 'lib/spiderfw/http/server.rb', line 194 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
149 150 151 152 153 |
# File 'lib/spiderfw/http/server.rb', line 149 def run(action=nil) @monitor_thread = monitor_fs spawn(action) @monitor_thread.join end |
#spawn(action) ⇒ Object
155 156 157 158 159 160 161 162 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 |
# File 'lib/spiderfw/http/server.rb', line 155 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.main_process_startup $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] @actions[action].call end end |