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>

そして今、ダッシュからの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 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」アイテムの数を数えることです (後でジェンキンに切り替えたときにアクティブなビルドになります)私は証明書を扱っているので、まだ切り替えません)、それを 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 に答える