def process_plugin(plugin)
info "Processing the #{plugin[:name]} plugin:"
last_run = @history["last_runs"][plugin[:name]]
memory = @history["memory"][plugin[:name]]
run_time = Time.now
delta = last_run.nil? ? nil : run_time - (last_run + plugin[:interval])
if last_run.nil? or delta.between?(-RUN_DELTA, 0) or delta >= 0
debug "Plugin is past interval and needs to be run. " +
"(last run: #{last_run || 'nil'})"
debug "Compiling plugin..."
begin
eval(plugin[:code], TOPLEVEL_BINDING, plugin[:path] || plugin[:name])
info "Plugin compiled."
rescue Exception
raise if $!.is_a? SystemExit
error "Plugin would not compile: #{$!.message}"
return
end
debug "Loading plugin..."
if job = Plugin.last_defined.load( last_run, (memory || Hash.new),
plugin[:options] || Hash.new )
info "Plugin loaded."
debug "Running plugin..."
begin
data = {}
Timeout.timeout(PLUGIN_TIMEOUT, PluginTimeoutError) do
data = job.run
end
rescue Timeout::Error
error "Plugin took too long to run."
return
rescue Exception
raise if $!.is_a? SystemExit
error "Plugin failed to run: #{$!.backtrace}"
end
info "Plugin completed its run."
send_report(data[:report], plugin[:plugin_id]) if data[:report]
if data[:reports] and not data[:reports].empty?
data[:reports].each { |r| send_report(r, plugin[:plugin_id]) }
end
send_alert(data[:alert], plugin[:plugin_id]) if data[:alert]
if data[:alerts] and not data[:alerts].empty?
data[:alerts].each { |a| send_alert(a, plugin[:plugin_id]) }
end
send_error(data[:error], plugin[:plugin_id]) if data[:error]
if data[:errors] and not data[:errors].empty?
data[:errors].each { |e| send_error(e, plugin[:plugin_id]) }
end
@history["last_runs"][plugin[:name]] = run_time
@history["memory"][plugin[:name]] = data[:memory]
else
scout_error({:subject => "Plugin would not load."}, plugin[:plugin_id])
end
else
debug "Plugin does not need to be run at this time. " +
"(last run: #{last_run || 'nil'})"
end
data
ensure
if Plugin.last_defined
debug "Removing plugin code..."
begin
Object.send(:remove_const, Plugin.last_defined.to_s.split("::").first)
Plugin.last_defined = nil
info "Plugin Removed."
rescue
raise if $!.is_a? SystemExit
error "Unable to remove plugin."
end
end
info "Plugin #{plugin[:name]} processing complete."
end