未定义的局部变量或方法`conn' for main:Dashing start

时间:2016-09-12 14:45:05

标签: ruby-on-rails ruby oracle dashing

我正在尝试设置Dashing信息中心以进行某些监控。用于填充仪表板小部件的数据将来自Oracle数据库。已安装Dashing并且测试仪表板工作正常但是当我创建自己的作业以使用来自Oracle实例的数据填充窗口小部件时,无法启动,则会显示以下错误

<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&amp; send_event行允许手动运行脚本,并按预期返回结果。

这是我第一次使用Ruby,所以请原谅任何明显的错误,但是如果有人能帮助我实现这一点,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

错误消息告诉您需要知道的所有内容:第25行未定义conn变量:

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