gpt4 book ai didi

ruby-on-rails - 每当 gem schedule.rb 文件 : doesn't recognize RAILS_ROOT variable

转载 作者:行者123 更新时间:2023-12-03 17:53:09 26 4
gpt4 key购买 nike

在 schedule.rb 文件中,声明:

require "#{RAILS_ROOT}/config/environment.rb"

every "10 10 2 * * *" do
command "mysqldump -u #{@db_username} -p#{@db_password} --single-transaction #{@db_name} > #{@backup_Path}/#{@db_name}.sql 2> log/error_crontab.log"
end

当我尝试从终端执行每当 cmd 时,出现以下错误:
 config/schedule.rb:48:in `initialize': uninitialized constant Whenever::JobList::RAILS_ROOT (NameError)
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/job_list.rb:19:in `instance_eval'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/job_list.rb:19:in `initialize'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever.rb:16:in `new'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever.rb:16:in `cron'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/command_line.rb:40:in `run'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/command_line.rb:7:in `execute'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/bin/whenever:38:in `<top (required)>'
from /usr/local/bin/whenever:19:in `load'
from /usr/local/bin/whenever:19:in `<main>'

我正在使用 require 语句从表单中获取动态值以安排作业。请帮忙解决这个问题?

注意:我看到了以下 stackoverflow 查询:
How to detect Rails environment inside whenever

按照此线程获取动态值,但遇到 require 语句的问题。
Rails - Whenever gem - Dynamic values

Ruby/Rails - Whenever gem - Loop cron tasks

config file in schedule.rb with Rails Whenever gem?

最佳答案

每当根本不需要或不依赖于 Rails,所以当它运行时,RAILS_ROOT 没有定义,但是因为每当的 schedule.rb 通常保存在/config/schedule.rb 中,我们可以假设它在 Rails 中项目,并像这样设置我们自己的 RAILS_ROOT:

# in schedule.rb
RAILS_ROOT = File.dirname(__FILE__) + '/..'

编辑:如果您确实需要加载 Rails,请执行以下操作:
# in schedule.rb
# this will require config/environment and load your entire rails environment
require File.expand_path(File.dirname(__FILE__) + "/environment")

关于ruby-on-rails - 每当 gem schedule.rb 文件 : doesn't recognize RAILS_ROOT variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7978177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com