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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



25
26
27
# File 'lib/datadog/core/workers/async.rb', line 25

def error
  @error
end

#fork_policyObject



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

def fork_policy
  @fork_policy ||= FORK_POLICY_STOP
end

#resultObject

Returns the value of attribute result.



25
26
27
# File 'lib/datadog/core/workers/async.rb', line 25

def result
  @result
end

Class Method Details

.included(base) ⇒ Object



14
15
16
# File 'lib/datadog/core/workers/async.rb', line 14

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

Instance Method Details

#completed?Boolean

Returns:

  • (Boolean)


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

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

#error?Boolean

Returns:

  • (Boolean)


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

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

  !@error.nil?
end

#failed?Boolean

Returns:

  • (Boolean)


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

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

#forked?Boolean

Returns:

  • (Boolean)


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

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

#join(timeout = nil) ⇒ Object



32
33
34
35
36
# File 'lib/datadog/core/workers/async.rb', line 32

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

  !worker.join(timeout).nil?
end

#run_async?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/datadog/core/workers/async.rb', line 47

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

  @run_async == true
end

#running?Boolean

Returns:

  • (Boolean)


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

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

#started?Boolean

Returns:

  • (Boolean)


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

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

#terminateObject



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

def terminate
  return false unless running?

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