如何使用Whenever gem强制rake任务在开发环境下运行

时间:2021-07-08 08:01:59

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.

我正在使用Whenever gem来运行rake任务。当我运行rake任务时,它在开发环境下运行,但是当它在预定时间运行时,它指的是生产环境。如何强制在开发环境下运行计划的rake任务。据我所知,我将不得不使用RAILS_ENV变量,但无法弄清楚它的位置。我想,它与Whenever gem无关。

2 个解决方案

#1


7  

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

在schedule.rb中,您可以指定要在其中运行计划任务的环境:

# 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 

#2


12  

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

在任何bash类型的shell中,您通常可以在运行时覆盖环境:

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.

这可以根据需要适用于其他外壳。

#1


7  

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

在schedule.rb中,您可以指定要在其中运行计划任务的环境:

# 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 

#2


12  

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

在任何bash类型的shell中,您通常可以在运行时覆盖环境:

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.

这可以根据需要适用于其他外壳。