Sha256: 399a8454691a2ca514d24190cafa2f25a1156d306ceda57e44ab63a67015eeda

Contents?: true

Size: 975 Bytes

Versions: 27

Compression:

Stored size: 975 Bytes

Contents

# frozen_string_literal: true

module Logster
  module Deferer
    attr_reader :queue, :thread
    def initialize
      @queue = Queue.new
      @mutex = Mutex.new
      @thread = nil
      @enabled = true
    end

    def disable
      @enabled = false
    end

    def enable
      @enabled = true
    end

    def schedule(&blk)
      if @enabled
        start_thread if !@thread&.alive?
        @queue << blk
      else
        return if blk == :terminate
        blk.call
      end
    end

    private

    def start_thread
      @mutex.synchronize do
        if !@thread&.alive?
          @thread = Thread.new { do_work }
        end
      end
    end

    def do_work
      while true
        blk = @queue.pop
        # we need to be able to break the loop so that the new
        # thread "finishes" and let us test this code.
        break if blk == :terminate
        blk.call
      end
    end
  end

  class Scheduler
    extend Deferer
    initialize
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
logster-2.12.2 lib/logster/scheduler.rb
logster-2.12.1 lib/logster/scheduler.rb
logster-2.11.4 lib/logster/scheduler.rb
logster-2.11.3 lib/logster/scheduler.rb
logster-2.11.2 lib/logster/scheduler.rb
logster-2.11.1 lib/logster/scheduler.rb
logster-2.11.0 lib/logster/scheduler.rb
logster-2.10.1 lib/logster/scheduler.rb
logster-2.10.0 lib/logster/scheduler.rb
logster-2.9.8 lib/logster/scheduler.rb
logster-2.9.7 lib/logster/scheduler.rb
logster-2.9.6 lib/logster/scheduler.rb
logster-2.9.5 lib/logster/scheduler.rb
logster-2.9.4 lib/logster/scheduler.rb
logster-2.9.3 lib/logster/scheduler.rb
logster-2.9.2 lib/logster/scheduler.rb
logster-2.9.1 lib/logster/scheduler.rb
logster-2.9.0 lib/logster/scheduler.rb
logster-2.8.0 lib/logster/scheduler.rb
logster-2.7.1 lib/logster/scheduler.rb