What is the best option to start and stop the eventmachine server from a Ruby on Rails application

CORRECTION:

In particular, I want to start and stop EventMachine (EM) from the Ruby on Rails controller.

(I was told that Slim would work well for this.)

Note: This EM server is SEPARATE from the Mongrel server running the Ruby on Rails application. (The EM server accepts connections from the Arduino microcontroller.)

Running "Thin" as a server. I MUST be able to accept both HTTP requests and Arduino connections.

# Starts Server def start_control_server EventMachine::run { @EchoServer = EventMachine::start_server "0.0.0.0", 40013, EchoServer } end # Attempts ( fails ) to stop server def stop_control_server EventMachine.stop_server(@EchoServer) end 

If you recommend servers other than direct EventMachine, please provide code to execute the above code.

The full controller code is available here: http://pastie.org/1698383

+4
source share
1 answer

I assume that you are not calling start_control_server and stop_control_server inside another controller method. This means that your instance variable (@EchoServer) will not exist when you call stop.

One solution could be to keep the identifier returned from start_server in the session. As in

  def start_control_server session[:em_server_id] = EventMachine::start_server "0.0.0.0", 4000, EchoServer end def stop_control_server EventMachine.stop_server(session[:em_server_id]) if session[:em_server_id] session[:em_server_id] = nil end 

In addition, if you use the rails application using thin ones, then you are already in the eventmachine loop, so you do not need to call EventMachine :: run. Calling EventMachine.stop_server does not seem to disconnect anything that is already connected, but stops further connections from the specified port.

Hope this will be helpful!

+1
source

Source: https://habr.com/ru/post/1344723/


All Articles