Class: PatientHttp::Sidekiq::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/patient_http/sidekiq/context.rb

Overview

Provides thread-safe context for Sidekiq jobs.

This class manages the current Sidekiq job context using a thread-id keyed hash, allowing async HTTP requests to access job information without it being passed explicitly. Only RequestWorker needs this context for re-enqueueing jobs.

Defined Under Namespace

Classes: Middleware

Class Method Summary collapse

Class Method Details

.current_jobHash?

Returns the current Sidekiq job hash from context.

Returns:

  • (Hash, nil)

    the current job hash or nil if no job context is set



37
38
39
# File 'lib/patient_http/sidekiq/context.rb', line 37

def current_job
  @jobs[Thread.current.object_id]
end

.with_job(job) { ... } ⇒ Object

Sets the current job context for the duration of the block.

Parameters:

  • job (Hash)

    the Sidekiq job hash

Yields:

  • executes the block with the job context set

Returns:

  • (Object)

    the return value of the block



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/patient_http/sidekiq/context.rb', line 46

def with_job(job)
  thread_id = Thread.current.object_id
  previous_job = @jobs[thread_id]
  @jobs[thread_id] = job
  yield
ensure
  if previous_job
    @jobs[thread_id] = previous_job
  else
    @jobs.delete(thread_id)
  end
end