change rails environment regarding to request header - ruby-on-rails

I'm pretty new in rails and I would like to ask different database regarding to the origin of the request.
Something like :
http://localhost:80 env: production database: production
http://localhost:8080 env: development database: development
http://localhost:8081 env: test database: test
I already have my three environments defined. I just want to switch !
According to this post, in my application.rb I could adapt
Rails.env = ActiveSupport::StringInquirer.new('production')
to
# application.rb
if request.headers['HTTP_ORIGIN'] == 'http://localhost:80'
Rails.env = ActiveSupport::StringInquirer.new('production')
elsif request.headers['HTTP_ORIGIN'] == 'http://localhost:8080'
Rails.env = ActiveSupport::StringInquirer.new('development')
elsif request.headers['HTTP_ORIGIN'] == 'http://localhost:8081'
Rails.env = ActiveSupport::StringInquirer.new('test')
but it seems I haven't access to method headers :
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/railtie/configurable.rb:30:in `method_missing': undefined method `request' for #<App::Application:0x3e9c088> (NoMethodError)
from C:/Sites/KML-backend/config/application.rb:88:in `<class:Application>'
from C:/Sites/KML-backend/config/application.rb:26:in `<module:App>'
from C:/Sites/KML-backend/config/application.rb:25:in `<top (required)>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:53:in `require'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:53:in `block in <top (required)>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

I don't really see why you would want to do this, but how about starting three servers instead?
RAILS_ENV="production" rails s -p 80
RAILS_ENV="development" rails s -p 8080
RAILS_ENV="test" rails s -p 8081

Related

Rails - switching my rails app DB from sqlite3 to postgresql

