每当 gem 时,schedule.rb 中的配置文件与 Rails 一起使用?

发布于 2024-08-26 21:17:00 字数 550 浏览 10 评论 0原文

我的 Rails 应用程序的 /config 文件夹中有一个名为 config.yml 的文件。 我还有一个初始化程序: config/initializers/load_config.rb ,其中包含以下代码:

APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")

我正在使用每当 gem 来设置 cron 作业,并且想使用我的 APP_CONFIG 来调用这样的函数:

#inside schedule.rb
every 2.hours do
  runner "MyModel.someMethod('#{APP_CONFIG['some_value']}')"
end

但是每当 gem 没有当我调用如何将配置中的值合并到我的 Schedule.rb 文件中时,似乎无法识别配置文件

 whenever --update-crontab mysite

(而不是对值进行硬编码)?

谢谢!

I have a file called config.yml in my /config folder of my rails application.
I also have an initializer: config/initializers/load_config.rb with the following code:

APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")

I am using the Whenever gem to set up a cron job, and would like to use my APP_CONFIG to call a function like so:

#inside schedule.rb
every 2.hours do
  runner "MyModel.someMethod('#{APP_CONFIG['some_value']}')"
end

but the Whenever gem doesn't seem to recognize the config file when I call

 whenever --update-crontab mysite

How can I incorporate values from my configuration in my schedule.rb file (instead of hard-coding the value)?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半透明的墙 2024-09-02 21:17:00

编辑 schedule.rb 文件以在顶部添加 require 'yaml' 语句。然后从初始化程序中添加以下行:

APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")

或者,您可能只需要直接加载 load_config.rb 文件。那么你应该可以走了。

Edit your schedule.rb file to add a require 'yaml' statement to the top. Then add the line from your initializer:

APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")

Altenatively, you can probably just require the load_config.rb file directly. You should be good to go then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文