Class: Console::Event::Spawn

Inherits:
Generic
  • Object
show all
Defined in:
lib/console/event/spawn.rb

Overview

Represents a child process spawn event.

“‘ruby Console.info(self, **Console::Event::Spawn.for(“ls”, “-l”))

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generic

#as_json, #to_json, #to_s

Constructor Details

#initialize(environment, arguments, options) ⇒ Spawn

Create a new spawn event.



40
41
42
43
44
45
46
47
48
49
# File 'lib/console/event/spawn.rb', line 40

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

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



55
56
57
# File 'lib/console/event/spawn.rb', line 55

def end_time
  @end_time
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



52
53
54
# File 'lib/console/event/spawn.rb', line 52

def start_time
  @start_time
end

#statusObject

Returns the value of attribute status.



58
59
60
# File 'lib/console/event/spawn.rb', line 58

def status
  @status
end

#The end time of the command.(end) ⇒ Object (readonly)



55
# File 'lib/console/event/spawn.rb', line 55

attr :end_time

#The start time of the command.(starttimeofthecommand.) ⇒ Object (readonly)



52
# File 'lib/console/event/spawn.rb', line 52

attr :start_time

#The status of the command, if it has completed.(statusofthecommand) ⇒ Object (readonly)



58
# File 'lib/console/event/spawn.rb', line 58

attr :status

Class Method Details

.for(*arguments, **options) ⇒ Object

Create a new spawn event.



25
26
27
28
29
30
31
32
33
# File 'lib/console/event/spawn.rb', line 25

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

Instance Method Details

#durationObject

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



71
72
73
74
75
# File 'lib/console/event/spawn.rb', line 71

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

#emit(*arguments, **options) ⇒ Object

Log the spawn event.



99
100
101
102
# File 'lib/console/event/spawn.rb', line 99

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

#to_hashObject

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/console/event/spawn.rb', line 80

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