Sha256: 0cf5f18874cfe50b25d0672e1ba0bb94722f869a67f5e163c3b28653cd770bc4

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module Puppet::Scheduler
  class SplayJob < Job
    attr_reader :splay, :splay_limit

    def initialize(run_interval, splay_limit, &block)
      @splay, @splay_limit = calculate_splay(splay_limit)
      super(run_interval, &block)
    end

    def interval_to_next_from(time)
      if last_run
        super
      else
        (start_time + splay) - time
      end
    end

    def ready?(time)
      if last_run
        super
      else
        start_time + splay <= time
      end
    end

    # Recalculates splay.
    #
    # @param splay_limit [Integer] the maximum time (in seconds) to delay before an agent's first run.
    # @return @splay [Integer] a random integer less than or equal to the splay limit that represents the seconds to
    # delay before next agent run.
    def splay_limit=(splay_limit)
      if @splay_limit != splay_limit
        @splay, @splay_limit = calculate_splay(splay_limit)
      end
    end

    private

    def calculate_splay(limit)
      [rand(limit + 1), limit]
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
puppet-8.10.0 lib/puppet/scheduler/splay_job.rb
puppet-8.10.0-x86-mingw32 lib/puppet/scheduler/splay_job.rb
puppet-8.10.0-x64-mingw32 lib/puppet/scheduler/splay_job.rb
puppet-8.10.0-universal-darwin lib/puppet/scheduler/splay_job.rb