Class: Datadog::Core::Remote::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/core/remote/worker.rb

Overview

Worker executes a block every interval on a separate Thread

Instance Method Summary collapse

Constructor Details

#initialize(interval:, &block) ⇒ Worker

Returns a new instance of Worker.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/datadog/core/remote/worker.rb', line 8

def initialize(interval:, &block)
  @mutex = Mutex.new
  @thr = nil

  @starting = false
  @started = false
  @stopped = false

  @interval = interval
  raise ArgumentError, 'can not initialize a worker without a block' unless block

  @block = block
end

Instance Method Details

#startObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/datadog/core/remote/worker.rb', line 22

def start
  Datadog.logger.debug { 'remote worker starting' }

  acquire_lock

  if @stopped
    Datadog.logger.debug('remote worker: refusing to restart after previous stop')
    return
  end

  return if @starting || @started

  @starting = true

  thread = Thread.new { poll(@interval) }
  thread.name = self.class.name
  thread.thread_variable_set(:fork_safe, true)
  @thr = thread

  @started = true
  @starting = false

  Datadog.logger.debug { 'remote worker started' }
ensure
  release_lock
end

#started?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/datadog/core/remote/worker.rb', line 70

def started?
  @started
end

#stopObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/datadog/core/remote/worker.rb', line 49

def stop
  Datadog.logger.debug { 'remote worker stopping' }

  acquire_lock

  thread = @thr

  if thread
    thread.kill
    thread.join
  end

  @started = false
  @thr = nil
  @stopped = true

  Datadog.logger.debug { 'remote worker stopped' }
ensure
  release_lock
end