对破折号小部件进行故障排除

Troubleshooting a dashing widget

我创建了一个小部件,用于显示服务器负载。但是,它不显示服务器负载。

dashboard.erb

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
  <div data-id="serverload" data-view="Graph" data-title="Server load" data-moreinfo="1 min intervals" ></div>
</li>

jobs/serverload.rb

points = []
(1..10).each do |i|
  points << { x: i, y: 0 }
end
last_x = points.last[:x]

SCHEDULER.every '1m' do
  points.shift
  last_x += 1
  uptime = %x('uptime').gsub(/.*: /, '').gsub(/,/, '')
  points << { x: last_x, y: uptime[0..3].to_f }
  send_event('loadavg1min', points: points)
end

很简单,显示如下。我们如何解决这个特定的小部件并找出它失败的地方?

离开 docs 中的示例,尝试添加 first_in: 选项并将散列作为第二个参数传递给 send_event

尝试:

SCHEDULER.every '1m', first_in: 1.second.since do
  points.shift
  last_x += 1
  uptime = %x('uptime').gsub(/.*: /, '').gsub(/,/, '')
  points << { x: last_x, y: uptime[0..3].to_f }
  send_event('loadavg1min', { points: points })
end

文档:

# :first_in sets how long it takes before the job is first run. In this case, 
it is run immediately
Dashing.scheduler.every '1m', first_in: 1.second.since do |job|
  Dashing.send_event('karma', { current: rand(1000) })
end