在Rails加载完成后运行代码? [英] Running code after Rails is done loading?

查看:48
本文介绍了在Rails加载完成后运行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定期任务,需要每分钟执行一次(使用delay_job)。我希望Rails可以在完成加载后立即自动将其排队,如果系统中还没有这样的任务。



哪里是个好地方让我在整个Rails引导流程结束时运行一些代码?有人建议config / environments / development.rb(或其他环境),但是当我从那里排队作业时,delay_job给了我ActiveRecord问题。



我咨询了 http://guides.rubyonrails.org/initialization.html ,而且似乎没有明确的位置



这种部署后设置是否可能是在我的应用程序代码的外部完成的,也许是通过rake或其他方式完成的?有什么建议吗?



谢谢!

解决方案

关于 http://guides.rubyonrails.org/initialization.html ,抱歉,我们正在努力重写它。对于您的问题,我会尝试在您的应用程序中使用 config.after_initialize 。rb

  def after_initialize(& block)
ActiveSupport.on_load(:after_initialize,:yield => true,& block)
end

I have a periodic task that needs to execute once a minute (using delayed_job). I would like for Rails to automatically queue it up as soon as it's finished loading, if one such task isn't already present in the system.

What is a good place for me to run some code right at the end of the entire Rails boot flow? Someone suggested config/environments/development.rb (or other environment), but delayed_job give me ActiveRecord issues when I queue up jobs from there.

I consulted http://guides.rubyonrails.org/initialization.html, and there doesn't seem to be a clear location for that kind of code either.

Is this kind of post-deployment setup to be done perhaps externally to my app's code, maybe through rake or some other means? Any suggestions?

Thank you!

解决方案

Regarding http://guides.rubyonrails.org/initialization.html, sorry, we're working hard to rewrite it. For your problem, I would try with config.after_initialize in your application.rb

def after_initialize(&block)
  ActiveSupport.on_load(:after_initialize, :yield => true, &block)
end

这篇关于在Rails加载完成后运行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