Sha256: ba472520c8c4d54531f72ab899ae3e9003a0ad31e04eb78e840321709ab59efa

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

module Sidetiq
  # Public: Mixin for Sidekiq::Worker classes.
  #
  # Examples
  #
  #   class MyWorker
  #     include Sidekiq::Worker
  #     include Sidetiq::Schedulable
  #
  #     # Daily at midnight
  #     recurrence { daily }
  #   end
  module Schedulable
    module ClassMethods
      include Logging

      # Public: Returns a Float timestamp of the last scheduled run.
      def last_scheduled_occurrence
        get_timestamp "last"
      end

      # Public: Returns a Float timestamp of the next scheduled run.
      def next_scheduled_occurrence
        get_timestamp "next"
      end

      def tiq(*args, &block) # :nodoc:
        warn "DEPRECATION WARNING: Sidetiq::Schedulable#tiq" <<
          " is deprecated and will be removed. Use" <<
          " Sidetiq::Schedulable#recurrence instead."
        recurrence(*args, &block)
      end

      def recurrence(options = {}, &block) # :nodoc:
        schedule = Sidetiq.clock.schedule_for(self)
        schedule.instance_eval(&block)
        schedule.set_options(options)
      end

      private

      def get_timestamp(key)
        Sidekiq.redis do |redis|
          (redis.get("sidetiq:#{name}:#{key}") || -1).to_f
        end
      end
    end

    def self.included(klass) # :nodoc:
      klass.extend(Sidetiq::Schedulable::ClassMethods)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sidetiq-0.4.0.rc2 lib/sidetiq/schedulable.rb
sidetiq-0.4.0.rc1 lib/sidetiq/schedulable.rb