Sha256: f4b2fa233ab7f209fe5da5eb8ff7f74023c8204a60463e4f71a986bf9534af43
Contents?: true
Size: 1.97 KB
Versions: 3
Compression:
Stored size: 1.97 KB
Contents
# frozen_string_literal: true module Datadog module Core module Remote # Worker executes a block every interval on a separate Thread class Worker def initialize(interval:, &block) @mutex = Mutex.new @thr = nil @starting = false @stopping = false @started = false @interval = interval raise ArgumentError, 'can not initialize a worker without a block' unless block @block = block end def start Datadog.logger.debug { 'remote worker starting' } acquire_lock return if @starting || @started @starting = true thread = Thread.new { poll(@interval) } thread.name = self.class.name unless Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.3') thread.thread_variable_set(:fork_safe, true) @thr = thread @started = true @starting = false Datadog.logger.debug { 'remote worker started' } ensure release_lock end def stop Datadog.logger.debug { 'remote worker stopping' } acquire_lock @stopping = true thread = @thr if thread thread.kill thread.join end @started = false @stopping = false @thr = nil Datadog.logger.debug { 'remote worker stopped' } ensure release_lock end def started? @started end private def acquire_lock @mutex.lock end def release_lock @mutex.unlock end 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
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
ddtrace-1.20.0 | lib/datadog/core/remote/worker.rb |
ddtrace-1.19.0 | lib/datadog/core/remote/worker.rb |
ddtrace-1.18.0 | lib/datadog/core/remote/worker.rb |