Terminate current Rails instance from initializer
ruby-on-rails, ruby-on-rails-4
Solution
there are three ways to end a ruby process:
- `exit` will end the process with status 0 (success).
- `exit!` will end with status 1 (which seems appropriate for your case), and also not run blocks defined using `Kernel#at_exit` (which may or may not be desirable for you).
- `abort` will end with status 1, and will run `Kernel#at_exit` blocks. You can also give it a string which will be sent to standard out.
So `abort` is probably your best bet. Unless you have a particular reason to not run `Kernel#at_exit` blocks you probably want to run them.
Since unicorn runs the initializers once and then forks processes after that, this approach should be fine.
I'll add that I'm guessing you are doing these checks at the "wrong" level -- if possible you should check the health of your environment outside of the context of rails. However I don't know your situation, and I realize that sometimes the quickness of a solution is more valuable than its elegance. :)
Problem
I'm writing a sanity-checking initializer which should kill the Rails process in the event certain properties are not met. What would be the safest way to abort a production server's startup process in this way? I suppose an extreme case is running `exit`, but I'm not sure if it's the best approach. Running this on unicorn if that matters.