【问题标题】:Dashing (Ruby) Nokogiri LoadErrorDashing (Ruby) Nokogiri LoadError
【发布时间】:2015-04-22 21:28:33
【问题描述】:

我一直在开发 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 文件已修改(唯一重要的是构建/评估的东西):

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”项的数量并将其打印到文件上,然后破折号文件会读取它并正确显示它,但是除非我重新启动它,否则它不会更新仪表板上的值,这违背了这个框架的目的。

现在是错误:

尝试编译 sample.rb(破折号文件)时:

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

尝试运行 dashing 服务器时:

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

我也可以发布 Number 小部件的 HTML/CSS/CoffeScript 组件,但我认为问题出在 sample.rb 上,而 Number 小部件是完全默认的。

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

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

【问题讨论】:

    标签: ruby-on-rails jenkins nokogiri mechanize-ruby dashing


    【解决方案1】:

    找到解决方案:

    卸载/重新安装 nokogiri gem(不带 sudo) 将我的爬虫放入 lib 文件夹并在作业中要求它 在作业本身上,将所有内容放入SCHEDULER 函数中,如下所示:

    # 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

    【讨论】:

      猜你喜欢
      • 2012-05-20
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多