Skip to content

Commit

Permalink
Add remote configuration worker
Browse files Browse the repository at this point in the history
  • Loading branch information
lloeki authored and GustavoCaso committed Mar 21, 2023
1 parent 5edffaa commit 42b74f7
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions lib/datadog/core/remote/worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# frozen_string_literal: true

module Datadog
module Core
module Remote
class Worker
def initialize(interval:, &block)
@mutex = Mutex.new
@thr = nil

@starting = false
@stopping = false
@started = false

@interval = interval
@block = block
end

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

@mutex.lock

return if @starting || @started

@starting = true

@thr = Thread.new { poll(@interval) }

@started = true
@starting = false

Datadog.logger.debug { 'remote worker started' }
ensure
@mutex.unlock
end

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

@mutex.lock

@stopping = true

@thr.kill unless @thr.nil?

@started = false
@stopping = false

Datadog.logger.debug { 'remote worker stopped' }
ensure
@mutex.unlock
end

def started?
@started
end

private

def poll(interval)
loop do
break unless @mutex.synchronize { @starting || @started }

call

sleep(interval)
end
end

def call
Datadog.logger.debug { 'remote worker perform' }

@block.call
end
end
end
end
end

0 comments on commit 42b74f7

Please sign in to comment.