我一直在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
问题是,在我让它工作之前,它会在localhost上获取页面,计算"p"项目的数量并将其打印在一个文件上,然后dashing文件会读取它并正确显示它,但除非我重新启动它,否则它不会更新仪表板上的值,这违背了这个框架的目的。
现在到错误:
当试图编译sample.rb(dashing文件)时:
$ 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/CoffeeScript组件,但我认为问题出在sample.rb上,Number小部件是完全默认的。
如果代码不够清晰,我要做的是获取localhost页面,计算"p"项目的数量(稍后当我切换到jenkins时,它将是活动的构建,因为我正在处理证书,所以还没有切换),然后将其发送到sample.rb,它将获取数据,并在仪表板显示上每2秒更新一次。
欢迎提出任何建议!提前感谢!
找到解决方案:
卸载/重新安装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