简体   繁体   中英

Rails rake task fails just in production: "NoMethodError: private method `open' called for URI:Module"

I am coding a rake task which function is getting info from other webpage. To do that I use open-uri and nokogiri. I have tested in development and it does the job, but then I deploy to the production server and fails.

This is the code:

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

When checking cron_error_log I get:

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)

And finally I have tried to run console in production getting:

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

In rails 2.7.0 there is a warning for future deprecation and I have change also the proposed new way (without results):

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

Why is this happening?

You should try to simply add

require 'open-uri'

at the start of your script

Not on rails, but similar issue: I wrote and ran a script on a machine running ruby 2.7.0, when trying to run the same script on a machine with ruby 2.3.0 I got the same error as you.

After examining the file open-uri.rb (in my case in /usr/lib/ruby/2.7.0/ ) I found out that the older ruby version's file has no public method in the URI module.

Instead, you need to use Kernel#Open. So your script would become:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM