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.



29
30
31
# File 'lib/datadog/core/workers/async.rb', line 29

def error
  @error
end

#fork_policyObject



83
84
85
# File 'lib/datadog/core/workers/async.rb', line 83

def fork_policy
  @fork_policy ||= FORK_POLICY_STOP
end

#resultObject

Returns the value of attribute result.



29
30
31
# File 'lib/datadog/core/workers/async.rb', line 29

def result
  @result
end

Class Method Details

.included(base) ⇒ Object



18
19
20
# File 'lib/datadog/core/workers/async.rb', line 18

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

Instance Method Details

#completed?Boolean

Returns:

  • (Boolean)


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

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

#error?Boolean

Returns:

  • (Boolean)


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

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

  !@error.nil?
end

#failed?Boolean

Returns:

  • (Boolean)


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

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

#forked?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/datadog/core/workers/async.rb', line 79

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

#join(timeout = nil) ⇒ Object



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

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

  !worker.join(timeout).nil?
end

#run_async?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/datadog/core/workers/async.rb', line 51

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

  @run_async == true
end

#running?Boolean

Returns:

  • (Boolean)


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

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

#started?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/datadog/core/workers/async.rb', line 57

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

#terminateObject



42
43
44
45
46
47
48
49
# File 'lib/datadog/core/workers/async.rb', line 42

def terminate
  return false unless running?

  @run_async = false
  Datadog.logger.debug { "Forcibly terminating worker thread for: #{self}" }
  worker.terminate
  true
end