Module: Arborist::Monitor::DefaultCallbacks

Included in:
RunContext
Defined in:
lib/arborist/monitor.rb

Overview

The module that contains the default logic for invoking an external program to do the work of a Monitor.

Instance Method Summary collapse

Instance Method Details

#exec_arguments(nodes) ⇒ Object

Given one or more nodes, return an Array of arguments that should be appended to the external command.



57
58
59
# File 'lib/arborist/monitor.rb', line 57

def exec_arguments( nodes )
	return []
end

#exec_input(nodes, io) ⇒ Object

Write the specified nodes as serialized data to the given io.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/arborist/monitor.rb', line 63

def exec_input( nodes, io )
	return if io.closed?

	nodes.each do |(identifier, data)|
		self.log.debug "Serializing node properties for %s" % [ identifier ]
		prop_map = data.collect do |key, val|
			val = val.join( ',' ) if val.is_a?( Array )
			"%s=%s" % [ key, Shellwords.escape(val) ]
		end

		self.log.debug "  writing %d properties to %p" % [ prop_map.size, io ]
		io.puts "%s %s" % [ identifier, prop_map.join(' ') ]
		self.log.debug "  wrote the node to FD %d" % [ io.fileno ]
	end

	self.log.debug "done writing to FD %d" % [ io.fileno ]
end

#handle_results(pid, out, err) ⇒ Object

Return the results of running the external command



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/arborist/monitor.rb', line 83

def handle_results( pid, out, err )
	err.flush
	err.close
	self.log.debug "Closed child's stderr."

    # identifier key1=val1 key2=val2
	results = out.each_line.with_object({}) do |line, accum|
		identifier, attributes = line.split( ' ', 2 )
		attrhash = Shellwords.shellsplit( attributes ).each_with_object({}) do |pair, hash|
			key, val = pair.split( '=', 2 )
			hash[ key ] = val
		end

		accum[ identifier ] = attrhash
	end
	out.close

	self.log.debug "Waiting on PID %d" % [ pid ]
	Process.waitpid( pid )

	return results
end