I've create a RoR application with default database sqlite3. but the problem is sqlite3 is supported on heroku (deploying my app on cloud).
It's been whole day but i can't be able to connect my rails app to postgresql.
This is the steps I've followed till now to connect my app to postgresql
Replace: gem 'sqlite3', '~> 1.4'
with: gem 'pg'
then: bundle install
successfully install all the gems
after that convert my db to postgresql
conflict config/database.yml
Overwrite D:/New folder/amazon-product/config/database.yml? (enter "h" for help) [Ynaqdhm]
force config/database.yml
gsub Gemfile
gsub Gemfile
in that it's overwrite my existing database.yml file to postgresql
after that when i enter command
rake db:create db:migrate
it giving me the error
rails db:create db:migrate
rake aborted!
Bundler::GemRequireError: There was an error while trying to load the gem 'pg'.
Gem Load Error is: AddDllDirectory failed for C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/x64-mingw32Backtrace for gem load error is:
C:/Ruby30-x64/lib/ruby/site_ruby/3.0.0/ruby_installer/runtime/dll_directory.rb:83:in `add_dll_directory_winapi'
C:/Ruby30-x64/lib/ruby/site_ruby/3.0.0/ruby_installer/runtime/dll_directory.rb:50:in `initialize'
C:/Ruby30-x64/lib/ruby/site_ruby/3.0.0/ruby_installer/runtime/singleton.rb:12:in `new'
C:/Ruby30-x64/lib/ruby/site_ruby/3.0.0/ruby_installer/runtime/singleton.rb:12:in `add_dll_directory'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/pg.rb:15:in `block in <main>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/pg.rb:26:in `rescue in <main>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/pg.rb:4:in `<main>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/loaded_features_index.rb:100:in `register'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bundler-2.2.33/lib/bundler/runtime.rb:60:in `block (2 levels) in require'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bundler-2.2.33/lib/bundler/runtime.rb:55:in `each'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bundler-2.2.33/lib/bundler/runtime.rb:55:in `block in require'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bundler-2.2.33/lib/bundler/runtime.rb:44:in `each'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bundler-2.2.33/lib/bundler/runtime.rb:44:in `require'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bundler-2.2.33/lib/bundler.rb:175:in `require'
D:/New folder/amazon-product/config/application.rb:7:in `<top (required)>'
D:/New folder/amazon-product/Rakefile:4:in `require_relative'
D:/New folder/amazon-product/Rakefile:4:in `<top (required)>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/rake_module.rb:29:in `load'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/rake_module.rb:29:in `load_rakefile'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:710:in `raw_load_rakefile'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:104:in `block in load_rakefile'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:103:in `load_rakefile'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:82:in `block in run'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:80:in `run'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
C:/Ruby30-x64/bin/rake:25:in `load'
C:/Ruby30-x64/bin/rake:25:in `<main>'
Bundler Error Backtrace:
D:/New folder/amazon-product/config/application.rb:7:in `<top (required)>'
D:/New folder/amazon-product/Rakefile:4:in `require_relative'
D:/New folder/amazon-product/Rakefile:4:in `<top (required)>'
Caused by:
RubyInstaller::Runtime::DllDirectory::WinApiError: AddDllDirectory failed for C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/x64-mingw32
D:/New folder/amazon-product/config/application.rb:7:in `<top (required)>'
D:/New folder/amazon-product/Rakefile:4:in `require_relative'
D:/New folder/amazon-product/Rakefile:4:in `<top (required)>'
Caused by:
LoadError: 126: The specified module could not be found. - C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/pg_ext.so
D:/New folder/amazon-product/config/application.rb:7:in `<top (required)>'
D:/New folder/amazon-product/Rakefile:4:in `require_relative'
D:/New folder/amazon-product/Rakefile:4:in `<top (required)>'
(See full trace by running task with --trace)
my database.yml
# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
# gem install pg
# On macOS with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: amazon_product_development
# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user running Rails.
#username: amazon_product
# The password associated with the postgres role (username).
#password:
# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost
# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432
# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: amazon_product_test
# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="postgres://myuser:mypass#localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV['MY_APP_DATABASE_URL'] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
<<: *default
database: amazon_product_production
username: amazon_product
password: <%= ENV['AMAZON_PRODUCT_DATABASE_PASSWORD'] %>
I've already installed postgresql also create a new database table called 'details'
hierarchy of pgadmin 4
server --> postgresql 14 --> databases(2) --> postgres, details(which i want to use)

Devise config modification based on subdomain / request

I'm wondering how I can go about something like this in the devise config.rb.
if request.subdomain == 'app'
config.case_insensitive_keys = [:username]
elsif request.subdomain == 'support'
config.case_insensitive_keys = [:email]
end
The use here is that I want system users to only be able to login with their employee_id's (username) because their email addresses can appear across multiple accounts so I needed a way to log them in and register without the devise email validations catching them, however I want the backend group to login only by email (me and future devs) as there should never be more than one user there with multiple emails
this is failing when I try to drop my database for some reason?
STACK TRACE
/Users/taurenltd/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activerecord-6.0.2.2/lib/active_record/type.rb:27: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/taurenltd/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activerecord-6.0.2.2/lib/active_record/type/adapter_specific_registry.rb:9: warning: The called method `add_modifier' is defined here
rails aborted!
SyntaxError: /Users/taurenltd/Desktop/Development/roadze-base/config/initializers/devise.rb:301: syntax error, unexpected end-of-input, expecting `end'
/Users/taurenltd/Desktop/Development/roadze-base/config/environment.rb:5:in `<main>'
/Users/taurenltd/Desktop/Development/roadze-base/bin/rails:9:in `<top (required)>'
/Users/taurenltd/Desktop/Development/roadze-base/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:drop => db:load_config => environment
(See full trace by running task with --trace)
taurenltd#Shawns-MBP roadze-base % rails db:drop db:create db:migrate
/Users/taurenltd/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activerecord-6.0.2.2/lib/active_record/type.rb:27: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/taurenltd/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activerecord-6.0.2.2/lib/active_record/type/adapter_specific_registry.rb:9: warning: The called method `add_modifier' is defined here
rails aborted!
NameError: undefined local variable or method `request' for main:Object
Did you mean? require
/Users/taurenltd/Desktop/Development/roadze-base/config/initializers/devise.rb:55:in `block in <main>'
/Users/taurenltd/Desktop/Development/roadze-base/config/initializers/devise.rb:5:in `<main>'
/Users/taurenltd/Desktop/Development/roadze-base/config/environment.rb:5:in `<main>'
/Users/taurenltd/Desktop/Development/roadze-base/bin/rails:9:in `<top (required)>'
/Users/taurenltd/Desktop/Development/roadze-base/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Got this working by using the following block
def self.matches?(request)
if request.subdomain.present? && request.subdomain == 'app'
config.case_insensitive_keys = [:username]
elsif request.subdomain.present && request.subdomain == 'support'
config.case_insensitive_keys = [:email]
end
end

undefined method `each' for false:FalseClass

