简体   繁体   中英

Dashing (Ruby) Nokogiri LoadError

I've been working on a dashboard on the Dashing framework, and I'm currently trying to make a little crawler to collect specific data on Jenkins-CI, and pass it to the Number widget. Here's the crawler (it's just a stub, it counts the number of "p" elements on a stub html page):

require 'nokogiri'
require 'open-uri'

class ActiveBuilds

      def initialize()
          @jenkins_page = nil
          @build_count = nil
      end

      # !STUB! Gets the jenkins page to parse to XML on Nokogiri
      @jenkins_page = Nokogiri::HTML(open("http://localhost:80"))

      # !STUB! Counts the number of 'p' items found on the page
      @build_count = @jenkins_page.css("p").length

      # !STUB! Returns the amount of active builds
      def amountOfActiveBuilds
          return @build_count
      end
end

and for reference, not really necessary, is the HTML page:

 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Number Stub | Project</title> </head> <body> <h1>Test</h1> <ul> <!-- Count these --> <li> <div> <p>Item 1 </div> <li> <div> <p>Item 2 </div> <li> <div> <p>Item 3 </div> <li> <div> <p>Item 4 </div> <li> <div> <p>Item 5 </div> <!-- Stop counting --> <li> <div> Item 6 </div> <li> <div> Item 7 </div> </ul> </body> </html> 

and now, the jobs/sample.rb file from dashing, modified (the only thing that matters is the builds/valuation stuff):

require './ActiveBuilds.rb'

active_builds = ActiveBuilds.new
current_valuation = active_builds.amountOfActiveBuilds
current_karma = 0

SCHEDULER.every '2s' do
  last_valuation = current_valuation
  last_karma     = current_karma
  current_karma  = rand(200000)

  send_event('valuation', { current: current_valuation, last: last_valuation })
  send_event('karma', { current: current_karma, last: last_karma })
  send_event('synergy', { value: rand(100) })

end

The thing is, before I had it working, it would get the page on localhost, count the number of "p" items and print it on a file, and then the dashing file would read it and display it correctly, but it wasn't updating the value on the dashboard unless I'd restart it, which defeats the purpose of this framework.

now to the errors:

When attempting to compile sample.rb (the dashing file):

$ ruby sample.rb
sample.rb:12:in '<main>': uninitialized constant SCHEDULER (NameError)

When attempting to run the dashing server:

$ dashing start
/home/yadayada/.rvm/gems/ruby-2.2.0/gems/backports-3.6.4/lib/backports/std_lib.rb:9:in 'require': cannot load such file -- nokogiri (LoadError)
from /home/yadayada/.rvm/gems/ruby-2.2.0/gems/backports-3.6.4/lib/backports/std_lib.rb:9:in 'require_with_backports'
from /home/yadayada/Desktop/dashing/project/jobs/ActiveBuilds.rb:2:in '<top (required)>'
(...)

I could also post the HTML/CSS/CoffeScript components of the Number widget, but I believe the problem lies on the sample.rb, and the Number widget is completely default.

In case the code wasn't clear enough, what I'm trying to do is to get the localhost page, count the number of "p" items (later it'll be the active builds when I switch to jenkins, didn't switch yet because i'm dealing with the certificates), then send it over to sample.rb, which will get the data and update it every 2 seconds on the dashboard display.

Any suggestions are welcome! Thanks in advance!

Found the solution:

uninstall/reinstall nokogiri gem (without sudo) put my crawler into the lib folder and require it inside the jobs on the job itself, placed everything into the SCHEDULER function, like this:

 # This job provides the data of the amount of active builds on Jenkins using the Number widget # Updates every 2 seconds SCHEDULER.every '2s' do # Invokes the crawlers from the lib folder Dir[File.dirname(__FILE__) + '/lib/*rb'].each { |file| require file } # Create the ActiveBuilds reference builds = ActiveBuilds.new # Attributes the amount of active builds to the current valuation current_valuation = builds.get_amount_of_active_builds # Pass the current valuation to the last to present the change percentage on the dashboard last_valuation = current_valuation # Sends the values to the Number widget (widget id is valuation) send_event('valuation', { current: current_valuation, last: last_valuation }) end 

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