Async::Container::SupervisorSourceAsyncContainerSupervisorWorker

class Worker

A worker represents a long running process that can be controlled by the supervisor.

There are various tasks that can be executed by the worker, such as dumping memory, threads, and garbage collection profiles.

Definitions

def self.run(...)

Run a worker with the given state.

Signature

parameter state Hash

The worker state (e.g. process_id, instance info).

parameter endpoint IO::Endpoint

The supervisor endpoint to connect to.

Implementation

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

def initialize(state = nil, endpoint: Supervisor.endpoint)

Initialize a new worker.

Signature

parameter state Hash

The worker state to register with the supervisor.

parameter endpoint IO::Endpoint

The supervisor endpoint to connect to.

Implementation

def initialize(state = nil, endpoint: Supervisor.endpoint)
	super(endpoint: endpoint)
	@state = state
end

def do_scheduler_dump(call)

Dump the current fiber scheduler hierarchy.

Generates a hierarchical view of all running fibers and their relationships.

Signature

parameter call Connection::Call

The call to respond to.

parameter call[:path] String

Optional file path to save the dump.

Implementation

def do_scheduler_dump(call)
	dump(call) do |file|
		Fiber.scheduler.print_hierarchy(file)
	end
end

def do_memory_dump(call)

Dump the entire object space to a file.

This is a heavyweight operation that dumps all objects in the heap. Consider using Async::Container::Supervisor::Worker#do_memory_sample for lighter weight memory leak detection.

Signature

parameter call Connection::Call

The call to respond to.

parameter call[:path] String

Optional file path to save the dump.

Implementation

def do_memory_dump(call)
	require "objspace"
	
	dump(call, buffer: false) do |file|
		ObjectSpace.dump_all(output: file)
	end
end

def do_memory_sample(call)

Sample memory allocations over a time period to identify potential leaks.

This method is much lighter weight than Async::Container::Supervisor::Worker#do_memory_dump and focuses on retained objects allocated during the sampling period. Late-lifecycle allocations that are retained are likely memory leaks.

The method samples allocations for the specified duration, forces a garbage collection, and returns a JSON report showing allocated vs retained memory broken down by gem, file, location, and class.

Signature

parameter call Connection::Call

The call to respond to.

parameter call[:duration] Numeric

The duration in seconds to sample for.

Implementation

def do_memory_sample(call)
	require "memory"
	
	unless duration = call[:duration] and duration.positive?
		raise ArgumentError, "Positive duration is required!"
	end
	
	Console.info(self, "Starting memory sampling...", duration: duration)
	
	# Create a sampler to track allocations
	sampler = Memory::Sampler.new
	
	# Start sampling
	sampler.start
	
	# Sample for the specified duration
	sleep(duration)
	
	# Stop sampling
	sampler.stop
	
	report = sampler.report
	
	dump(call) do |file|
		file.puts(report.to_s)
	end
ensure
	GC.start
end

def do_thread_dump(call)

Dump information about all running threads.

Includes thread inspection and backtraces for debugging.

Signature

parameter call Connection::Call

The call to respond to.

parameter call[:path] String

Optional file path to save the dump.

Implementation

def do_thread_dump(call)
	dump(call) do |file|
		Thread.list.each do |thread|
			file.puts(thread.inspect)
			file.puts(thread.backtrace)
		end
	end
end

def do_garbage_profile_start(call)

Start garbage collection profiling.

Enables the GC profiler to track garbage collection performance.

Signature

parameter call Connection::Call

The call to respond to.

Implementation

def do_garbage_profile_start(call)
	GC::Profiler.enable
	call.finish(started: true)
end

def do_garbage_profile_stop(call)

Stop garbage collection profiling and return results.

Disables the GC profiler and returns collected profiling data.

Signature

parameter call Connection::Call

The call to respond to.

parameter call[:path] String

Optional file path to save the profile.

Implementation

def do_garbage_profile_stop(call)
	dump(connection, message) do |file|
		file.puts GC::Profiler.result
	end
ensure
	GC::Profiler.disable
end