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



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

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)


73
74
75
# File 'lib/datadog/core/workers/async.rb', line 73

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

#error?Boolean

Returns:

  • (Boolean)


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

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

  !@error.nil?
end

#failed?Boolean

Returns:

  • (Boolean)


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

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

#forked?Boolean

Returns:

  • (Boolean)


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

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)


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

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

  @run_async == true
end

#running?Boolean

Returns:

  • (Boolean)


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

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

#started?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/datadog/core/workers/async.rb', line 59

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

#terminateObject



44
45
46
47
48
49
50
51
# 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
  true
end