Class: Async::Container::Generic

Inherits:
Object
  • Object
show all
Defined in:
lib/async/container/generic.rb

Overview

A base class for implementing containers.

Direct Known Subclasses

Forked, Threaded

Constant Summary collapse

UNNAMED =
"Unnamed"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Generic

Initialize the container.



45
46
47
48
49
50
51
52
53
# File 'lib/async/container/generic.rb', line 45

def initialize(**options)
	@group = Group.new(**options)
	@running = true
	
	@state = {}
	
	@statistics = Statistics.new
	@keyed = {}
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



56
57
58
# File 'lib/async/container/generic.rb', line 56

def group
  @group
end

#stateObject (readonly)

Returns the value of attribute state.



64
65
66
# File 'lib/async/container/generic.rb', line 64

def state
  @state
end

#statisticsObject

Statistics relating to the behavior of children instances.



80
81
82
# File 'lib/async/container/generic.rb', line 80

def statistics
  @statistics
end

#The group of running children instances.(groupofrunningchildreninstances.) ⇒ Object (readonly)



56
# File 'lib/async/container/generic.rb', line 56

attr :group

Class Method Details

.runObject

Run a new container.



36
37
38
# File 'lib/async/container/generic.rb', line 36

def self.run(...)
	self.new.run(...)
end

Instance Method Details

#[](key) ⇒ Object

Look up a child process by key. A key could be a symbol, a file path, or something else which the child instance represents.



74
75
76
# File 'lib/async/container/generic.rb', line 74

def [] key
	@keyed[key]&.value
end

#async(**options, &block) ⇒ Object

Deprecated.

Please use #spawn or run instead.



230
231
232
233
234
235
236
237
238
# File 'lib/async/container/generic.rb', line 230

def async(**options, &block)
	# warn "#{self.class}##{__method__} is deprecated, please use `spawn` or `run` instead.", uplevel: 1
	
	require "async"
	
	spawn(**options) do |instance|
		Async(instance, &block)
	end
end

#failed?Boolean

Whether any failures have occurred within the container.

Returns:

  • (Boolean)


84
85
86
# File 'lib/async/container/generic.rb', line 84

def failed?
	@statistics.failed?
end

#key?(key) ⇒ Boolean

Whether a child instance exists for the given key.

Returns:

  • (Boolean)


269
270
271
272
273
# File 'lib/async/container/generic.rb', line 269

def key?(key)
	if key
		@keyed.key?(key)
	end
end

#mark?(key) ⇒ Boolean

Mark the container’s keyed instance which ensures that it won’t be discarded.

Returns:

  • (Boolean)


256
257
258
259
260
261
262
263
264
265
266
# File 'lib/async/container/generic.rb', line 256

def mark?(key)
	if key
		if value = @keyed[key]
			value.mark!
			
			return true
		end
	end
	
	return false
end

#reloadObject

Reload the container’s keyed instances.



241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/async/container/generic.rb', line 241

def reload
	@keyed.each_value(&:clear!)
	
	yield
	
	dirty = false
	
	@keyed.delete_if do |key, value|
		value.stop? && (dirty = true)
	end
	
	return dirty
end

#run(count: Container.processor_count, **options, &block) ⇒ Object

Run multiple instances of the same block in the container.



221
222
223
224
225
226
227
# File 'lib/async/container/generic.rb', line 221

def run(count: Container.processor_count, **options, &block)
	count.times do
		spawn(**options, &block)
	end
	
	return self
end

#running?Boolean

Whether the container has running children instances.

Returns:

  • (Boolean)


89
90
91
# File 'lib/async/container/generic.rb', line 89

def running?
	@group.running?
end

#sizeObject



59
60
61
# File 'lib/async/container/generic.rb', line 59

def size
	@group.size
end

#sleep(duration = nil) ⇒ Object

Sleep until some state change occurs.



95
96
97
# File 'lib/async/container/generic.rb', line 95

def sleep(duration = nil)
	@group.sleep(duration)
end

#spawn(name: nil, restart: false, key: nil, health_check_timeout: nil, &block) ⇒ Object

Spawn a child instance into the container.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/async/container/generic.rb', line 164

def spawn(name: nil, restart: false, key: nil, health_check_timeout: nil, &block)
	name ||= UNNAMED
	
	if mark?(key)
		Console.debug(self) {"Reusing existing child for #{key}: #{name}"}
		return false
	end
	
	@statistics.spawn!
	
	fiber do
		while @running
			child = self.start(name, &block)
			
			state = insert(key, child)
			
			# If a health check is specified, we will monitor the child process and terminate it if it does not update its state within the specified time.
			if health_check_timeout
				age_clock = state[:age] = Clock.start
			end
			
			begin
				status = @group.wait_for(child) do |message|
					case message
					when :health_check!
						if health_check_timeout&.<(age_clock.total)
							health_check_failed!(child, age_clock, health_check_timeout)
						end
					else
						state.update(message)
						age_clock&.reset!
					end
				end
			ensure
				delete(key, child)
			end
			
			if status.success?
				Console.debug(self) {"#{child} exited with #{status}"}
			else
				@statistics.failure!
				Console.error(self, status: status)
			end
			
			if restart
				@statistics.restart!
			else
				break
			end
		end
	end.resume
	
	return true
end

#status?(flag) ⇒ Boolean

Returns true if all children instances have the specified status flag set. e.g. ‘:ready`. This state is updated by the process readiness protocol mechanism. See Notify::Client for more details.

Returns:

  • (Boolean)


108
109
110
111
# File 'lib/async/container/generic.rb', line 108

def status?(flag)
	# This also returns true if all processes have exited/failed:
	@state.all?{|_, state| state[flag]}
end

#stop(timeout = true) ⇒ Object

Stop the children instances.



141
142
143
144
145
146
147
148
149
150
# File 'lib/async/container/generic.rb', line 141

def stop(timeout = true)
	@running = false
	@group.stop(timeout)
	
	if @group.running?
		Console.warn(self) {"Group is still running after stopping it!"}
	end
ensure
	@running = true
end

#The state of each child instance.=(stateofeachchildinstance. = (value)) ⇒ Object



64
# File 'lib/async/container/generic.rb', line 64

attr :state

#to_sObject

A human readable representation of the container.



68
69
70
# File 'lib/async/container/generic.rb', line 68

def to_s
	"#{self.class} with #{@statistics.spawns} spawns and #{@statistics.failures} failures."
end

#waitObject

Wait until all spawned tasks are completed.



100
101
102
# File 'lib/async/container/generic.rb', line 100

def wait
	@group.wait
end

#wait_until_readyObject

Wait until all the children instances have indicated that they are ready.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/async/container/generic.rb', line 115

def wait_until_ready
	while true
		Console.debug(self) do |buffer|
			buffer.puts "Waiting for ready:"
			@state.each do |child, state|
				buffer.puts "\t#{child.inspect}: #{state}"
			end
		end
		
		self.sleep
		
		if self.status?(:ready)
			Console.logger.debug(self) do |buffer|
				buffer.puts "All ready:"
				@state.each do |child, state|
					buffer.puts "\t#{child.inspect}: #{state}"
				end
			end
			
			return true
		end
	end
end