In Rails, how do I run certain tasks on shutdown? - ruby-on-rails

I maintain a connection to a RabbitMQ server. When I safely turn the Rails Server off (via CTRL+C or whatever), how do I trigger a close to that connection?
I need an 'On shutdown' trigger. Is there one?

If you are using puma as your webserver you can add on_worker_shutdown as a callback and then execute a shell script or whatever else is needed to close the connection to RabbitMQ.

Related

ruby/rails grpc server restart without disconnecting clients

I am using the following code snippet to start a grpc server which works fine. But whenever I need to deploy new code to the server, what is the right way for me to restart the server? Should I just kill the server process, and let client to handle the error message? Or is there a way for enabling master/worker mode like unicorn does?
s = GRPC::RpcServer.new
s.run_till_terminated
There is no such support for rolling out new deployments that's built in to the ruby-gRPC.
However, it should be possible for applications with multiple server instances to do rolling restarts. E.g., note that if gRPC connects to a server and starts to make RPC's to it and that server gets shut down, then gRPC will internally notice that the connection went bad and it will try to make its next RPC on a newly connection (the default gRPC behavior will be to perform its next RPC on the next resolved address that can be successfully connected to, and this might mean reconnecting to the same address for which the connection just broke). Note too that gRPC servers use SO_REUSEPORT by default, so one could potentially run multiple servers on the same port.

How to quit a Ruby on Raily server?

Inside a test environment I am using the wash_out GEM within a Ruby on Rails server to run a SOAP service that serves as a Proxy between two different transfer protocols.
At the end of the test run, I would like to quit the Rails server with a dedicated SOAP request.
So I implemented such a request and called inside the request exit(0). But it seems to me that exit and abort functions are hooked away because of security or other reasons.
One might argue that terminating the service within a request is a little bit harsh, but at that point I do not care how the service gets terminated. Since it does not hold any state.
I would like to avoid to patch the Rails sources so that it is later easier to update the GEMSs.
Edit:
The server is running (unfortunately) on Windows.
You could execute shell script and kill running rails process. For example if you running service on unicorn you could do something like
def exit_app
%x(kill -9 $(cat tmp/pids/unicorn.pid))
end

WebSocket in initializers, doesn't start in detached mode

My webapp consits of a HTTP server and a WebSocket server, both running on Rails. For Websockets I am using em-websocket which I start in the initializers, like this:
Thread.new do
EventMachine.run do
EventMachine::WebSocket.run(EVENTCHAT_CONFIG) do |socket|
[...]
end
end
end if Rails.const_defined?(:Server)
This works fine when I start the server with 'rails s', but it doesn't work in detached mode ('rails s -d'). When I try to connect to the Websocket server via JS it tells me, that it is still in connecting state, so I guess something is blocking it.
I also think this might be related to the threading.
I also tried starting the server with thin and unicorn, but both fail to start the Websocket Server.
Am I going against the convention here?
I just made the switch to foreman, which enables me to start multiple ruby processes with one command. You just have to add a Procfile. For deployments you can export this to various init systems, like in my case upstart.
It doesn't work for me yet, though I do think this is the way to go.

DelayedJob fails silently when couldn't connect to database

So, i have a big rails application that is using delayed_job to send emails and SMS to the users.
Once in a while, the delayed_job process will simply stop working without any message on the logs. I have finally pinpointed the problem as the delayed_job process crashs when it coulnd't connect to the database.
Is there any configuration i can make so it will retry the connection instead of just crashing? I've tried setting the reconnect: true on the database.yml file with no success.
Another option that i'm looking for is maybe using a monitoring tool like god or bluepill.
Try using the -m flag when you start delayed job - that should start a separate monitor process that in my experience has been very good about restarting the process.

Java Service Wrapper & shutdown command

I use JSW to wrap HSQLdb server. HSQL docs say I must connect to server and execute "shutdown" command to terminate server correctly. Is there any way to execute such shutdown command from within wrapper when I use standard service management commands like "net service stop" or button "stop" in service properties?
not quite sure what you are trying to accomplish, but assume that you are running the HSQL server as service and you want to connect from your application to it and when your application stops, it should also stop the HSQL server?
If that's so, then please take a look at the following page:
http://www.informit.com/guides/content.aspx?g=java&seqNum=619
cheers,
edit:
registering the code to cleanly shutdown HSQL as shutdown hook should work for you I think. Furthermore you can also do this with the WrapperListener.stop function (following Integration Method #3)
cheers,

Resources