4

In my Rails app I use whenever gem to run a Sideqik task. When I run that command manually in the rails console it works. But the Whenever cron fails with the following error : It keeps repeating in the log. How can I fix this? What is the problem?

/home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol_connection': 'production' database is not configured. Available: ["development", "test"] (ActiveRecord::AdapterNotSpecified)
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:211:in `resolve_connection'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:139:in `resolve'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:169:in `spec'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:50:in `establish_connection'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:27:in `each'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/railtie.rb:116:in `block in <class:Railtie>'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `instance_exec'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `run'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `each'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `call'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/initializable.rb:54:in `run_initializers'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/application.rb:352:in `initialize!'
    from /home/pubudu/Projects/istockseller/config/environment.rb:5:in `<top (required)>'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application.rb:92:in `preload'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application.rb:143:in `serve'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application.rb:131:in `block in run'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application.rb:125:in `loop'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application.rb:125:in `run'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application/boot.rb:18:in `<top (required)>'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'
THpubs
  • 7,804
  • 16
  • 68
  • 143

1 Answers1

6

You're encountering this error because your production settings aren't configured correctly to connect to your database. When you run the command from the Rails console, you're connecting to the development environment, but your cron job is being run in the production environment.


Rails has 3 built-in environments. The main difference is that each has their own database, but there are other differences too.

  • Production, for when your code is running live. This environment does a lot of caching, and doesn't show development error messages to the user.
  • Development, for building your application. This is the default environment, and is the environment that you spend most of your time in.
  • Test, for running automated tests. The main reason this is separate is so your test suite can empty & recreate the database on each test run. This makes your tests more reproducible, and stops an accidental test run from destroying your development DB.

Mostly, these are configured via files in config/environments. The database connections are configured in config/database.yml - you'll find YAML keys for development, production, and test in there. The Rails Guide has more details about configuring applications.

You can find out which environment you're running in via Rails.env. For instance:

user@foo $ rails c
Loading development environment (Rails 4.1.1)
irb(main):001:0> Rails.env
"development"

And you can force a command to run in a given environment via a RAILS_ENV shell environment variable:

user@foo $ RAILS_ENV=production rails c
Loading production environment (Rails 4.1.1)
irb(main):001:0> Rails.env
"production"

To fix your problem, you have two choices:

  1. Force your cron command to run in the development environment. It sounds like your app is running in the development environment (because it works from the console, and you have no production database configured). So forcing the cron job to run in the development environment should fix this. You could set it in your cronjob directly by setting the RAILS_ENV variable as we saw above, but whenever lets you specify the environment on the commandline or within your schedule.rb:

    set :environment, "development"
    
  2. Fix your app to run in production. If your application is live, it really should be running in the production environment. It will be faster and more secure that way - as well as letting you mess things up when developing new features without the risk of destroying something used by your users. You'll have to set up the database, and will have to test your app in production. The best way to do this varies by platform; this Stack Overflow question has a good summary of the various options.

Alex P
  • 5,942
  • 2
  • 23
  • 30
  • Thank you very much for the explanation! Now I understand the problem. Without changing the whole app to production and without forcing the cron to run in development, is there a way to detect the current environment and force the cron to run in the correct environment? – THpubs Dec 26 '14 at 14:30
  • 1
    Found the solution! Simply run whenever with development environment to set the cron `whenever --update-crontab --set environment=development` – THpubs Dec 26 '14 at 14:33
  • [`whenever` runs in `production` by default](https://github.com/javan/whenever). If you want it to run in a different environment, you'll have to configure it like in option 1. – Alex P Dec 26 '14 at 14:33