我一直在使用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>
&#13;
现在,来自破坏,修改的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上获取页面,计算&#34; p&#34;将项目打印并打印在文件上,然后划线文件将读取并正确显示,但它不会更新仪表板上的值,除非我重新启动它,这违背了此框架的目的
现在出错:
尝试编译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小部件完全是默认的。
如果代码不够清楚,我想要做的就是获取本地主页,计算&#34; p&#34;项目(后来当我切换到jenkins时,它将成为活动版本,因为我正在处理证书而没有切换),然后将其发送到sample.rb,这将获得数据并在仪表板显示屏上每2秒更新一次。
欢迎任何建议!提前谢谢!
答案 0 :(得分: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
&#13;