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:
parent
f4c81098f1
commit
046ccef679
3 changed files with 41 additions and 2 deletions
|
@ -1,11 +1,11 @@
|
||||||
# stdlib
|
# stdlib
|
||||||
require 'singleton'
|
|
||||||
require 'monitor'
|
require 'monitor'
|
||||||
require 'ostruct'
|
require 'ostruct'
|
||||||
|
require 'singleton'
|
||||||
|
|
||||||
# gems
|
# gems
|
||||||
require 'sidekiq'
|
|
||||||
require 'ice_cube'
|
require 'ice_cube'
|
||||||
|
require 'sidekiq'
|
||||||
|
|
||||||
# c extensions
|
# c extensions
|
||||||
require 'sidetiq_ext'
|
require 'sidetiq_ext'
|
||||||
|
@ -13,6 +13,7 @@ require 'sidetiq_ext'
|
||||||
# internal
|
# internal
|
||||||
require 'sidetiq/config'
|
require 'sidetiq/config'
|
||||||
require 'sidetiq/clock'
|
require 'sidetiq/clock'
|
||||||
|
require 'sidetiq/middleware'
|
||||||
require 'sidetiq/schedule'
|
require 'sidetiq/schedule'
|
||||||
require 'sidetiq/schedulable'
|
require 'sidetiq/schedulable'
|
||||||
require 'sidetiq/version'
|
require 'sidetiq/version'
|
||||||
|
|
20
lib/sidetiq/middleware.rb
Normal file
20
lib/sidetiq/middleware.rb
Normal 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
18
test/test_middleware.rb
Normal 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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue