Class: PatientHttp::Sidekiq::TaskMonitorThread

Inherits:
Object
  • Object
show all
Includes:
TimeHelper
Defined in:
lib/patient_http/sidekiq/task_monitor_thread.rb

Overview

Background thread that maintains heartbeats and performs garbage collection for in-flight HTTP requests.

Constant Summary collapse

MAX_MONITOR_SLEEP =

Minimum seconds to sleep between monitor thread checks

5.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, task_monitor, inflight_ids_callback) ⇒ void

Initialize the monitor thread.

Parameters:

  • config (Configuration)

    the configuration object

  • task_monitor (TaskMonitor)

    the inflight request registry

  • inflight_ids_callback (Proc)

    callback to get current inflight request IDs



25
26
27
28
29
30
31
32
# File 'lib/patient_http/sidekiq/task_monitor_thread.rb', line 25

def initialize(config, task_monitor, inflight_ids_callback)
  @config = config
  @task_monitor = task_monitor
  @inflight_ids_callback = inflight_ids_callback
  @thread = nil
  @running = Concurrent::AtomicBoolean.new(false)
  @stop_signal = Concurrent::Event.new
end

Instance Attribute Details

#configConfiguration (readonly)

Returns the configuration object.

Returns:



14
15
16
# File 'lib/patient_http/sidekiq/task_monitor_thread.rb', line 14

def config
  @config
end

#task_monitorTaskMonitor (readonly)

Returns the inflight request registry.

Returns:



17
18
19
# File 'lib/patient_http/sidekiq/task_monitor_thread.rb', line 17

def task_monitor
  @task_monitor
end

Instance Method Details

#running?Boolean

Check if monitor thread is running.

Returns:

  • (Boolean)


69
70
71
# File 'lib/patient_http/sidekiq/task_monitor_thread.rb', line 69

def running?
  @running.true?
end

#startvoid

This method returns an undefined value.

Start the monitor thread.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/patient_http/sidekiq/task_monitor_thread.rb', line 37

def start
  return if @running.true?
  @running.make_true
  @stop_signal.reset

  @task_monitor.ping_process

  @thread = Thread.new do
    run
  rescue => e
    # Log error but don't crash
    @config.logger&.error("[PatientHttp::Sidekiq] Monitor error: #{e.message}\n#{e.backtrace.join("\n")}")
    raise if ::Sidekiq.testing?
  end

  @thread.name = "patient-http-monitor"
end

#stopvoid

This method returns an undefined value.

Stop the monitor thread.



58
59
60
61
62
63
64
# File 'lib/patient_http/sidekiq/task_monitor_thread.rb', line 58

def stop
  @running.make_false
  @stop_signal.set  # Interrupt the sleep immediately
  @thread&.join(1)
  @thread&.kill if @thread&.alive?
  @thread = nil
end