max pleaner max pleaner - 2 months ago 15
Ruby Question

Headless operations don't work inside Sinatra route

I am using the

gems to launch a headless Firefox browser:

headless =
video: {
frame_rate: 12,
codec: 'libx264'
driver = Selenium::WebDriver.for(:firefox)

With this code I can write the following:"")

Yet the following raises an error after I visit '/' in the browser:

get '/' do"")
erb :root

The error is as follows:

*** Errno::ECONNREFUSED Exception: Failed to open TCP connection to (Connection refused - connect(2) for "" port 7055)

I'm pretty sure this is because the
is not being called in the headless scope, therefore it can't connect to the Firefox instance.

I have also tried using the modular sinatra style, but the same error occurred.


What I ended up doing is separating the headless server in a separate script. This script has
and gets input, printing the output of running the command in the headless scope. Then from the sinatra server, i use
to instantiate the server and pass around its stdin and stdout so I can use it in my Sinatra app. This way the headless script is only run once (therefore multiple headless servers aren't started) and I can connect to it from my Sinatra routes. I can't interact with the headless script's variables or methods directly - I need to just use i/o.

I am hoping for an answer which hows how to make the original code work, though (when the sinatra app's routes are called in headless scope)


One possible trick that can help:

this = self
get '/' do"")
  erb :root

What is really important, the headless browser is to be started after the sinatra route was hit. So putting start_headless and start_driver methods in a separate class and invoking them when appropriate would do a trick.