Failed to get session options? - ruby-on-rails

I'm trying to switch the session store in Rails 3 by changing the
config/application.rb as following:
config/application.rb
#-----------------------------------
memcache_options = {
:compression => true,
:debug => false,
:namespace => "xx-cache",
:readonly => false,
:urlencode => false
}
CACHE = MemCache.new(memcache_options)
CACHE.servers = ['127.0.0.1:17898']
#check if CACHE is connected
#puts CACHE
config.action_dispatch.session = {
:session_key => '_xx_session',
:secret => 'xx',
:cache => CACHE,
:expires => 900
}
config.action_dispatch.session_store = :mem_cache_store
#-----------------------------------
Memcache server is running. However, when run rails s, i got this
message:
=> Booting WEBrick
=> Rails 3.0.0.beta3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.0.beta3/lib/
action_dispatch/middleware/session/mem_cache_store.rb:19:in
`initialize': #<ActionDispatch::Session::MemCacheStore:0xa302950>
unable to find server during initialization. (RuntimeError)
It seems the session options was not passed correctly. But i'm not
sure what's wrong here cause I'm new to Rails.
Any help will be appreciated.
Thanks

I'm not -entirely- sure, but I think you need to include "cached_model." Try adding
require 'cached_model' #(At the top)
Let me know if that worked.

Related

Warning! ActionDispatch::Session::CacheStore failed to save session. Content dropped. in logfile

I'm running a Rails 4 application and I'm seeing this in my production logs on Heroku. I see it frequently and some users are reported getting logged out and I don't see anything in the logs that would indicate why it's happening. I don't see any information from dalli that it cannot save. For most users, it works just fine. Any tips on how to hunt this down?
Error Message in Production Logs
Warning! ActionDispatch::Session::CacheStore failed to save session. Content dropped.
My configuration in production
config.cache_store = :dalli_store,
(ENV["MEMCACHIER_SERVERS"] || "").split(","),
{:username => ENV["MEMCACHIER_USERNAME"],
:password => ENV["MEMCACHIER_PASSWORD"],
:failover => true,
:socket_timeout => 1.5,
:socket_failure_delay => 0.2
}
config.session_store = :mem_cache_store
I found the problem that was creating this error, I'm using a custom header variable to set the session ID and the client was sending nil which was causing rack to not be able to store to a nil session ID. I wrapped the call in a blank? check to ensure it's only set if there is a valid value and that fixed my issue.
if(!request.headers["Session"].blank?)
request.session_options[:id] = request.headers["Session"]
end

How to distinguish between Rails and Rake at the initializers level?

Whether I execute rails or rake, the code present in the initializers folder will be executed. What I want to do is to make sure that the code in a given initializer file doesn't get fired up when running a rake task (hence only when Rails server or console are launched).
For example, I'd like to be able to do something like (in config/initializers/blabla.rb):
do_something # code always executed
unless ENV['rake']
something_when_not_rake # code executed only if rails server or console
end
do_something_more # more code always executed
I've been looking into the ENV variable to see what's available there but I've seen nothing interesting, here's the content (in the format "key" => "value"):
"rvm_bin_path" => "/Users/lucke84/.rvm/bin"
"GEM_HOME" => "/Users/lucke84/.rvm/gems/ruby-2.2.3"
"SHELL" => "/bin/bash"
"TERM" => "xterm-256color"
"TMPDIR" => "/var/folders/73/q8t0jjrx3zb8zvd072dgq11c0000gn/T/"
"IRBRC" => "/Users/lucke84/.rvm/rubies/ruby-2.2.3/.irbrc"
"Apple_PubSub_Socket_Render" => "/private/tmp/com.apple.launchd.mF5Uh4NPbk/Render"
"MY_RUBY_HOME" => "/Users/lucke84/.rvm/rubies/ruby-2.2.3"
"USER" => "lucke84"
"rvm_stored_umask" => "0022"
"_system_type" => "Darwin"
"rvm_path" => "/Users/lucke84/.rvm"
"SSH_AUTH_SOCK" => "/private/tmp/com.apple.launchd.ryhqrdVtVU/Listeners"
"__CF_USER_TEXT_ENCODING" => "0x1F5:0x0:0x0"
"rvm_prefix" => "/Users/lucke84"
"PATH" => "/Users/lucke84/.rvm/gems/ruby-2.2.3/bin:/Users/lucke84/.rvm/gems/ruby-2.2.3#global/bin:/Users/lucke84/.rvm/rubies/ruby-2.2.3/bin:/Users/lucke84/.rvm/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/apache-maven/apache-maven-3.3.1/bin"
"rvm_loaded_flag" => "1"
"PWD" => "/Users/lucke84/projects/website"
"JAVA_HOME" => "/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home"
"XPC_FLAGS" => "0x0"
"_system_arch" => "x86_64"
"_system_version" => "10.11"
"XPC_SERVICE_NAME" => "0"
"rvm_version" => "1.26.11 (latest)"
"HOME" => "/Users/lucke84"
"SHLVL" => "1"
"rvm_ruby_string" => "ruby-2.2.3"
"LOGNAME" => "lucke84"
"LC_CTYPE" => "en_US.UTF-8"
"GEM_PATH" => "/Users/lucke84/.rvm/gems/ruby-2.2.3:/Users/lucke84/.rvm/gems/ruby-2.2.3#global"
"rvm_delete_flag" => "0"
"RUBY_VERSION" => "ruby-2.2.3"
"_system_name" => "OSX"
"rvm_user_install_flag" => "1"
"BUNDLE_GEMFILE" => "/Users/lucke84/projects/website/Gemfile"
"_ORIGINAL_GEM_PATH" => "/Users/lucke84/.rvm/gems/ruby-2.2.3:/Users/lucke84/.rvm/gems/ruby-2.2.3#global"
"BUNDLE_BIN_PATH" => "/Users/lucke84/.rvm/gems/ruby-2.2.3/gems/bundler-1.10.6/bin/bundle"
"RUBYOPT" => "-rbundler/setup"
"RUBYLIB" => "/Users/lucke84/.rvm/gems/ruby-2.2.3/gems/bundler-1.10.6/lib"
"MANPATH" => "/Users/lucke84/.rvm/gems/ruby-2.2.3/gems/unicorn-5.0.1/man"
I cannot find a built-in environment key/value to programmatically determine who is triggering the command. Maybe one of you can shed some light on this topic for me?
Further details:
this cannot be an environment-dependent check, it should work for development as well as for production (or whatever the environment)
I would like to avoid to specify a custom parameter (i.e. bin/rake my:task skip_initializer=true), as what I'm trying to do is to skip that initializer's executions for all rake tasks
running Rails 4.2.x at the moment
Thanks in advance for your help!

Heroku request timeout when Memcachier reaches cache limit

I have a Rails app deployed to Heroku using Memcachier (Dalli as client). I'm using the free add-on (which offers a 25 MB cache).
We started to receive request timeouts from heroku and, after debugging, we found out that manually flushing Memcachier solved the problem.
Timeouts occur when Memcachier reaches levels near its limit, like 20 MB (when limit is 25 MB).
Why Memcachier doesn't free cache space with time? Is there any missing configuration to tell Memcachier to flush when cache reach certain size?
My conf:
application.rb
config.cache_store = :dalli_store
production.rb
client = Dalli::Client.new
config.action_dispatch.rack_cache = {
:metastore => client,
:entitystore => client,
:allow_reload => false
}
Perhaps try updating your production.rb to include socket_timeout and socket_failure_delay options.
require 'dalli'
cache = Dalli::Client.new((ENV["MEMCACHIER_SERVERS"] || "").split(","),
{:username => ENV["MEMCACHIER_USERNAME"],
:password => ENV["MEMCACHIER_PASSWORD"],
:failover => true,
:socket_timeout => 1.5,
:socket_failure_delay => 0.2
})
-- OR --
If you are using Puma, you need to have the connection_pool gem installed.
Gemfile
gem 'connection_pool'
And this configuration in your production.rb
config.cache_store = :dalli_store,
(ENV["MEMCACHIER_SERVERS"] || "").split(","),
{:username => ENV["MEMCACHIER_USERNAME"],
:password => ENV["MEMCACHIER_PASSWORD"],
:failover => true,
:socket_timeout => 1.5,
:socket_failure_delay => 0.2,
:pool_size => 5
}

Chaining Webrick Proxies

I have been trying to test the chaining of Webrick proxies, and I am having some trouble.
Each proxy works fine on its own from 127.0.0.1:port (when :ProxyURI is commented out for proxy_2), but I am getting the error:
ERROR unsupported method `GET'.
from proxy_2 output (httpproxy.rb) when I try chaining them.
To clarify, when I chain them I am using 127.0.0.1:8086 as my access point from another application.
Looking at the logs for proxy_1, it appears that it is not receiving any requests.
Any help would be much appreciated.
require 'webrick'
require 'webrick/httpproxy'
port_1 = 8085
port_2 = 8086
proxy_1 =
WEBrick::HTTPProxyServer.new(
:Port => port_1,
:ServerType => Thread,
:Logger => WEBrick::Log.new("./logs/#{port_1}.out"),
:ServerName => "future_authentication_proxy"
)
proxy_1.start
proxy_2 =
WEBrick::HTTPProxyServer.new(
:Port => port_2,
:ProxyURI => '127.0.0.1:'+port_1.to_s
)
trap("INT"){
proxy_1.shutdown
proxy_2.shutdown
}
proxy_2.start
You passed wrong ProxyURI option, it should be something like:
:ProxyURI => URI.parse("http://#{host_1_ip}:#{port_1}/")