When I do rake db:migrate I get this error:
rake db:migrate
rake aborted!
NoMethodError: undefined method `each' for false:FalseClass
/home/chirag/social-login-in-rails/config/application.rb:25:in `<class:Application>'
/home/chirag/social-login-in-rails/config/application.rb:10:in `<module:SociaLoginRails>'
/home/chirag/social-login-in-rails/config/application.rb:9:in `<top (required)>'
/home/chirag/social-login-in-rails/Rakefile:4:in `require'
/home/chirag/social-login-in-rails/Rakefile:4:in `<top (required)>'
/home/chirag/.rvm/gems/ruby-2.0.0-p0/bin/ruby_executable_hooks:15:in `eval'
/home/chirag/.rvm/gems/ruby-2.0.0-p0/bin/ruby_executable_hooks:15:in `<main>'
(See full trace by running task with --trace)
I have cloned this app :- https://github.com/mohitjain/social-login-in-rails
Rename the file 'social_keys_example.yml' to 'social_keys.yml' and put your app keys then restart the sever, it will work
in config/application.rb
change
social_keys = File.join(Rails.root, 'config', 'social_keys.yml')
to
social_keys = File.join(Rails.root, 'config', 'social_keys_example.yml')
follow the instructions
http://codebeerstartups.com/2013/10/social-login-integration-with-all-the-popular-social-networks-in-ruby-on-rails/
In the config directory there is a file ie social_keys.yml. You need
to specify all the social keys there. Make sure have restarted the
application after making any change in that file.
Here are the urls from where you can create an app and get the keys

rails script using whenever gem error: __rvm_add_to_path: command not found

