潇洒 (Ruby) Nokogiri LoadError

Dashing (Ruby) Nokogiri LoadError

我一直在 Dashing 框架上开发仪表板,目前我正在尝试制作一个小爬虫来收集 Jenkins 上的特定数据-CI,并将其传递给 Number 小部件。这是爬虫(它只是一个存根,它计算存根 html 页面上 "p" 元素的数量):

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

HTML 页面:

仅供参考,并非真正必要

<!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>

现在,来自 dashing 的 jobs/sample.rb 文件已修改(唯一重要的是 builds/valuation 内容):

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

事情是,在我让它工作之前,它会在本地主机上获取页面,计算 "p" 项目的数量并将其打印在文件上,然后 dashing 文件会读取它并显示它正确,但它不会更新仪表板上的值,除非我重新启动它,这违背了这个框架的目的。

现在解决错误:

尝试编译 sample.rb(虚线文件)时:

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

尝试 运行 破折号服务器时:

$ 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)>'
(...)

我也可以 post 数字小部件的 HTML/CSS/CoffeScript 组件,但我认为问题在于 sample.rb,并且数字小部件是完全默认的。

如果代码不够清晰,我想做的是获取本地主机页面,计算 "p" 项目的数量(稍后它将成为活动构建,当我切换到 jenkins,还没有切换,因为我正在处理证书),然后将其发送到 sample.rb,它将获取数据并每 2 秒在仪表板显示屏上更新一次。

欢迎提出任何建议!提前致谢!

找到解决方案:

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