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

Track worker execution history.

This commit is contained in:
Tobias Svensson 2013-09-16 14:48:51 +01:00
parent e3fb989b74
commit 4e009089ac
8 changed files with 128 additions and 2 deletions

View file

@ -5,6 +5,7 @@ require 'sidekiq/web'
require 'sidetiq/web'
require './workers/simple.rb'
require './workers/failing.rb'
Sidekiq.configure_client do |config|
config.redis = { :size => 1 }

View file

@ -2,7 +2,9 @@
require 'sidekiq'
require 'sidetiq'
require_relative 'workers/simple'
require_relative 'workers/failing'
Sidekiq.logger.level = Logger::DEBUG

View file

@ -0,0 +1,13 @@
class Failing
include Sidekiq::Worker
include Sidetiq::Schedulable
recurrence { secondly }
def perform(*args)
if rand(5) == 0
raise "This didn't go well."
end
end
end

View file

@ -5,7 +5,6 @@ class Simple
recurrence { secondly }
def perform(*args)
Sidekiq.logger.info "#perform"
end
end

View file

@ -20,6 +20,9 @@ require 'sidetiq/schedule'
require 'sidetiq/schedulable'
require 'sidetiq/version'
# middleware
require 'sidetiq/middleware/history'
# actor topology
require 'sidetiq/actor/clock'
require 'sidetiq/actor/handler'

View file

@ -24,7 +24,7 @@ module Sidetiq
end
configure do |config|
config.priority = Thread.main.priority
config.worker_history = 100
config.resolution = 1
config.lock_expire = 1000
config.utc = false

View file

@ -0,0 +1,48 @@
module Sidetiq
module Middleware
class History
def call(worker, msg, queue, &block)
if worker.kind_of?(Sidetiq::Schedulable)
call_with_sidetiq_history(worker, msg, queue, &block)
else
yield
end
end
def call_with_sidetiq_history(worker, msg, queue)
entry = {
status: :success,
error: "",
exception: "",
backtrace: "",
processor: "#{Socket.gethostname}:#{Process.pid}-#{Thread.current.object_id}",
processed: Time.now.iso8601
}
yield
rescue StandardError => e
entry[:status] = :failure
entry[:exception] = e.class.to_s
entry[:error] = e.message
entry[:backtrace] = e.backtrace
raise e
ensure
Sidekiq.redis do |redis|
redis.pipelined do |pipe|
list_name = "sidetiq:#{worker.class.name}:history"
pipe.lpush(list_name, JSON.dump(entry))
pipe.ltrim(list_name, 0, Sidetiq.config.worker_history - 1)
end
end
end
end
end
end
Sidekiq.configure_server do |config|
config.server_middleware do |chain|
chain.add Sidetiq::Middleware::History
end
end

60
test/test_history.rb Normal file
View file

@ -0,0 +1,60 @@
require_relative 'helper'
class TestHistory < Sidetiq::TestCase
class HistoryWorker
include Sidekiq::Worker
include Sidetiq::Schedulable
end
def test_success
middlewared do; end
entry = Sidekiq.redis do |redis|
redis.lrange('sidetiq:TestHistory::HistoryWorker:history', 0, -1)
end
actual = JSON.parse(entry[0], symbolize_names: true)
assert_equal 'success', actual[:status]
assert_empty actual[:error]
assert_empty actual[:backtrace]
assert_empty actual[:exception]
refute_empty actual[:processor]
refute_empty actual[:processed]
end
def test_failure
begin
middlewared do
raise StandardError.new("failed")
end
rescue
end
entry = Sidekiq.redis do |redis|
redis.lrange('sidetiq:TestHistory::HistoryWorker:history', 0, -1)
end
actual = JSON.parse(entry[0], symbolize_names: true)
assert_equal 'failure', actual[:status]
assert_equal "failed", actual[:error]
assert_equal "StandardError", actual[:exception]
refute_empty actual[:backtrace]
refute_empty actual[:processor]
refute_empty actual[:processed]
end
def middlewared
middleware = Sidetiq::Middleware::History.new
middleware.call(HistoryWorker.new, {}, 'default') do
yield
end
end
end