2016-09-12 124 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添加到顶部,现在所有作品。欢呼的帮助。 –

相关问题