ruby on rails - Does foreman restart processes gracefully? -


i'm use foreman upstart export in production start infinite rake tasks.

my humble "daemons" simple this:

task magic: :environment   loop     make_stuff_happen      sleep 10   end end 

i'd know if foreman doesn't kill processes when asked restart (eg. capistrano deploy), since don't want corrupt operations occur because of this.

and if how prevent it?

if export, may have file on configuration app-jekyll-1.conf contain this

start on starting app-jekyll                                                                                                stop on stopping app-jekyll respawn  exec su - username -c 'cd /some/directory; export port=5000; bundle exec jekyll serve -w >> /tmp/a    pp.log/jekyll-1.log 2>&1' 

which configuration of ubuntu upstart.

now when restart service upstart, call initctl restart

http://upstart.ubuntu.com/cookbook/#restart

calling initctl restart send sigterm signal job

https://serverfault.com/questions/189780/what-signal-does-upstart-initctl-use-to-restart-a-job

now cleaning resource depending whether program clean-up when receive sigterm signal.

the question if foreman kill rake process when asked restart, answer yes kill process sending sigterm rake process.

by default when rake received sigtem, exit process.

for example, if rakefile

def do_some_magic   puts "doing magic.."   sleep 5 end  task :magic   puts "running task wit pid #{$$}"   loop     do_some_magic      sleep 5   end end 

and if run it

$ rake magic running task wit pid 29527 doing magic.. 

on separate terminal, can send sigterm rake process given pid. , throw exception

$ kill 29527 

and on previous terminal get

...                          doing magic..                                             rake aborted!                                                  sigterm                                                        /tmp/foo/rakefile:11:in `sleep'                                /tmp/foo/rakefile:11:in `block (2 levels) in <top (required)>' /tmp/foo/rakefile:8:in `loop'                                  /tmp/foo/rakefile:8:in `block in <top (required)>'             tasks: top => magic                                            (see full trace running task --trace) 

so that's rake died. stop everything. may leave inconsistent state if do_some_magic performing db operation example.

you can of course handle signal perform clean up. how can handle if want wait until do_some_magic finished

def do_some_magic                             puts "doing magic.."                   sleep 5                                   end                                          task :magic                                puts "running task wit pid #{$$}"       loop                                       do_some_magic                                if $shutdown                                  puts "shutting down..., exiting loop"       break                                     end                                       end                                       end                                          trap "term"                                puts "sig term received."                   $shutdown = true                          end                                         

and again run it..

$ rake magic running task wit pid 30150                      doing magic..                                  

and on separater terminal kill process pid again

$ kill 30150 

on previous terminal process this

.... doing magic..              sig term received.              shutting down..., exiting loop  

conclusion

  • foreman export create ubuntu upstart script.
  • restart mechanism handled os. sending sigterm process.
  • it depend on actual process maintain state of system.
  • rake default doesn't handle sigterm. have self make sure if task interrupted, clean-up make sure state of system not corrupted

hopes help


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -