1
0
Fork 0
mirror of https://github.com/endofunky/sidetiq.git synced 2022-11-09 13:53:30 -05:00
endofunky--sidetiq/lib/sidetiq/schedulable.rb

56 lines
1.4 KiB
Ruby
Raw Normal View History

2013-01-31 17:42:19 +00:00
module Sidetiq
# Public: Mixin for Sidekiq::Worker classes.
#
# Examples
#
# class MyWorker
2013-03-11 17:11:37 +00:00
# include Sidekiq::Worker
# include Sidetiq::Schedulable
#
2013-03-11 17:11:37 +00:00
# # Daily at midnight
# recurrence { daily }
2013-03-11 17:11:37 +00:00
# end
2013-01-31 17:42:19 +00:00
module Schedulable
module ClassMethods
2013-03-11 14:51:31 +00:00
# Public: Returns a Float timestamp of the last scheduled run.
def last_scheduled_occurrence
get_timestamp "last"
end
2013-03-11 14:51:31 +00:00
# Public: Returns a Float timestamp of the next scheduled run.
def next_scheduled_occurrence
get_timestamp "next"
end
def tiq(*args, &block) # :nodoc:
Sidetiq.logger.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:
clock = Sidetiq::Clock.instance
clock.mon_synchronize do
2013-03-11 17:11:37 +00:00
schedule = clock.schedule_for(self)
schedule.instance_eval(&block)
schedule.set_options(options)
2013-01-31 17:42:19 +00:00
end
end
2013-03-11 17:11:37 +00:00
private
2013-03-08 16:07:25 +00:00
def get_timestamp(key)
Sidekiq.redis do |redis|
(redis.get("sidetiq:#{name}:#{key}") || -1).to_f
end
end
2013-01-31 17:42:19 +00:00
end
def self.included(klass) # :nodoc:
2013-01-31 17:42:19 +00:00
klass.extend(Sidetiq::Schedulable::ClassMethods)
end
end
end
2013-03-11 14:51:31 +00:00