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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interval:, logger:, &block) ⇒ Worker

Returns a new instance of Worker.

Raises:

  • (ArgumentError)


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

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

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

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

  @block = block
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



23
24
25
# File 'lib/datadog/core/remote/worker.rb', line 23

def logger
  @logger
end

Instance Method Details

#startObject



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

def start
  logger.debug { "remote worker starting (pid: #{Process.pid})" }

  @mutex.synchronize do
    if @stopped
      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
  end

  logger.debug { 'remote worker started' }
end

#started?Boolean

Returns:

  • (Boolean)


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

def started?
  @started
end

#stopObject



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

def stop
  logger.debug { "remote worker stopping (pid: #{Process.pid})" }

  @mutex.synchronize do
    thread = @thr

    if thread
      thread.kill
      thread.join
    end

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

  logger.debug { 'remote worker stopped' }
end