How to restart Rails from within Rails?

后端 未结 3 1606
轻奢々
轻奢々 2021-02-09 13:53

Ok, so I would like to create an action in Rails to restart itself. I did a little searching and found:

http://snippets.dzone.com/posts/show/5002

Which suggests

相关标签:
3条回答
  • 2021-02-09 14:29

    If you don't mind switching to mod_rails, you can restart your server by creating $RAILS_ROOT/tmp/restart.txt, which causes only the Rails instance you care about to restart.

    Your PS command looks (cursorary glance) like it will kill all rails processes on your box. That's fine if you are the only Rails app on a machine, but if there's a few running as the same user or you are running as root you'll kill them all. Bad form!

    This points it out for mongrel. There's the way you want to try.

    0 讨论(0)
  • 2021-02-09 14:30

    Ok I found a fix... I changed how I start rails to:

    mongrel_rails start -d
    

    and now the following action will do it:

    def restart
      fork { exec "mongrel_rails restart" }
      redirect_to "/server_maintenance"
    end
    

    As a caveat, the redirect_to will cause a failed load because the server will be down... however a reload after a pause will show that the restart was successful. This could be fixed by changing the restart to be done with AJAX, followed by a javascript reload... but I will leave that as an exercise to the reader.

    0 讨论(0)
  • 2021-02-09 14:37

    In our consulting with startups running their sites on Rails, we used two methods for managing mongrel processes.

    First, is a custom gem we wrote called mongrel_runit. This sets mongrels up as services in runit.

    Second, we used god to monitor mongrel processes. This will work with mongrel_runit, or with 'normal' mongrel configurations.

    0 讨论(0)
提交回复
热议问题