Async::Job::Adapter::AsyncJobSourceAsyncJobAdapterActiveJobThreadLocalDispatcher

class ThreadLocalDispatcher

Used for dispatching jobs to a thread-local queue to avoid thread-safety issues.

Definitions

def initialize(definitions, aliases)

Initialize with queue definitions and aliases.

Signature

parameter definitions Hash

The queue definitions.

parameter aliases Hash

The queue aliases.

Implementation

def initialize(definitions, aliases)
	@definitions = definitions
	@aliases = aliases
end

attr :definitions

Signature

attribute Hash(String, Proc)

The definitions to use for creating queues.

attr :aliases

Signature

attribute Hash(String, String)

The aliases for the job queues.

def dispatcher

The dispatcher for the current thread.

As a dispatcher contains state, it is important to ensure that each thread has its own dispatcher.

Implementation

def dispatcher
	Thread.current.async_job_adapter_active_job_dispatcher ||= Dispatcher.new(@definitions, @aliases)
end

def [](name)

Get a queue by name.

Signature

parameter name String

The queue name.

Implementation

def [](name)
	dispatcher[name]
end

def call(job)

Dispatch a job using the thread-local dispatcher.

Signature

parameter job ActiveJob::Base

The job to dispatch.

Implementation

def call(job)
	dispatcher.call(job)
end

def start(name)

Start processing jobs in the queue with the given name.

Signature

parameter name String

The name of the queue.

Implementation

def start(name)
	dispatcher.start(name)
end

def keys

The names of all the queues that are available for processing jobs.

Implementation

def keys
	@definitions.keys
end

def status_string

Get the status string for the dispatcher, useful for process titles.

Implementation

def status_string
	dispatcher.status_string
end