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

Add middleware to restart the clock if necessary.

This commit is contained in:
Tobias Svensson 2013-02-04 10:54:48 +00:00
parent f4c81098f1
commit 046ccef679
3 changed files with 41 additions and 2 deletions

View file

@ -1,11 +1,11 @@
# stdlib
require 'singleton'
require 'monitor'
require 'ostruct'
require 'singleton'
# gems
require 'sidekiq'
require 'ice_cube'
require 'sidekiq'
# c extensions
require 'sidetiq_ext'
@ -13,6 +13,7 @@ require 'sidetiq_ext'
# internal
require 'sidetiq/config'
require 'sidetiq/clock'
require 'sidetiq/middleware'
require 'sidetiq/schedule'
require 'sidetiq/schedulable'
require 'sidetiq/version'

20
lib/sidetiq/middleware.rb Normal file
View file

@ -0,0 +1,20 @@
module Sidetiq
class Middleware
def initialize
@clock = Sidetiq::Clock.instance
end
def call(*args)
# Restart the clock if the thread died.
@clock.start! if !@clock.ticking?
yield
end
end
end
Sidekiq.configure_server do |config|
config.server_middleware do |chain|
chain.add Sidetiq::Middleware
end
end

18
test/test_middleware.rb Normal file
View file

@ -0,0 +1,18 @@
require_relative 'helper'
class TestMiddleware < Sidetiq::TestCase
def middleware
Sidetiq::Middleware.new
end
def test_restarts_clock
clock.stubs(:ticking?).returns(false)
clock.expects(:start!).once
middleware.call {}
clock.stubs(:ticking?).returns(true)
clock.expects(:start!).never
middleware.call {}
end
end