Sha256: 74e57021e35fa690f195c24883afae8a5181add0064a243460123c7a2a30e158

Contents?: true

Size: 993 Bytes

Versions: 8

Compression:

Stored size: 993 Bytes

Contents

module Pallets
  class Scheduler
    def initialize(manager)
      @manager = manager
      @needs_to_stop = false
      @thread = nil
    end

    def start
      @thread ||= Thread.new { work }
    end

    def shutdown
      @needs_to_stop = true

      return unless @thread
      @thread.join
    end

    def needs_to_stop?
      @needs_to_stop
    end

    def debug
      @thread.backtrace
    end

    def id
      "S#{@thread.object_id.to_s(36)}".upcase if @thread
    end

    private

    def work
      loop do
        break if needs_to_stop?

        backend.reschedule_all(Time.now.to_f)
        wait_a_bit
      end
    end

    def wait_a_bit
      # Wait for roughly 10 seconds
      # We don't want to block the entire polling interval, since we want to
      # deal with shutdowns synchronously and as fast as possible
      10.times do
        break if needs_to_stop?
        sleep 1
      end
    end

    def backend
      @backend ||= Pallets.backend
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
pallets-0.9.0 lib/pallets/scheduler.rb
pallets-0.8.0 lib/pallets/scheduler.rb
pallets-0.7.0 lib/pallets/scheduler.rb
pallets-0.6.0 lib/pallets/scheduler.rb
pallets-0.5.1 lib/pallets/scheduler.rb
pallets-0.5.0 lib/pallets/scheduler.rb
pallets-0.4.0 lib/pallets/scheduler.rb
pallets-0.3.0 lib/pallets/scheduler.rb