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

2

15

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?

Ifc answered 4/2, 2020 at 20:33 Comment(4)
Did you try the recommended method?Bastogne
What platform is your production on?Swarts
Yes, I tried. nothing changed. I am using Ubuntu server 18.04Ifc
How to run Rake tasks from consoleBastogne
H
24

You should try to simply add

require 'open-uri'

at the start of your script

Heisenberg answered 19/8, 2020 at 8:36 Comment(0)
M
5

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")
Miscible answered 31/3, 2020 at 4:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.