Module: HTM::JobAdapter

Defined in:
lib/htm/job_adapter.rb

Overview

Job adapter for pluggable background job backends

Supports multiple job backends to work seamlessly across different application types (CLI, Sinatra, Rails).

Supported backends:

  • :active_job - Rails ActiveJob (recommended for Rails apps)

  • :sidekiq - Direct Sidekiq integration (recommended for Sinatra apps)

  • :inline - Synchronous execution (recommended for CLI and tests)

  • :thread - Background thread (legacy, for standalone apps)

  • :fiber - Fiber-based concurrency using async gem (recommended for I/O-bound jobs)

Examples:

Configure job backend

HTM.configure do |config|
  config.job.backend = :active_job
end

Enqueue a job

HTM::JobAdapter.enqueue(HTM::Jobs::GenerateEmbeddingJob, node_id: 123)

See Also:

  • Async Embedding and Tag Generation

Class Method Summary collapse

Class Method Details

.enqueue(job_class, **params) ⇒ void

This method returns an undefined value.

Enqueue a background job using the configured backend

Parameters:

  • job_class (Class)

    Job class to enqueue (must respond to :perform)

  • params (Hash)

    Parameters to pass to the job

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/htm/job_adapter.rb', line 39

def enqueue(job_class, **params)
  backend = HTM.configuration.job_backend

  case backend
  when :active_job
    enqueue_active_job(job_class, **params)
  when :sidekiq
    enqueue_sidekiq(job_class, **params)
  when :inline
    enqueue_inline(job_class, **params)
  when :thread
    enqueue_thread(job_class, **params)
  when :fiber
    enqueue_fiber(job_class, **params)
  else
    raise HTM::Error, "Unknown job backend: #{backend}. Supported backends: :active_job, :sidekiq, :inline, :thread, :fiber"
  end
end

.enqueue_parallel(jobs) ⇒ void

This method returns an undefined value.

Execute multiple jobs in parallel using fibers Best for I/O-bound jobs like LLM API calls

Examples:

Run embedding and tags jobs in parallel

JobAdapter.enqueue_parallel([
  [GenerateEmbeddingJob, { node_id: 123 }],
  [GenerateTagsJob, { node_id: 123 }]
])

Parameters:

  • jobs (Array<Array>)

    Array of [job_class, params] pairs



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/htm/job_adapter.rb', line 129

def enqueue_parallel(jobs)
  return if jobs.empty?

  backend = HTM.configuration.job_backend

  case backend
  when :fiber
    enqueue_parallel_fiber(jobs)
  when :inline
    # Run sequentially for inline backend
    jobs.each { |job_class, params| enqueue_inline(job_class, **params) }
  else
    # For other backends, enqueue each job separately
    jobs.each { |job_class, params| enqueue(job_class, **params) }
  end
end