class DockDriver::DockItem
Runs a command periodically and notifies an observer periodically, and provides output from the command.
In your dock_driver config YAML file (typically ~/.dock_driver.yml), you can specify lists of DockItems to create and make available for your template. For example:
In your YAML file:
dock: items: - name: mail command: dock_mail.rb period: 10
The above creates an item named ‘mail’ that executes the command script dock_mail.rb
every 10 seconds.
To include the output of dock_mail.rb
in your dock, you’d then add the following to the template
section of your YAML file.
<%= mail %>
Attributes
The command to be run.
This item’s name.
The last output from the command.
How many seconds to wait between command executions.
A regex to scan for in the command output.
Public Instance Methods
Lazily create the thread to periodically call poll.
# File lib/dock_driver/dock_item.rb, line 59 def thread return @thread ||= Thread.new { loop { poll } } end
Returns the formatted output from the command.
# File lib/dock_driver/dock_item.rb, line 64 def to_s return @output_lock.synchronize { self.output.to_s } end
Private Instance Methods
Polls a command and update observers if there’s new output.
# File lib/dock_driver/dock_item.rb, line 73 def poll return unless self.command begin # Requires Ruby 1.9+ out,err,status = Open3.capture3( self.command ) if status.success? out = out.strip.gsub( /\s+/, ' ' ) if self.scan_regex and out out = out.scan( self.scan_regex ).flatten.first end if out != self.output self.changed @output_lock.synchronize do self.output = out end self.notify_observers end else self.log.warn "Command failed: \n%s" % [ err ] end rescue Exception => e self.log.warn "Command failed: %s" % [e.message] end sleep self.period end