Can not open localhost by running Ruby in Rails application - ruby-on-rails

I am trying to run a ruby script in rails application using system call, like
def runSystemCall
system("ruby /path/to/ruby/script/watir.rb localhost:3000/articles/14")
end
and watir.rb:
def watir(url)
bb = Watir::Browser.new :chrome
bb.goto "#{url[0]}"
end
watir(ARGV)
when running this in rails application, a browser opened and stay in a status of Waiting for localhost..., until error
.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/net/protocol.rb:158:in `rescue in rbuf_fill': Net::ReadTimeout (Net::ReadTimeout)
loaded the website localhost:3000/articles/14 opened.
Dose anyone know why?
and, when run this in a terminal
$ ruby /path/to/ruby/script/watir.rb localhost:3000/articles/14
a browser opened and open website localhost:3000/articles/14
That's what I expected.

Your local server is single threaded. That means it can only handle one request at a time. If one local request wants to load an other page from the same local server, than you need at least two local threads.

Related

How to debug with Rubymine, Docker-Compose and multiple debug sessions?

I am experimenting with Rubymine's "Docker-Compose" integration. I normally do the "usual" debugging, by using the and "BUG"-buttons to start a debug session.
Normally I can start these sessions for the "rails server"- itself and one for debugging within the "rails console"- and the "rspec"-commands simultaneously. That's no problem when working "the old way" and works perfectly.
Old way means: running a
rails console AND rails server in debug session
MySQL
Elasticsearch
Redis
...
on my local machine.
But with the docker-compose-configuration I have to do that differently:
I followed that tutorial (https://www.jetbrains.com/help/ruby/using-docker-compose-as-a-remote-interpreter.html#configure_ruby_docker_integration) to set up a singular debug session, but I was not able to find out how to use more than one debug session at once. (adding rails console in debug session while rails server's debug session is still running). Somehow they are conflicting. It's no problem to start a regular rails console in Rubymine while a rails serverdebug session is running
All tutorials and videos I found were only covering the most basic cases, but not mine.
Is this possible?
Do I have to change the debug port / open another one for the second session? (I didn't found how to change the Rubymine attached debugger port, it's fixed to "3000:3000"), it is "hardcoded in the appended docker-compose.override.1234.yml-file.
My containers are running
Rails Development Server Configuration
Rails Console Configuration

Rails: How to run code when server starts up, but not when running a rake task or the console?

Before, I added code to a file called config/initializers/remote_publishers.rb which set up a connection to RabbitMQ using the Bunny gem on server startup.
However, this is now also executed when running rails c, rails g model SomeModel foo:integer, rails db:migrate etc.
For this app, the RabbitMQ-connection only makes sense when rails is started using rails s(erver).
What is the proper way to conditionally execute this code? Is there a way to see if Rails is starting as server, or only as task-runner?
What web server are you using? On Puma, for example, you can use
on_worker_boot do
# Establish RabbitMQ connection
end
Another possibility might be to check if defined?(Rails::Server) in your initializer: this should only be true when running in the context of the web server.

How to automatically launch browser when rails server starts?

Is there a way of automatically launching your browser and pointing it to http://localhost:3000 when you run rails server?
And secondly ensuring this only happens in development?
I would have thought that Launchy would have done the trick, but I'm struggling to work out where to put it.
I've tried adding an initialiser config/initializers/launchy.rb
require 'launchy'
Launchy.open("http://localhost:3000")
And this triggers the browser to open, create a new tab and visit http://localhost:3000 as expected, however it runs before the server / application has finished booting and I get a Cannot connect to Server error in the browser.
If I reload in the browser my app works just fine so I'm confident it's not a problem with my app, rather the timing of the Launch.open call.
I tried renaming launchy.rb to z_launchy.rb so it gets loaded last as per the docs but still the same problem. Launchy fires before the Application is ready for it.
I've also tried adding the code to config/puma.rb (I'm using puma as my server), to config/enviroments/developoment.rb but always the same problem. The Launchy.open command gets called to soon.
Where should I call Launchy.open("http://localhost:3000") to ensure that a) It runs after the application has loaded and is ready to receive requests; and b) So it only runs in development, not in test or production?
System set up OS X 10.11.3, Rails 4.2.5, ruby 2.2.1p85, puma version 2.15.3
Putting it into config.ru would work, after the run Rails.application line. By the time that returns, your app is ready to go. To run it only in development, check the Rails.env.
require ::File.expand_path('../config/environment', __FILE__)
run Rails.application
Launchy.open("http://localhost:3000") if Rails.env.development?
This is easy, but it feels dirty to me to have this sitting in your app. Better would be a small script that starts your server, waits a moment, then launches the browser.

Unable to start heroku with ruby on rails. Digest/SHA2 no such file

So i've been making a basic app on my local machine and I cant get it to transfer over to Heroku easily. The server crashes when I try to run it.
When I type heroku run rails s, it boots for a second and then spits out a huge stack trace.
At the top its says :
/app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/depenencies.rb:317:in 'rescue in depend_on': No such file to load -- Digest/SHA2 (loadError):
I use the SHA2 for authentication, and it seems to be crashing the application. does anyone have any suggestions?
It could be because it's a case-sensitive filesystem, where your local system isn't.
Try changing the require to this:
require 'digest/sha2'

Cramp and heroku

I have been playing around with Cramp to do some real time pushing of information in an app. Its all working great locally but when I push off to heroku I seem to be having issues with the ports.
I have a socket set up in cramp which inherits from websocket
class LiveSocket < Cramp::Websocket
and I also have a cramp action called home which basically just renders some erb for the home page
class HomeAction < Cramp::Action
in my route file I set up the following and also a static file server
Rack::Builder.new do
puts "public file at #{File.join(File.dirname(__FILE__), '../public')}"
file_server = Rack::File.new(File.join(File.dirname(__FILE__), 'public'))
routes = HttpRouter.new do
add('/').to(HomeAction)
get('/LiveSocket').to(LiveSocket)
end
run Rack::Cascade.new([file_server, routes])
end
Then on the client end the javascript connects to
var ws = new WebSocket("ws://<%= request.host_with_port %>/LiveSocket");
As I say locally it all works. We connect and start receiving notifications from the server. On heroku we run thin on the Cedar stack and have a profile which looks like
web: bundle exec thin --timeout 0 start -p $PORT
When I load up the site the page itself loads fine but on trying to connect the websocket I get an error which says
servername.herokuapp.com Unexpected response code: 200
I am guessing this has something to do with how heroku routes its requests but I do know that you can run a node.js websocket server on heroku so figure there must be a way to get this working too.
Thanks in advance for any help.
cheers
stuart
I don't think Heroku supports websockets :( http://devcenter.heroku.com/articles/http-routing#the_herokuappcom_http_stack

Resources