Dashing 启动时 main:Object (NameError) 的未定义局部变量或方法“conn”

Undefined local variable or method `conn' for main:Object (NameError) on Dashing start

我正在尝试设置一个 Dashing 仪表板来进行一些监控。用于填充仪表板小部件的数据将来自 Oracle 数据库。 Dashing 已安装并且测试仪表板运行良好,但是当我创建自己的作业以使用来自 Oracle 实例的数据填充小部件时,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 代码 (new.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>

注释掉 SCHEDULER & send_event 行允许脚本 运行 手动并按预期返回结果。

这是我第一次使用 Ruby,所以请原谅任何明显的错误,但如果有人可以帮助让它工作,我将不胜感激。

错误消息告诉您您需要知道的一切: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