Class: Datadog::Profiling::WorkerState
- Inherits:
-
Object
- Object
- Datadog::Profiling::WorkerState
- Defined in:
- lib/datadog/profiling/worker_state.rb
Overview
Thread-safe helper, used to watch for success vs failure for a background worker thread.
Instance Method Summary collapse
- #failed!(failure) ⇒ Object
-
#failure ⇒ Object
: () -> Exception?.
-
#initialize ⇒ WorkerState
constructor
: () -> void.
-
#reset! ⇒ Object
: () -> void.
-
#running! ⇒ Object
: () -> void.
-
#wait(timeout_seconds) ⇒ Object
Waits until the worker runs or fails, and returns its failure, :running, or :not_started on timeout.
Constructor Details
#initialize ⇒ WorkerState
: () -> void
13 14 15 16 17 18 |
# File 'lib/datadog/profiling/worker_state.rb', line 13 def initialize @mutex = Mutex.new @condition = ConditionVariable.new @running = false @failure = nil end |
Instance Method Details
#failed!(failure) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/datadog/profiling/worker_state.rb', line 30 def failed!(failure) @mutex.synchronize do @failure = failure @condition.broadcast end end |
#failure ⇒ Object
: () -> Exception?
46 47 48 |
# File 'lib/datadog/profiling/worker_state.rb', line 46 def failure @mutex.synchronize { @failure } end |
#reset! ⇒ Object
: () -> void
38 39 40 41 42 43 |
# File 'lib/datadog/profiling/worker_state.rb', line 38 def reset! @mutex.synchronize do @running = false @failure = nil end end |
#running! ⇒ Object
: () -> void
21 22 23 24 25 26 |
# File 'lib/datadog/profiling/worker_state.rb', line 21 def running! @mutex.synchronize do @running = true @condition.broadcast end end |
#wait(timeout_seconds) ⇒ Object
Waits until the worker runs or fails, and returns its failure, :running, or :not_started on timeout.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/datadog/profiling/worker_state.rb', line 53 def wait(timeout_seconds) @mutex.synchronize do @condition.wait(@mutex, timeout_seconds) unless @running || @failure failure = @failure return failure if failure return @running ? :running : :not_started end end |