[英]Where should I put my cronjobs in rails?
Is there a special place for cronjobs in rails? 在rails中有cronjobs的特殊地方吗? I couldn't find information on this subject, the only examples out there are to execute Model.some_method
我找不到有关此主题的信息,仅有的示例是执行Model.some_method
I created a class Crawler, with a method run, and tried different places for it (including app/models), but I keep getting this error: 我创建了一个类别为Crawler的类,并运行了一个方法,并尝试了它的不同位置(包括应用程序/模型),但始终出现此错误:
$ rails runner -e development 'Crawler.run'
$ railsRunner -e开发“ Crawler.run”
/usr/local/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/commands/runner.rb:53: undefined method `run' for Crawler:Class (NoMethodError)
/usr/local/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/commands/runner.rb:53:Crawler:Class的未定义方法“ run”(NoMethodError)
from /usr/local/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/commands.rb:64:in `eval' from /usr/local/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/commands/runner.rb:53 from /usr/local/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/commands.rb:64:in `require' from /usr/local/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/commands.rb:64 from script/rails:6:in `require' from script/rails:6
class Crawler
def run
puts 'bla'
end
end
As you can see, it can't find the method 'run'. 如您所见,它找不到方法“运行”。
If I put it inside /scripts I get this: uninitialized constant Crawler (NameError), so this is probably not what I want. 如果将其放在/ scripts中,则会得到:未初始化的常量Crawler(NameError),所以这可能不是我想要的。
Any ideas? 有任何想法吗?
note. 注意。 I'm on rails 3
我在轨道上3
You're calling an instance method on class level. 您正在类级别调用实例方法。
Rewrite like this: 像这样重写:
class Crawler
def self.run
puts 'bla'
end
end
To answer your initial question, there is no real dedicated place but I usually create a crons
folder under app
. 要回答您最初的问题,没有真正的专用场所,但是我通常在
app
下创建一个crons
文件夹。
Regarding rake tasks, they are supposed to live in /lib
but it's really up to you. 关于rake任务,它们应该存在于
/lib
但这实际上取决于您。
I encourage you to use the whenever gem . 我鼓励您使用everything gem 。 It allows you to set up tasks in a
schedule.rb
file as follows: 它使您可以按以下方式在
schedule.rb
文件中设置任务:
every 3.hours do
runner "MyModel.some_process"
rake "my:rake:task"
command "/usr/bin/my_great_command"
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.