I created an HTML app which is meant to be run locally using rackup config.ru -p 3000
in "production".
This app communicates with RabbitMQ via the Bunny gem, and publishes messages when I hit various endpoints via Ajax.
I also have two methods called init
and deinit
which setup and destroy global variables related to the RabbitMQ server: $connection
, $channel
and $exchange
.
These things are not meant to be done every single request though, in fact it crashes RabbitMQ at least 50% of the time.
What I need is to run init
when starting the server, and deinit
when I CTRL+C
out of it, so these global variables are created only a single time and are available for use by the rest of the app (across multiple requests).
I just have no idea how to go about doing this.
Here is how I'm defining my methods.
def init
$conn = Bunny.new(host: SETTINGS['host'])
$conn.start
$ch = $conn.create_channel
$x = $ch.default_exchange
end
def deinit
puts "\nExiting..."
$ch.close
$conn.close
end