Rails 中的 cron 作业如何工作?
我的模型:
class Uzer < ActiveRecord::Base
establish_connection :uzer
set_table_name 'managers'
def self.synchronize_emails
get_emails_from_uzer = Uzer.find(:all)
get_emails_from_flower = Respondent.find(:all)
get_emails_from_uzer.each do |e|
Respondent.create(:email => e.email, :user_id => 6, :respondent_group_id => e.organisational_id)
end
end
end
一切都很完美,但是...
我想每小时执行一次 def self.synchronize_emails 来从另一个数据库获取电子邮件并插入到我的数据库中。
我尝试使用这个 -> https://github.com/javan/whenever
我安装了 gem,创建了 config/schedule.rb 并写道:
every 2.minutes do
runner "Mossad.synchronize_emails"
end
(只是为了测试看看它是否有效)
但是,什么也没有发生。有什么想法/建议吗?我想要 cron!:(
谢谢!
更新:
我在控制台中写道:whenever --update-crontab store
# Begin Whenever generated tasks for: store
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /bin/bash -l -c 'cd /home/ee-vzaharov/Projects/AptanaRadRails/survey && script/runner -e production '\''Mossad.synchronize_emails'\'''
# End Whenever generated tasks for: store
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
Type :quit<Enter> to exit Vim
My model:
class Uzer < ActiveRecord::Base
establish_connection :uzer
set_table_name 'managers'
def self.synchronize_emails
get_emails_from_uzer = Uzer.find(:all)
get_emails_from_flower = Respondent.find(:all)
get_emails_from_uzer.each do |e|
Respondent.create(:email => e.email, :user_id => 6, :respondent_group_id => e.organisational_id)
end
end
end
Everything is perfect, BUT...
I want to execute def self.synchronize_emails
every hour to get emails from another database and insert into my database.
I tried to use this -> https://github.com/javan/whenever
I installed the gem, created config/schedule.rb and wrote:
every 2.minutes do
runner "Mossad.synchronize_emails"
end
(just for test to see it works or not)
However, nothing happens. Any ideas / suggestions? I WANT cron!:(
Thank you!
UPDATE:
I wrote in console: whenever --update-crontab store
# Begin Whenever generated tasks for: store
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /bin/bash -l -c 'cd /home/ee-vzaharov/Projects/AptanaRadRails/survey && script/runner -e production '\''Mossad.synchronize_emails'\'''
# End Whenever generated tasks for: store
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
Type :quit<Enter> to exit Vim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
cron
,您可以安排以下cd /path/to/web/app && /path/to/rake -s name_of_task
随时运行。我没有使用提到的 gem,cron
似乎很好。使用 crontab -e 编辑当前用户的 crontab。如果您需要的话,维基百科有一篇关于如何使用它的好文章。
With
cron
you can schedule the followingcd /path/to/web/app && /path/to/rake -s name_of_task
to be run whenever you want. I have not used the mentioned gem,cron
seems just fine.Use
crontab -e
to edit the crontab of a current user. Wikipedia has a nice article on how to use it, if you need.检查 crontab -e 是否在 crontab 中创建了条目。如果没有,请运行命令:
插件将更新 crontab(使用 crontab -e 再次检查)
Check with
crontab -e
if whenever created entries in your crontab. If not, run the commands:And the plugin will update the crontab (check it again with
crontab -e
)