From c7ca1660cc2e0e12fbb3f7bc8cfdb967932cd1b0 Mon Sep 17 00:00:00 2001 From: Nikolay Kondratyev Date: Thu, 3 Jul 2014 16:54:40 +0600 Subject: [PATCH] Schedule recurring jobs only sidekiq server side Fixes #104 --- lib/sidetiq/schedulable.rb | 16 ++++++---------- sidetiq.gemspec | 2 +- test/helper.rb | 6 ++++++ test/test_schedulable.rb | 15 +++++++++++++++ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/lib/sidetiq/schedulable.rb b/lib/sidetiq/schedulable.rb index c7df814..a90917f 100644 --- a/lib/sidetiq/schedulable.rb +++ b/lib/sidetiq/schedulable.rb @@ -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 - diff --git a/sidetiq.gemspec b/sidetiq.gemspec index 14bbf8c..0078261 100644 --- a/sidetiq.gemspec +++ b/sidetiq.gemspec @@ -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' diff --git a/test/helper.rb b/test/helper.rb index a9cd45f..3e6c7c4 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -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 diff --git a/test/test_schedulable.rb b/test/test_schedulable.rb index d3bf43b..6810603 100644 --- a/test/test_schedulable.rb +++ b/test/test_schedulable.rb @@ -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