ConsoleSourceConsoleEventSpawn

class Spawn

Represents a child process spawn event.

Console.info(self, **Console::Event::Spawn.for("ls", "-l"))

event = Console::Event::Spawn.for("ls", "-l")
event.status = Process.wait

Definitions

def self.for(*arguments, **options)

Create a new spawn event.

Signature

parameter arguments Array

The arguments to the command, similar to how you would pass them to Kernel.system or Process.spawn.

parameter options Hash

The options to pass to the command, similar to how you would pass them to Kernel.system or Process.spawn.

returns Spawn

The new spawn event representing the command.

Implementation

def self.for(*arguments, **options)
	# Extract out the command environment:
	if arguments.first.is_a?(Hash)
		environment = arguments.shift
		self.new(environment, arguments, options)
	else
		self.new(nil, arguments, options)
	end
end

def initialize(environment, arguments, options)

Create a new spawn event.

Signature

parameter environment Hash

The environment to use when running the command.

parameter arguments Array

The arguments used for command execution.

parameter options Hash

The options to pass to the command, similar to how you would pass them to Kernel.system or Process.spawn.

Implementation

def initialize(environment, arguments, options)
	@environment = environment
	@arguments = arguments
	@options = options
	
	@start_time = Clock.now
	
	@end_time = nil
	@status = nil
end

attr :start_time

Signature

attribute Numeric

The start time of the command.

attr :end_time

Signature

attribute Numeric

The end time of the command.

attr :status

Signature

attribute Process::Status

The status of the command, if it has completed.

def status=(status)

Set the status of the command, and record the end time.

Signature

parameter status Process::Status

The status of the command.

Implementation

def status=(status)
	@end_time = Time.now
	@status = status
end

def duration

Calculate the duration of the command, if it has completed.

Signature

returns Numeric

The duration of the command.

Implementation

def duration
	if @end_time
		@end_time - @start_time
	end
end

def to_hash

Convert the spawn event to a hash suitable for JSON serialization.

Signature

returns Hash

The hash representation of the spawn event.

Implementation

def to_hash
	Hash.new.tap do |hash|
		hash[:type] = :spawn
		hash[:environment] = @environment if @environment&.any?
		hash[:arguments] = @arguments if @arguments&.any?
		hash[:options] = @options if @options&.any?
		
		hash[:status] = @status.to_i if @status
		
		if duration = self.duration
			hash[:duration] = duration
		end
	end
end

def emit(*arguments, **options)

Log the spawn event.

Signature

parameter arguments Array

The arguments to log.

parameter options Hash

Additional options to pass to the logger output.

Implementation

def emit(*arguments, **options)
	options[:severity] ||= :info
	super
end