I'm using capistrano and the whenever gem.
Capistrano deploys and updates the crontab on our ubuntu server with the cronjob detailed out in our schedule.rb file.
But the cronjob doesn't happen. It fails and emails me every 5 minutes this error:
/etc/profile.d/rvm.sh: line 67: __rvm_add_to_path: command not found
/home/pkatepalli/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- bundler/setup (LoadError)
from /home/pkatepalli/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /[REMOVED]/releases/20140204194143/config/boot.rb:6:in `<top (required)>'
from /home/pkatepalli/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/pkatepalli/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from script/rails:4:in `<main>'
The weird part is we're deploying with a different username than pkatepalli. pkatepalli is my username on the server. That explains why it's emailing me the error - I think.
Also, we're using ruby 1.9.3 not 1.9.1. I'm not sure why it's not using the right ruby version.
On server:
Bundler version 1.3.5
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-linux]
$ rbenv version
1.9.3-p448 (set by /[REMOVED]/current/.ruby-version)
But interestingly enough in the current directory there's ruby 1.9.1, which I'm confused by:
/current/vendor/bundle/ruby/1.9.1
gemfile:
gem 'whenever', require: false
schedule.rb:
I've tried uncommenting: env 'PATH', ENV['PATH'], but the PATH that gets put into the schedule.rb file doesn't help.
Locally we're using RVM instead of rbenv (which is being used on the server). I'm not sure if that screws up the path if we set: env 'PATH', ENV['PATH']
#env 'PATH', ENV['PATH']
#set :output, "/log/cron.log"
#set :stage, :environment_variable
every 5.minutes, :roles => [:app] do
runner "Model.method"
end
deploy.rb:
require 'capistrano/log_with_awesome'
require "bundler/capistrano"
set :application, "[REMOVED]"
set :scm, :git
set :repository, "[REMOVED]"
set :branch, "master"
set :deploy_via, :remote_cache
set :user, "[REMOVED]"
set :password, "[REMOVED]"
set :deploy_to, "[REMOVED]"
set :keep_releases, 5
task :qa do
set :domain, "[REMOVED]"
role :web, "[REMOVED]", {:port => [REMOVED]} # Your HTTP server, Nginx
role :app, "[REMOVED]", {:port => [REMOVED]} # This may be the same as your `Web` server
set :env, "test"
end
task :production do
set :domain, "[REMOVED]"
role :web, "[REMOVED]", {:port => [REMOVED]} # Your HTTP server, Nginx
role :app, "[REMOVED]", {:port => [REMOVED]} # This may be the same as your `Web` server
set :env, "production"
end
set :use_sudo, false
default_run_options[:pty] = true
role :db, "[REMOVED]", {:port => [REMOVED], primary: true, :no_release => true}
after "deploy:setup", "deploy:chown"
namespace :bundle do
task :install, {:roles => :app} do
run "cd #{release_path} && bundle install --deployment --without development test"
end
end
before "deploy:assets:precompile" do
transfer :up, "config/application.yml", "#{shared_path}/application.yml", :via => :scp
run "ln -nfs #{shared_path}/application.yml #{release_path}/config/application.yml"
end
namespace :whenever do
task :start, :roles => :app do
run "cd #{release_path} && bundle exec whenever --update-crontab"
end
end
namespace :deploy do
task :execute_migrations, :roles => :app do
puts "RUNNING DB MIGRATIONS"
run "cd #{current_path}; bundle exec rake RAILS_ENV=#{env} db:migrate"
end
task :chown do
run "#{try_sudo} chown -R #{user} #{deploy_to}"
end
task :restart_nginx, {:roles => :web} do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
before "deploy:restart_nginx", "deploy:execute_migrations"
after :deploy, "deploy:restart_nginx"
after "deploy:restart_nginx", "deploy:cleanup"
after "deploy:update", "whenever:start"
end
When I ssh into the server and run crontab -l in the app's current directory:
# Begin Whenever generated tasks for: /[REMOVED]/releases/20140204194143/config/schedule.rb
#0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /[REMOVED]/releases/20140204194143 && script/rails runner -e production '\''Model.method'\'''
# End Whenever generated tasks for: [REMOVED]/releases/20140204194143/config/schedule.rb
When I run the cron job on the server like this I get this result:
:/[REMOVED]/current$ rails runner -e production '\''Model.method'\'''
Rails Error: Unable to access log file. Please ensure that /[REMOVED]/releases/20140204194143/log/production.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
/[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:53:in `eval': (eval):1: syntax error, unexpected $undefined (SyntaxError)
\Model.method'
^
(eval):1: unterminated string meets end of file
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:53:in `<top (required)>'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `require'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `<top (required)>'
from script/rails:5:in `require'
from script/rails:5:in `<main>'
When I do it like this. I get this:
[REMOVED]/current$ rails runner Model.method
Rails Error: Unable to access log file. Please ensure that [REMOVED]/releases/20140204194143/log/development.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
[REMOVED]/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `initialize': could not connect to server: Connection refused (PG::ConnectionBad)
Is the server running on host "localhost" ([REMOVED]) and accepting
TCP/IP connections on port 5432?
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `new'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `connect'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:326:in `initialize'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `new_connection'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `checkout_new_connection'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:247:in `block (2 levels) in checkout'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `loop'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `block in checkout'
from /usr/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:239:in `checkout'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:102:in `block in connection'
from /usr/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:101:in `connection'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/model_schema.rb:308:in `clear_cache!'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/railtie.rb:97:in `block (2 levels) in <class:Railtie>'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:418:in `_run__249672195884464632__prepare__2220886522034318467__callbacks'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `__run_callback'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/application/finisher.rb:47:in `block in <module:Finisher>'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:30:in `instance_exec'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:30:in `run'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:55:in `block in run_initializers'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:54:in `each'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:54:in `run_initializers'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/application.rb:136:in `initialize!'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /[REMOVED]/releases/20140204194143/config/environment.rb:5:in `<top (required)>'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `require'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `block in require'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:236:in `load_dependency'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `require'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/application.rb:103:in `require_environment!'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:44:in `<top (required)>'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `require'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `<top (required)>'
from script/rails:5:in `require'
from script/rails:5:in `<main>'
In response to Leonid Shevtsov's answer:
I ran:
rails runner -e production 'Model.method'
and I got this:
[REMOVED]/current$ rails runner -e production 'Model.methodName'
/[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:53:in `eval': undefined method `methodName' for #<Class:0x0000000570d668> (NoMethodError)
from (eval):1:in `<top (required)>'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:53:in `eval'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:53:in `<top (required)>'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `require'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `<top (required)>'
from script/rails:5:in `require'
from script/rails:5:in `<main>'
when I run crontab -l, I get the whenever generated crons
a) 1.9.1 is the "base" version of Ruby 1.9.3 (1.9.3 is considered to be a version of Ruby 1.9.1); it's normal that you see it in the paths.
b) the command line in cron is escaping quotes; the proper way to check it manually is rails runner -e production 'Model.method'
c) are you sure the error isn't arriving from a locally installed cron job? (check with crontab -l)
Ok this is kind of complicated and some debugging info is missing so I will try to take a guess.
First of all it seems that you have both rvm and rbenv installed on your system which is not a very good idea. Try uninstalling one of the two, or better yet uninstall both and reinstall the one you prefer.
In your deploy script it seems you have rolled your own whenever capistrano tasks. This is not bad, but whenever already provides this out of the box with better support (e.g. rollbacks). You can also get better feedback on your future problems.
Those are potential pitfalls but their are not probably the cause of your problems. You seem to have messed things up regarding the linux users and their permissions. As far as I understand there are two users on your production system: pkatepalli and another one which you commented out, lets call him deployer.
From what I see you are trying to set everything up to work from the perspective of deployer which is a good practice.
I assume the capistrano user variable is set to deployer. If not this is the root of your problem. This is the user which capistrano uses to log in and issue your commands.
Also it seems that when you run crontab -l you see the cron jobs correctly but if you are logged in with pkatepalli when you run the command then this is your problem. You have written your tasks to the wrong user's crontab. If this has happened it more likely means either that you run the whenever command from the wrong user during cap deploy (in which case you need to check which is the logged in user during cap) or you have actually run the whenever command from both users and you are only getting errors for pkatepalli (clear your crontab before experimenting again). If it still looks it is ok then you can try passing the -u username option to whenever command (you probably need sudo for this).
To set up the deployer user properly you can follow the instructions by capistrano