Rails 3 additional session configuration options (key, expires_after, secure)

Can someone point out what the new Rails 3.x session configuration options are?
I'm trying to duplicate the same configuration that I have in my Rails 2.3.x application.
This is the configuration that I used in the application:
#environment.rb
config.action_controller.session_store = :active_record_store
config.action_controller.session = {
:key => '_something', #non-secure for development
:secret => 'really long random string'
}
# production.rb - override environment.rb for production
config.action_controller.session = {
:key => '_something_secure',
:secret => 'really long random string',
:expire_after => 60*60,#time in seconds
:secure => true #The session will now not be sent or received on HTTP requests.
}
However, in Rails 3.x, I can only find mention of the following:
AppName::Application.config.session_store :active_record_store
AppName::Application.config.secret_token = 'really long random string'
AppName::Application.config.cookie_secret = 'another really long random string'
Are there other config settings to control the key, expire_after time, and secure option?
Regarding the latter, if "config.force_ssl = true" is set in production.rb, I assume the secure option is no longer required?
Thanks very much!
You now configure the Cookie-based session store through an initializer, probably in config/initializers/session_store.rb. In Rails 3 the session store is a piece of middleware, and the configuration options are passed in with a single call to config.session_store:
Your::Application.config.session_store :cookie_store, :key => '_session'
You can put any extra options you want in the hash with :key, e.g.
Your::Application.config.session_store :cookie_store, {
:key => '_session_id',
:path => '/',
:domain => nil,
:expire_after => nil,
:secure => false,
:httponly => true,
:cookie_only => true
}
(Those are just the standard defaults)
If you force SSL in production then setting secure on the cookie shouldn't really make a difference in practice, but you might want to set it just to be on the safe side...
Your::Application.config.session_store :cookie_store, {
:key => '_session_id',
:secure => Rails.env.production?
}

Resources