Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect Rails environment inside whenever

Tags:

This question will probably only make sense if you know about the whenever gem for creating cron jobs.

For my app, I want to use whenever in all the environments, including testing and development.
My schedule.rb looks like this:

set :output, {     :error    => "#{path}/log/error.log",     :standard => "#{path}/log/cron.log" }  set :environment, Rails.env.to_sym every 5.minutes do   rake 'db:activity:synchronize' end 

but it fails on Rails.env.to_sym (and the same stands for RAILS_ENV):

/home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever/job_list.rb:21:in `eval': uninitialized constant Whenever::JobList::Rails (NameError)     from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever/job_list.rb:21:in `eval'     from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever/job_list.rb:21:in `initialize'     from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever.rb:15:in `new'     from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever.rb:15:in `cron'     from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever/command_line.rb:41:in `run'     from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever/command_line.rb:8:in `execute'     from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/bin/whenever:38:in `<top (required)>'     from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/bin/whenever:19:in `load'     from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/bin/whenever:19:in `<main>' 

So, my question basically boils down to:

  1. How do I access the current environment, or
  2. What should I do to use whenever in all the environments?
like image 241
Marius Butuc Avatar asked Aug 16 '11 20:08

Marius Butuc


People also ask

How do I check my current Rails environment?

The Rails environment is determined by whatever the value of the RAILS_ENV environment variable is when the server starts. You might have some configuration file somewhere that specifies it, or maybe you just start your server with a command of the form RAILS_ENV=production my_rails_server ?

What environments Does Rails have by default?

When we generate a new Rails application we get three environments by default: development , test and production . There's nothing special about these particular environments, though, and there are very few places in the Rails source code that refer to them.

Where are Rails environment variables stored?

Keeping Environment Variables PrivateRemote git repositories such as GitHub are a place to store and share code. If your project is open source, any developer will have access to your code. You don't want to share email account credentials or private API keys with the public.


2 Answers

At least in newer version of whenever it is possible to access the environment with @environment. For example if you want whenever to only generate cron entries for some jobs in production:

case @environment when 'production'   every 1.day, :at => '0:00 am' do     rake "some:task"   end  end 
like image 81
Mattias Wadman Avatar answered Oct 23 '22 13:10

Mattias Wadman


The error message suggests that Rails isn't defined. i.e the framework isn't loaded when you're asking the question what environment is rails running with.

In fact from looking at the code for Whenever it looks like rails isn't a requirement for it (i.e. You can install and run Whenever without rails even being installed on your system). Hence there's no way for Whenever to look at your rails environment (as far as i can tell)

like image 41
Chris Bailey Avatar answered Oct 23 '22 13:10

Chris Bailey