From 42b74f716dad21ac514550e0f75c43e9cc105032 Mon Sep 17 00:00:00 2001 From: Loic Nageleisen Date: Tue, 14 Mar 2023 13:13:34 +0100 Subject: [PATCH] Add remote configuration worker --- lib/datadog/core/remote/worker.rb | 79 +++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 lib/datadog/core/remote/worker.rb diff --git a/lib/datadog/core/remote/worker.rb b/lib/datadog/core/remote/worker.rb new file mode 100644 index 00000000000..3c6071b7b92 --- /dev/null +++ b/lib/datadog/core/remote/worker.rb @@ -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