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

Schedule recurring jobs only sidekiq server side

Fixes #104
This commit is contained in:
Nikolay Kondratyev 2014-07-03 16:54:40 +06:00
parent b1021d8cf1
commit c7ca1660cc
4 changed files with 28 additions and 11 deletions

View file

@ -38,6 +38,8 @@ module Sidetiq
end
def recurrence(options = {}, &block) # :nodoc:
return unless Sidekiq.server?
schedule.instance_eval(&block)
schedule.set_options(options)
@ -45,28 +47,23 @@ module Sidetiq
old_description = get_schedulable_key("schedule_description")
if old_description != schedule.to_s
get_schedulable_keys.map do |key|
schedulable_redis.del(key)
Sidekiq.redis_pool.with { |r| r.del(key) }
end
set_schedulable_key("schedule_description", schedule.to_s)
end
end
private
def schedulable_redis
Sidekiq.redis { |redis| redis }
end
def get_schedulable_keys
schedulable_redis.keys("sidetiq:#{name}:*")
Sidekiq.redis_pool.with { |r| r.keys("sidetiq:#{name}:*") }
end
def get_schedulable_key(key)
schedulable_redis.get("sidetiq:#{name}:#{key}")
Sidekiq.redis_pool.with { |r| r.get("sidetiq:#{name}:#{key}") }
end
def set_schedulable_key(key, value)
schedulable_redis.set("sidetiq:#{name}:#{key}", value)
Sidekiq.redis_pool.with { |r| r.set("sidetiq:#{name}:#{key}", value) }
end
def get_timestamp(key)
@ -83,4 +80,3 @@ module Sidetiq
end
end
end

View file

@ -19,7 +19,7 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]
gem.extensions = []
gem.add_dependency 'sidekiq', '>= 2.16.0'
gem.add_dependency 'sidekiq', '>= 3.0.0'
gem.add_dependency 'celluloid', '>= 0.14.1'
gem.add_dependency 'ice_cube', '~> 0.12.0'

View file

@ -13,6 +13,12 @@ require 'rack/test'
require 'sidetiq'
require 'sidetiq/web'
module Sidekiq
def self.server?
true
end
end
class Sidetiq::Supervisor
def self.clock
@clock ||= Sidetiq::Clock.new

View file

@ -1,10 +1,25 @@
require_relative 'helper'
require 'minitest/mock'
class TestShedulable < Sidetiq::TestCase
class FakeWorker
include Sidetiq::Schedulable
end
def test_recurrence
Sidekiq.stub(:server?, true) do
Sidekiq.expects(:redis_pool).returns(Sidekiq::RedisConnection.create).at_least(1)
FakeWorker.recurrence { hourly }
end
Sidekiq.stub(:server?, false) do
Sidekiq.expects(:redis_pool).times(0)
FakeWorker.recurrence { hourly }
end
end
def test_resheduling
last_run = (Time.now - 100).to_f
next_run = (Time.now + 100).to_f