Rails caching not working on server - ruby-on-rails

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.

Related

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

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.

Sharing session data between Rails and Node?

The main question is: Can I read Rails session data in Node?
More details:
I have a project that is written in Ruby on Rails. It works but I want to add to it and eventually replace it using NodeJS. Both are running on the same server, just on different ports.
As of now RoR will serve up all the HTML files (and continue handeling the existing functionality) and then I'll connect to the Node server via AJAX. Node will just dish up JSON for the time being.
The problem is, how can I work with session variables between the two? More specifically, can I get to RoRs session variables in Node? Mostly I just need to know which user is logged in.
If it matters, I am running Rails 2.3.5, Ruby 1.8.7, and Node 0.8.17.
I haven't tried exactly same stuff, myself, but, we did something similar but with Sinatra and Java.
I wouldn't comment about your approach on application design, but, in case you don't mind using Memcached session store in your rails application, yes it is possible. Configuring Memcached with Ruby app is explained on Heroku Doc
In Node application you can use Memcached Client like 3rd-Eden and access session variable from memcache
You would have to explicitly pass session id generated by rails to Node.

How does Rails handle concurrent request on the different servers?

This has been asked before, but never answered particularly exhaustively.
Let's say you have Rails running on one of the several web servers that support it, such as WEBrick, Mongrel, Apache and Nginx (through Passenger Phusion). The server receives two concurrent GETs, what happens? Is this clearly documented anywhere?
Basically I'm curious:
Is a new instance or rails is created by the server every time?
Does it somehow try to re-use existing instances (ruby processes with Rails already loaded in it?) to handle the request?
Isn't starting a new ruby process and re-loading Rails in it pretty slow?
Thanks! Any links to exhaustive clarifications would be greatly appreciated.
Some use workers (apache, phusion, unicorn), some don't. If you don't
use workers, it really depends wherever your application is threadsafe
or not. If you are, more than one request may be served at a time,
otherwise there's Rack::Lock which blocks that. If there are workers
(separate processes), each of them does a request then goes back to
the pool where the master assigns it a new request. Read
on

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.

Resources