Module: Datadog::Core::Workers::Async::Thread

Defined in:
lib/datadog/core/workers/async.rb

Overview

Adds threading behavior to workers to run tasks asynchronously.

Defined Under Namespace

Modules: PrependedMethods

Constant Summary collapse

FORK_POLICY_STOP =
:stop
FORK_POLICY_RESTART =
:restart
SHUTDOWN_TIMEOUT =
1
MUTEX_INIT =

This single shared mutex is used to avoid concurrency issues during the initialization of per-instance lazy-initialized mutexes.

Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



31
32
33
# File 'lib/datadog/core/workers/async.rb', line 31

def error
  @error
end

#fork_policyObject



93
94
95
# File 'lib/datadog/core/workers/async.rb', line 93

def fork_policy
  @fork_policy ||= FORK_POLICY_STOP
end

#resultObject

Returns the value of attribute result.



31
32
33
# File 'lib/datadog/core/workers/async.rb', line 31

def result
  @result
end

Class Method Details

.included(base) ⇒ Object



20
21
22
# File 'lib/datadog/core/workers/async.rb', line 20

def self.included(base)
  base.prepend(PrependedMethods)
end

Instance Method Details

#completed?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/datadog/core/workers/async.rb', line 81

def completed?
  !worker.nil? && worker.status == false && !error?
end

#error?Boolean

Returns:

  • (Boolean)


75
76
77
78
79
# File 'lib/datadog/core/workers/async.rb', line 75

def error?
  return false unless instance_variable_defined?(:@error)

  !@error.nil?
end

#failed?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/datadog/core/workers/async.rb', line 85

def failed?
  !worker.nil? && worker.status.nil?
end

#forked?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/datadog/core/workers/async.rb', line 89

def forked?
  !pid.nil? && pid != Process.pid
end

#join(timeout = nil) ⇒ Object



38
39
40
41
42
# File 'lib/datadog/core/workers/async.rb', line 38

def join(timeout = nil)
  return true unless running?

  !worker.join(timeout).nil?
end

#run_async?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/datadog/core/workers/async.rb', line 61

def run_async?
  return false unless instance_variable_defined?(:@run_async)

  @run_async == true
end

#running?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/datadog/core/workers/async.rb', line 71

def running?
  !worker.nil? && worker.alive?
end

#started?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/datadog/core/workers/async.rb', line 67

def started?
  !(worker.nil? || forked?)
end

#terminateObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/datadog/core/workers/async.rb', line 44

def terminate
  return false unless running?

  @run_async = false
  Datadog.logger.debug { "Forcibly terminating worker thread for: #{self}" }
  worker.terminate
  # Wait for the worker thread to end
  begin
    Timeout.timeout(SHUTDOWN_TIMEOUT) do
      worker.join
    end
  rescue Timeout::Error
    Datadog.logger.debug { "Worker thread did not end after #{SHUTDOWN_TIMEOUT} seconds: #{self}" }
  end
  true
end