0

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

现在到错误:

尝试编译 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)>'
(...)

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

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

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

4

1 回答 1

0

找到了解决方案:

卸载/重新安装 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

于 2015-04-22T13:28:33.480 回答