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

Use Sidekiq's wrappers for parsing/generating JSON.

This commit is contained in:
Tobias Svensson 2013-09-26 12:11:15 +01:00
parent 9354bc4e08
commit e7bed3a4f4
6 changed files with 8 additions and 9 deletions

View file

@ -13,12 +13,12 @@ module Sidetiq
end end
def from_json(json = "") def from_json(json = "")
# Avoid TypeError when nil is passed to JSON.parse. # Avoid TypeError when nil is passed to Sidekiq.load_json.
json = "" if json.nil? json = "" if json.nil?
hash = JSON.parse(json, symbolize_names: true) hash = Sidekiq.load_json(json).symbolize_keys
new(hash) new(hash)
rescue JSON::ParserError => e rescue StandardError => e
if json != "" if json != ""
# Looks like garbage lock metadata, so report it. # Looks like garbage lock metadata, so report it.
handle_exception(e, context: "Garbage lock meta data detected: #{json}") handle_exception(e, context: "Garbage lock meta data detected: #{json}")

View file

@ -41,7 +41,7 @@ module Sidetiq
Sidekiq.redis do |redis| Sidekiq.redis do |redis|
list_name = "sidetiq:#{worker.class.name}:history" list_name = "sidetiq:#{worker.class.name}:history"
redis.lpush(list_name, JSON.dump(entry)) redis.lpush(list_name, Sidekiq.dump_json(entry))
redis.ltrim(list_name, 0, Sidetiq.config.worker_history - 1) redis.ltrim(list_name, 0, Sidetiq.config.worker_history - 1)
end end
end end

View file

@ -19,7 +19,7 @@
</thead> </thead>
<% @history.each do |entry| %> <% @history.each do |entry| %>
<% entry = JSON.parse(entry, symbolize_names: true) %> <% entry = Sidekiq.load_json(entry).symbolize_keys %>
<tr class="<%= 'error' if entry[:status] == 'failure' %>"> <tr class="<%= 'error' if entry[:status] == 'failure' %>">
<td><%= entry[:status].capitalize %></td> <td><%= entry[:status].capitalize %></td>
<td><%= Time.parse(entry[:timestamp]).strftime("%m/%d/%Y %I:%M:%S%p") %></td> <td><%= Time.parse(entry[:timestamp]).strftime("%m/%d/%Y %I:%M:%S%p") %></td>

View file

@ -22,7 +22,6 @@ Gem::Specification.new do |gem|
gem.add_dependency 'sidekiq', '~> 2.14.0' gem.add_dependency 'sidekiq', '~> 2.14.0'
gem.add_dependency 'celluloid', '>= 0.14.1' gem.add_dependency 'celluloid', '>= 0.14.1'
gem.add_dependency 'ice_cube', '~> 0.11.0' gem.add_dependency 'ice_cube', '~> 0.11.0'
gem.add_dependency 'json'
gem.add_development_dependency 'rake' gem.add_development_dependency 'rake'
gem.add_development_dependency 'sinatra' gem.add_development_dependency 'sinatra'

View file

@ -13,7 +13,7 @@ class TestHistory < Sidetiq::TestCase
redis.lrange('sidetiq:TestHistory::HistoryWorker:history', 0, -1) redis.lrange('sidetiq:TestHistory::HistoryWorker:history', 0, -1)
end end
actual = JSON.parse(entry[0], symbolize_names: true) actual = Sidekiq.load_json(entry[0]).symbolize_keys
assert_equal 'success', actual[:status] assert_equal 'success', actual[:status]
@ -37,7 +37,7 @@ class TestHistory < Sidetiq::TestCase
redis.lrange('sidetiq:TestHistory::HistoryWorker:history', 0, -1) redis.lrange('sidetiq:TestHistory::HistoryWorker:history', 0, -1)
end end
actual = JSON.parse(entry[0], symbolize_names: true) actual = Sidekiq.load_json(entry[0]).symbolize_keys
assert_equal 'failure', actual[:status] assert_equal 'failure', actual[:status]

View file

@ -78,7 +78,7 @@ class TestLockMetaData < Sidetiq::TestCase
hash = { timestamp: 42, owner: "me", key: "foobar" } hash = { timestamp: 42, owner: "me", key: "foobar" }
md = Sidetiq::Lock::MetaData.new(hash) md = Sidetiq::Lock::MetaData.new(hash)
assert_equal hash, JSON.parse(md.to_json, symbolize_names: true) assert_equal hash, Sidekiq.load_json(md.to_json).symbolize_keys
end end
def test_to_s def test_to_s