Class: RSpecBackgroundProcess::ProcessPool

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-background-process/server.rb,
lib/rspec-background-process/process_pool.rb,
lib/rspec-background-process/refresh_actions.rb,
lib/rspec-background-process/readiness_checks.rb

Defined Under Namespace

Classes: LRUPool, ProcessDefinition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ProcessPool

Returns a new instance of ProcessPool.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/rspec-background-process/process_pool.rb', line 243

def initialize(options)
	@stats = {}

	@max_running = options.delete(:max_running) || 4

	@pool = LRUPool.new(@max_running) do |key, instance|
		#puts "too many instances running, stopping: #{instance.name}[#{key}]; #{@pool}"
		stats(instance.name)[:lru_stopped] += 1
		instance.stop
	end

	# keep track of running instances
	@pool.after_store do |key, instance|
		instance.after_state_change do |new_state|
			# we mark running before it is actually started to have a chance to stop over-limit instance first
			if new_state == :starting
				#puts "new instance running: #{instance.name}[#{key}]"
				@pool.running(key)
				stats(instance.name)[:started] += 1
			end
			@pool.not_running(key) if [:not_running, :dead, :jammed].include? new_state
		end

		# mark running if added while already running
		@pool.running(key) if instance.running?

		# init stats
		stats(instance.name)[:started] ||= 0
		stats(instance.name)[:lru_stopped] ||= 0
	end

	# for storing shared data
	@global_context = {}

	# for filling template strings with actual instance data
	@template_renderer = ->(variables, string) {
		out = string.dup
		variables.merge(
			/project directory/ => -> { Dir.pwd.to_s }
		).each do |regexp, source|
			out.gsub!(/<#{regexp}>/) do
				source.call(*$~.captures)
			end
		end
		out
	}

	# this are passed down to instance
	@options = options.merge(
		global_context:  @global_context,
		template_renderer: @template_renderer
	)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



298
299
300
# File 'lib/rspec-background-process/process_pool.rb', line 298

def options
  @options
end

#poolObject (readonly)

Returns the value of attribute pool.



297
298
299
# File 'lib/rspec-background-process/process_pool.rb', line 297

def pool
  @pool
end

Instance Method Details

#cleanupObject



304
305
306
# File 'lib/rspec-background-process/process_pool.rb', line 304

def cleanup
	@pool.reset_active
end

#failed_instanceObject



324
325
326
327
328
329
330
331
332
# File 'lib/rspec-background-process/process_pool.rb', line 324

def failed_instance
	@pool.instances.select do |instance|
		instance.dead? or
		instance.failed? or
		instance.jammed?
	end.sort_by do |instance|
		instance.state_change_time
	end.last
end

#logging_enabled?Boolean

Returns:

  • (Boolean)


300
301
302
# File 'lib/rspec-background-process/process_pool.rb', line 300

def logging_enabled?
	@options[:logging]
end

#report_failed_instanceObject



334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/rspec-background-process/process_pool.rb', line 334

def report_failed_instance
	if failed_instance
		puts "Last failed process instance state log: "
		failed_instance.state_log.each do |log_line|
			puts "\t#{log_line}"
		end
		puts "Working directory: #{failed_instance.working_directory}"
		puts "Log file: #{failed_instance.log_file}"
		puts "State: #{failed_instance.state}"
		puts "Exit code: #{failed_instance.exit_code}"
	else
		puts "No process instance in failed state"
	end
end

#report_logsObject



349
350
351
352
353
354
# File 'lib/rspec-background-process/process_pool.rb', line 349

def report_logs
	puts "Process instance logs:"
	@pool.instances.each do |instance|
		puts "#{instance.name}: #{instance.log_file}"
	end
end

#report_statsObject



312
313
314
315
316
317
318
319
320
321
322
# File 'lib/rspec-background-process/process_pool.rb', line 312

def report_stats
	puts
	puts "Process pool stats (max running: #{@max_running}):"
	@stats.each do |key, stats|
		puts "  #{key}: #{stats.map{|k, v| "#{k}: #{v}"}.join(' ')}"
	end
	puts "Total instances: #{@stats.length}"
	puts "Total starts: #{@stats.reduce(0){|total, stat| total += stat.last[:started]}}"
	puts "Total LRU stops: #{@stats.reduce(0){|total, stat| total += stat.last[:lru_stopped]}}"
	puts "Total extra LRU stops: #{@stats.reduce(0){|total, stat| extra = (stat.last[:lru_stopped] - 1); total += extra if extra > 0; total}}"
end

#stats(name) ⇒ Object



308
309
310
# File 'lib/rspec-background-process/process_pool.rb', line 308

def stats(name)
	@stats[name] ||= {}
end