Sha256: 1d9d6eca1fa92985f23a08fa79af188201cea8ea122647d5a6114bb7968984ab

Contents?: true

Size: 1.43 KB

Versions: 7

Compression:

Stored size: 1.43 KB

Contents

module Sidetiq
  class Handler
    include Logging
    include Sidekiq::ExceptionHandler

    def dispatch(worker, tick)
      schedule = worker.schedule

      return unless schedule.schedule_next?(tick)

      Lock::Redis.new(worker).synchronize do |redis|
        if schedule.backfill? && (last = worker.last_scheduled_occurrence) > 0
          last = Sidetiq.config.utc ? Time.at(last).utc : Time.at(last)
          schedule.occurrences_between(last + 1, tick).each do |past_t|
            enqueue(worker, past_t, redis)
          end
        end

        enqueue(worker, schedule.next_occurrence(tick), redis)
      end
    rescue StandardError => e
      handle_exception(e, context: "Sidetiq::Handler#dispatch")
      raise e
    end

    private

    def enqueue(worker, time, redis)
      key      = "sidetiq:#{worker.name}"
      time_f   = time.to_f
      next_run = (redis.get("#{key}:next") || -1).to_f

      if next_run < time_f
        info "Enqueue: #{worker.name} (at: #{time_f}) (last: #{next_run})"

        redis.mset("#{key}:last", next_run, "#{key}:next", time_f)

        case worker.instance_method(:perform).arity.abs
        when 0
          worker.perform_at(time)
        when 1
          worker.perform_at(time, next_run)
        else
          worker.perform_at(time, next_run, time_f)
        end
      end
    rescue StandardError => e
      handle_exception(e, context: "Sidetiq::Handler#enqueue")
      raise e
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sidetiq-0.5.0 lib/sidetiq/handler.rb
sidetiq-0.4.3 lib/sidetiq/handler.rb
sidetiq-0.4.2 lib/sidetiq/handler.rb
sidetiq-0.4.1 lib/sidetiq/handler.rb
sidetiq-0.4.0 lib/sidetiq/handler.rb
sidetiq-0.4.0.rc4 lib/sidetiq/handler.rb
sidetiq-0.4.0.rc3 lib/sidetiq/handler.rb