【问题标题】:Rails rake task fails just in production: "NoMethodError: private method `open' called for URI:Module"Rails rake 任务在生产中失败:“NoMethodError:私有方法 `open' 调用 URI:Module”
【发布时间】:2020-05-20 17:44:43
【问题描述】:

我正在编写一个 rake 任务,该任务的功能是从其他网页获取信息。为此,我使用 open-uri 和 nokogiri。我已经在开发中进行了测试,它可以完成这项工作,但后来我部署到生产服务器并失败了。

这是代码:

require 'open-uri'
require 'nokogiri'

desc 'recover eventos llerena'
task recover_eventos_llerena: :environment  do

  enlaces = []

  # Getting index and all links
  url = open(URI.parse("https://llerena.org/eventos/lista"))
  page = Nokogiri::HTML(url)

  page.css("a.fusion-read-more").each do |line|
    enlaces.push(line.attr('href'))
  end

  enlaces = enlaces.uniq

  #Inspecting everyone of them
  enlaces.each do |link|
    url = open(URI.parse(link))
    page = Nokogiri::HTML(url)

    title = page.css("h1").text
    if Evento.find_by_titulo(title) == nil

      description = page.css(".tribe-events-single-event-description.tribe-events-content.entry-content.description p").text
      date = page.css(".tribe-events-abbr").attr('title')
      image = page.css(".size-full").attr('src')

      Evento.create!(
        titulo: title,
        remote_imgevento_url: image,
        info: description,
        fecha: Date.parse(date)
      )

    end
  end

end

当检查 cron_error_log 我得到:

rake aborted!
NoMethodError: private method `open' called for URI:Module
/home/deploy/guialocal/releases/20200204193434/lib/tasks/example.rake:23:in `block in <top (required)>'
/home/deploy/guialocal/shared/bundle/ruby/2.4.0/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/cli/exec.rb:74:in `load'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/cli/exec.rb:74:in `kernel_load'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/cli/exec.rb:28:in `run'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/cli.rb:463:in `exec'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/cli.rb:27:in `dispatch'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/cli.rb:18:in `start'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/exe/bundle:30:in `block in <top (required)>'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/lib/bundler/friendly_errors.rb:124:in `with_friendly_errors'
/home/deploy/.rvm/gems/ruby-2.4.4/gems/bundler-2.0.1/exe/bundle:22:in `<top (required)>'
/home/deploy/.rvm/gems/ruby-2.4.4/bin/bundle:23:in `load'
/home/deploy/.rvm/gems/ruby-2.4.4/bin/bundle:23:in `<main>'
Tasks: TOP => recover_eventos_llerena
(See full trace by running task with --trace)

最后我尝试在生产环境中运行控制台:

2.4.0 :001 > Rake::Task['recover_eventos_llerena'].execute
NameError: uninitialized constant Rake::Task
    from (irb):1

在 Rails 2.7.0 中,有一个未来弃用的警告,我也改变了提议的新方法(没有结果):

url = URI.open("https://llerena.org/eventos/lista")

为什么会这样?

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-5 rake rake-task open-uri


【解决方案1】:

你应该尝试简单地添加

require 'open-uri'

在脚本的开头

【讨论】:

    【解决方案2】:

    不是在 Rails 上,而是类似的问题:我在运行 ruby​​ 2.7.0 的机器上编写并运行了一个脚本,当我尝试在具有 ruby​​ 2.3.0 的机器上运行相同的脚本时,我遇到了和你一样的错误。

    在检查文件open-uri.rb(在我的情况下是/usr/lib/ruby/2.7.0/)后,我发现旧版ruby 的文件在URI 模块中没有公共方法。

    相反,您需要使用 Kernel#Open。所以你的脚本会变成:

    url = open("https://llerena.org/eventos/lista")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-13
      • 2013-02-18
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 2011-04-26
      • 2016-11-13
      • 1970-01-01
      相关资源
      最近更新 更多