2016-09-12 46 views
0

我正在嘗試爲某些監控設置Dashing儀表板。用於填充儀表板小部件的數據將來自Oracle數據庫。短跑已經安裝和測試儀表板工作正常,但是當我創建了自己的工作來用Oracle實例瀟灑無法啓動數據的小部件,會顯示以下錯誤未定義的本地變量或方法`conn'爲main:Object(NameError)on Dashing開始

<pre>/home/{home}/dashboard/jobs/new.rb:25:in `<top (required)>': undefined local variable or method `conn' for main:Object (NameError) 
    from /usr/local/lib/ruby/gems/2.0.0/gems/backports-3.6.8/lib/backports/std_lib.rb:9:in `require' 
    from /usr/local/lib/ruby/gems/2.0.0/gems/backports-3.6.8/lib/backports/std_lib.rb:9:in `require_with_backports' 
    from /usr/local/lib/ruby/gems/2.0.0/gems/dashing-1.3.7/lib/dashing/app.rb:171:in `block in require_glob' 
    from /usr/local/lib/ruby/gems/2.0.0/gems/dashing-1.3.7/lib/dashing/app.rb:170:in `each' 
    from /usr/local/lib/ruby/gems/2.0.0/gems/dashing-1.3.7/lib/dashing/app.rb:170:in `require_glob' 
    from /usr/local/lib/ruby/gems/2.0.0/gems/dashing-1.3.7/lib/dashing/app.rb:181:in `<top (required)>' 
    from /usr/local/lib/ruby/gems/2.0.0/gems/dashing-1.3.7/lib/dashing.rb:3:in `require' 
    from /usr/local/lib/ruby/gems/2.0.0/gems/dashing-1.3.7/lib/dashing.rb:3:in `<top (required)>' 
    from config.ru:1:in `require' 
    from config.ru:1:in `block in <main>' 
    from /usr/local/lib/ruby/gems/2.0.0/gems/rack-1.5.5/lib/rack/builder.rb:55:in `instance_eval' 
    from /usr/local/lib/ruby/gems/2.0.0/gems/rack-1.5.5/lib/rack/builder.rb:55:in `initialize' 
    from config.ru:1:in `new' 
    from config.ru:1:in `<main>' 
    from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.6.4/lib/rack/adapter/loader.rb:33:in `eval' 
    from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.6.4/lib/rack/adapter/loader.rb:33:in `load' 
    from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.6.4/lib/thin/controllers/controller.rb:182:in `load_rackup_config' 
    from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.6.4/lib/thin/controllers/controller.rb:72:in `start' 
    from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.6.4/lib/thin/runner.rb:200:in `run_command' 
    from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.6.4/lib/thin/runner.rb:156:in `run!' 
    from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.6.4/bin/thin:6:in `<top (required)>' 
    from /usr/local/bin/thin:23:in `load' 
    from /usr/local/bin/thin:23:in `<main>'</pre> 

的Ruby代碼(新.RB)正在運行是

<pre>require 'rubygems' 
require 'oci8' 

# :first_in sets how long it takes before the job is first run. In this case, it is run immediately 
SCHEDULER.every '15m', :first_in => 0 do |job| 

tnsname = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = {ORACLE_SERVER})(PORT = 1521)) (CONNECT_DATA = (SID = {SID})))' 

conn = OCI8.new('{USER}', '{PASS}', tnsname) 

last_value = rowitem 

cursor = conn.parse("SELECT count(status) AS status_count FROM generation WHERE billcycle is NULL AND status = '13'") 
cursor.define(1, Integer) 
cursor.exec() 
rowitem = [] 

while r = cursor.fetch() 
     rowitem = r 
end 

send_event('new', { current: rowitem, last: last_value }) 
cursor.close() 
end 
conn.logoff</pre> 

談到了調度器& send_event線允許腳本可以手動運行和預期的結果被返回。

這是我第一次使用Ruby,所以原諒任何明顯的錯誤,但如果任何人都可以幫助得到這個工作,我將不勝感激。

回答

0

錯誤消息告訴你你需要知道的一切:conn變量沒有定義,在第25行:

SCHEDULER.every '15m', :first_in => 0 do |job| 
    # ... 

    conn = OCI8.new('{USER}', '{PASS}', tnsname) 

    # ... 
end 

conn.logoff 

您定義conn的範圍內,然後試圖訪問它從外面。這不是變量的工作原理 - 你不能從他們定義的地方以外訪問它們。

一個平凡的解決方法是移動logoff方法調用了塊內:

SCHEDULER.every '15m', :first_in => 0 do |job| 
    # ... 

    conn = OCI8.new('{USER}', '{PASS}', tnsname) 

    # ... 

    conn.logoff 
end 
+0

我喜歡它時,答案是盯着你的臉是那麼簡單。在我的辯護中,我使用這個https://github.com/Shopify/dashing/issues/344來構建ruby腳本。還必須將r​​owitem = 0添加到頂部,現在所有作品。歡呼的幫助。 –

相關問題