including module works in development, fails at startup in production?

I've authored a module that lives in my lib folder. I'm including it at startup by putting it in my environment.rb file as follows
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
MyProject::Application.initialize!
ActiveRecord::Base.send :include, MyProject::Has::Formatter
include MyProject::EventFormatters
This works great in development but if I try to run in production mode or if I deploy my app to production I get the following stack trace when the server tries to start up:
=> Booting WEBrick
=> Rails 3.0.3 application starting in production on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:1008:in `method_missing': undefined local variable or method `has_formatter' for #<Class:0x808f830> (NameError)
from C:/Data/myproject/app/models/event.rb:2
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:227:in `load_dependency'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:346:in `require_or_load'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:300:in `depend_on'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:216:in `require_dependency'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:138:in `eager_load!'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:137:in `each'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:137:in `eager_load!'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:135:in `each'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:135:in `eager_load!'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:108:in `eager_load!'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application/finisher.rb:41
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `instance_exec'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `run'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:50:in `run_initializers'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `each'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `run_initializers'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:134:in `initialize!'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:77:in `send'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
from C:/Data/myproject/config/environment.rb:5
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
from C:/Data/myproject/config.ru:3
from C:/Ruby187/lib/ruby/gems/1.8/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
from C:/Ruby187/lib/ruby/gems/1.8/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
from C:/Data/myproject/config.ru:1:in `new'
from C:/Data/myproject/config.ru:1
Now here is the most mind boggling part of the whole thing: If I set config.cache_classes = false in config/environments/production.rb then I don't get that stack trace and the app behaves the same in production mode as it does dev mode!!
What about settings config.cache_classes = false makes including a module in this fashion work in production? Am I including the module in a wrong/weird way?
Hope I'm totally, right, I'll at least present my point of view:
In development, all your classes are lazy loaded. This way, you can easily change them since they're loaded only when necessary. This means that:
MyProject::Application.initialize!
does not load module content which requires:
ActiveRecord::Base.send :include, MyProject::Has::Formatter
include MyProject::EventFormatters
which is included afterwards, it makes the whole thing working.
In production, modules and classes are really loaded => crash, they need their dependencies!
As a result, put:
ActiveRecord::Base.send :include, MyProject::Has::Formatter
include MyProject::EventFormatters
in an initializer you create in config/initializers, call it active_record_extension.rb for instance.
Does autoloading not work for your scenario?
# config/application.rb
config.autoload_paths += %W(#{config.root}/lib)
# lib/my_project/event_formatters.rb
module MyProject
module EventFormatters
# .. etc. ...
end
end

Resources