What is the most accurate way to check if you are in rails server process for the needs of Hyperstack - ruby-on-rails

Hyperstack is an isomorphic framework where same code can run server or client side. So there are specific cases where depending on where some piece of code gets executed (server or client side) different things should be accomplished (client synchronization etc.).
The problem is that relying on the default check if
defined?(Rails::Server)
depends on the webserver you are running and the enclosing environment.
For example i run on puma (in docker for development and in Ubuntu server for production) and even in that case defined?(Rails::Server) works fine in development but not in production. This reveals that server execution detection depends not only on the actual server you are running on, but also on the method used to start it (e.x. rails s VS puma start)
Additional information can be found here:
Detect if application was started as HTTP server or not (rake task, rconsole etc)
https://gitter.im/ruby-hyperloop/chat?at=59d60f2201110b72317cd61c
https://hyperstack-org.slack.com/archives/CHRQ5U8HL/p1557262851049900
Is there a standard way to check whether something in Rails is executing on the server process/thread (not in browser, some sort of client, console, migration, rake task etc.) without relying on some hack to identify or declare what server we deploy on (puma, thin, nginx etc.)?

You can use the RUBY_ENGINE guard to see if the code is running in Opal or not.
if RUBY_ENGINE == 'opal'
# I am on the client
end
This is very useful in your Isomorphic models to exclude parts of the model's methods from existing on the client. Also very useful for using added Gem methods which make no sense in the client code.

Related

Rails caching not working on server

In my application, I have used caching. This is the code, I have used. In after_filter, I called the method which include this one line code.
Rails.cache.write("properties", #properties.to_xml)
I try to get this in another action in before_filter like
#hotels = Rails.cache.fetch("properties")
this all working fine in development machine. But in server it returns null value. the application run in same development mode in server. Can you please anyone suggest me the right way. Thanks in advance.
It sounds like you haven't configured a backend for the store, so it will use ActiveSupport::Cache::MemoryStore
From the documentation:
If you're running multiple Ruby on Rails server processes (which is the case if you're using mongrel_cluster or Phusion Passenger), then this means that Rails server process instances won't be able to share cache data with each other.
This works in development since you are likely using a single server instance, so the cache is only stored in one process. For production you need to configure an alternative shared store. I'd recommend running a memcached instance, and installing and using the Dalli Gem as per the README.

How to run rails request specs on a cloud deployment?

I am new to rails testing. Two days of running down leads with Google has turned up no solutions for what ought to be a frequent need.
If I write request (integration) specs to use a Selenium or other browser-based driver, is it possible to redirect the test's i/o to a staging deployment on a cloud server (in my case Heroku)?
If so, how? If not, what prevents this from working?
So far I have been using rspec/capybara, but would switch to anything of similar power if necessary.
You can use Capybara with Selenium driver, and set Capybara.app_host to specify the IP of you staging app server. While doing so you can turn off Capybara local rack with Capybara.run_server = false
Remote testing will allow you only to perform human kind of action and test the returned generated HTML/JS/Json etc .. but no access to controller, view, or any other app internal objects.
On thing you could do (I never tried, but I don't see why it wouldn't work) is to set-up your database.yml test configuration to remotely access you staging database, allowing you to control the database during your tests. It's not really secure so you may want to do that over a SSH tunnel , or a similar solution.

Rails development: how to respond to several requests at once?

I have inherited the maintenance of a legacy web-application with an "interesting" way to manage concurrent access to the database.
The application is based on ruby-on-rails 2.3.8.
I'd like to set up a development environment and from there have two web browser make simultaneous requests, just to get the gist of what is going on.
Of course this is not going to work if I use Webrick, since it services just one http request at a time, so all the requests are effectively serialized by it.
I thought that mongrel could help me, but
mongrel_rails start -n 5
is actually spawning a single process and it seems to be single-threaded, too.
What is the easiest way of setting my development environment so that it responds to more than one request at a time? I'd like to avoid using apache and mod_passenger because, this being development, I'd like to be able to change the code and have it reloaded automatically on the next request.
In development mode, mod_passenger does reload classes and views. I use passenger exclusively for both development and deployment.
In production, you can (from the root of the rails app):
touch tmp/restart.txt
and passenger will reload the app.
Take a look at thin
http://code.macournoyer.com/thin/

em-websocket gem with Ruby on Rails

I started developing a web-socket based game using the em-websocket gem.
To test the application I start the server by running
$> ruby server.rb
and then I just open two browsers going directly to the html file (no web server) and start playing.
But now I want to add a web server, some database tables, an other Ruby on Rails based gems.
How an achieve communication between my web-socket server and my Ruby on Rails application? Should they run in the same server and run as a single process? Run in separate servers and communicate through AJAX?
I need to support authentication and other features like updating the database when a game is finished, etc.
Thanks in advance.
There is an issue created about this:
https://github.com/igrigorik/em-websocket/issues/21
Here is the deal. I also wanted to develop a websocket server client with ruby on rails framework. However ruby-on-rails is not very friendly with eventmachine. I have struggeled with having a websocket client, so I managed to copy/cut/paste with from existing lib, and end up with the following two escessential ones.
Em-Websocket server
https://gist.github.com/ffaf2a8046b795d94ba0
ROR friendly websocket client
https://gist.github.com/2416740
have the server code in script directory, the start like the following in ruby code.
# Spawn a new process and run the rake command
pid = Process.spawn("ruby", "web_socket_server.rb",
"--loglevel=debug", "--logfile=#{Rails.root}/log/websocket.log",
:chdir=>"#{Rails.root}/script") #,
:out => 'dev/null', :err => 'dev/null'
Process.detach pid # Detach the spawned process
Then your client can be used like this
ws = WebSocketClient.new("ws://127.0.0.1:8099/import")
Thread.new() do
while data = ws.receive()
if data =~ /cancel/
ws.send("Cancelling..")
exit
end
end
end
ws.close
I wish there is a good ROR friendly em-websocket client, but couldn't fine one yet.
Once you made server/client works well, auth. and database support must not be very different from other rails code. (I mean having client side with some auth/db restrictions)
I am working on a gem that may be helpful with your current use case. The gem is called websocket-rails and has been designed from the ground up to make using WebSockets inside of a Rails application drop dead simple. It is now at a stable release.
Please let me know if you find this helpful or have any thoughts on where it may be lacking.

Why does the server need to restart when a model file is updated?

I'd like to know why is there a need to restart the server (Mongrel/WEBrick) every time a model file is updated? I know it doesn't get loaded in if you don't do it - but is there any documentation out there that would explain why it does so?
Thanks!
Development environments do not require you to restart the server if you change a model. They will reload the environment for each request if necessary.
Production environments are a different story. A Rails server (mongrel/passenger/webrick/etc) running in a production environment will only load your Rails environment once when the process is started. This takes a couple of seconds, as you might notice when starting the console which also loads your Rails environment. To avoid this overhead for each request the server will spawn a new thread from the loaded environment to handle each incoming request.
Because the server only responds to HTTP requests, and the usual signals. There's no good way to force an environment reload beyond always loading a fresh environment (like a development environment, or restarting the server.

Resources