how to force rake task to run under development environment using Whenever gem
Asked Answered
C

2

6

I'm using Whenever gem to run a rake task. When I run rake task it runs under development environment, but when it runs on a scheduled time it refers to a production environment. How can I force to run a scheduled rake task under development environment. As I understand I'll have to use RAILS_ENV variable, but can't figure out where to put it. I think, it has nothing to do with Whenever gem here.

Create answered 28/6, 2013 at 5:2 Comment(0)
D
8

In schedule.rb, you can specify the environment you'd like scheduled tasks to be run in:

# config/schedule.rb
set :environment, 'development'

Alternatively, you can set the environment on a per-job basis:

# config/schedule.rb
every 1.day do 
  runner 'Model.task', :environment => 'development'
  runner 'Model.task', :environment => 'production' 
end 
Davis answered 28/6, 2013 at 5:5 Comment(2)
Can you run the tasks that are in the scheduler without waiting for the time period to pass?Gunlock
Are you asking if it's possible to run tasks manually (i.e., outside the scheduler)? If so, yes. However, if you're trying to utilize the Whenever gem, you'll necessarily need to set some sort of time interval – AFAIK, the minimum interval is one second. Otherwise, you'd be continually and simultaneously running your tasks... which isn't actually possible/feasible.Davis
P
12

In any bash-type shell you can usually override the environment when you run it:

RAILS_ENV=development rake task:name...

You can also write a small script to do this for you:

#!/bin/sh

export RAILS_ENV=development

rake task:name...

This can be adapted for other shells as required.

Planogamete answered 28/6, 2013 at 5:5 Comment(0)
D
8

In schedule.rb, you can specify the environment you'd like scheduled tasks to be run in:

# config/schedule.rb
set :environment, 'development'

Alternatively, you can set the environment on a per-job basis:

# config/schedule.rb
every 1.day do 
  runner 'Model.task', :environment => 'development'
  runner 'Model.task', :environment => 'production' 
end 
Davis answered 28/6, 2013 at 5:5 Comment(2)
Can you run the tasks that are in the scheduler without waiting for the time period to pass?Gunlock
Are you asking if it's possible to run tasks manually (i.e., outside the scheduler)? If so, yes. However, if you're trying to utilize the Whenever gem, you'll necessarily need to set some sort of time interval – AFAIK, the minimum interval is one second. Otherwise, you'd be continually and simultaneously running your tasks... which isn't actually possible/feasible.Davis

© 2022 - 2024 — McMap. All rights reserved.