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

View file

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

View file

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

View file

@ -1,10 +1,25 @@
require_relative 'helper' require_relative 'helper'
require 'minitest/mock'
class TestShedulable < Sidetiq::TestCase class TestShedulable < Sidetiq::TestCase
class FakeWorker class FakeWorker
include Sidetiq::Schedulable include Sidetiq::Schedulable
end 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 def test_resheduling
last_run = (Time.now - 100).to_f last_run = (Time.now - 100).to_f
next_run = (Time.now + 100).to_f next_run = (Time.now + 100).to_f