forked from ixti/sidekiq-throttled
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: DRY-up job class/args extraction
Related-PR: ixti#184
- Loading branch information
Showing
8 changed files
with
267 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sidekiq | ||
module Throttled | ||
class Message | ||
def initialize(item) | ||
@item = item.is_a?(Hash) ? item : parse(item) | ||
end | ||
|
||
def job_class | ||
@item.fetch("wrapped") { @item["class"] } | ||
end | ||
|
||
def job_args | ||
@item.key?("wrapped") ? @item.dig("args", 0, "arguments") : @item["args"] | ||
end | ||
|
||
def job_id | ||
@item["jid"] | ||
end | ||
|
||
private | ||
|
||
def parse(item) | ||
item = Sidekiq.load_json(item) | ||
item.is_a?(Hash) ? item : {} | ||
rescue JSON::ParserError | ||
{} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Sidekiq::Throttled::Message do | ||
subject(:message) do | ||
described_class.new(item) | ||
end | ||
|
||
let(:item) do | ||
{ | ||
"class" => "ExcitingJob", | ||
"args" => [42], | ||
"jid" => "deadbeef" | ||
} | ||
end | ||
|
||
describe "#job_class" do | ||
subject { message.job_class } | ||
|
||
it { is_expected.to eq("ExcitingJob") } | ||
|
||
context "with serialized payload" do | ||
let(:item) do | ||
JSON.dump({ | ||
"class" => "ExcitingJob", | ||
"args" => [42], | ||
"jid" => "deadbeef" | ||
}) | ||
end | ||
|
||
it { is_expected.to eq("ExcitingJob") } | ||
end | ||
|
||
context "with ActiveJob payload" do | ||
let(:item) do | ||
{ | ||
"class" => "ActiveJob", | ||
"wrapped" => "ExcitingJob", | ||
"args" => [{ "arguments" => [42] }], | ||
"jid" => "deadbeef" | ||
} | ||
end | ||
|
||
it { is_expected.to eq("ExcitingJob") } | ||
end | ||
|
||
context "with serialized ActiveJob payload" do | ||
let(:item) do | ||
JSON.dump({ | ||
"class" => "ActiveJob", | ||
"wrapped" => "ExcitingJob", | ||
"args" => [{ "arguments" => [42] }], | ||
"jid" => "deadbeef" | ||
}) | ||
end | ||
|
||
it { is_expected.to eq("ExcitingJob") } | ||
end | ||
|
||
context "with invalid payload" do | ||
let(:item) { "invalid" } | ||
|
||
it { is_expected.to be nil } | ||
end | ||
|
||
context "with invalid serialized payload" do | ||
let(:item) { JSON.dump("invalid") } | ||
|
||
it { is_expected.to be nil } | ||
end | ||
end | ||
|
||
describe "#job_args" do | ||
subject { message.job_args } | ||
|
||
it { is_expected.to eq([42]) } | ||
|
||
context "with serialized payload" do | ||
let(:item) do | ||
JSON.dump({ | ||
"class" => "ExcitingJob", | ||
"args" => [42], | ||
"jid" => "deadbeef" | ||
}) | ||
end | ||
|
||
it { is_expected.to eq([42]) } | ||
end | ||
|
||
context "with ActiveJob payload" do | ||
let(:item) do | ||
{ | ||
"class" => "ActiveJob", | ||
"wrapped" => "ExcitingJob", | ||
"args" => [{ "arguments" => [42] }], | ||
"jid" => "deadbeef" | ||
} | ||
end | ||
|
||
it { is_expected.to eq([42]) } | ||
end | ||
|
||
context "with serialized ActiveJob payload" do | ||
let(:item) do | ||
JSON.dump({ | ||
"class" => "ActiveJob", | ||
"wrapped" => "ExcitingJob", | ||
"args" => [{ "arguments" => [42] }], | ||
"jid" => "deadbeef" | ||
}) | ||
end | ||
|
||
it { is_expected.to eq([42]) } | ||
end | ||
|
||
context "with invalid payload" do | ||
let(:item) { "invalid" } | ||
|
||
it { is_expected.to be nil } | ||
end | ||
|
||
context "with invalid serialized payload" do | ||
let(:item) { JSON.dump("invalid") } | ||
|
||
it { is_expected.to be nil } | ||
end | ||
end | ||
|
||
describe "#job_id" do | ||
subject { message.job_id } | ||
|
||
it { is_expected.to eq("deadbeef") } | ||
|
||
context "with serialized payload" do | ||
let(:item) do | ||
JSON.dump({ | ||
"class" => "ExcitingJob", | ||
"args" => [42], | ||
"jid" => "deadbeef" | ||
}) | ||
end | ||
|
||
it { is_expected.to eq("deadbeef") } | ||
end | ||
|
||
context "with ActiveJob payload" do | ||
let(:item) do | ||
{ | ||
"class" => "ActiveJob", | ||
"wrapped" => "ExcitingJob", | ||
"args" => [{ "arguments" => [42] }], | ||
"jid" => "deadbeef" | ||
} | ||
end | ||
|
||
it { is_expected.to eq("deadbeef") } | ||
end | ||
|
||
context "with serialized ActiveJob payload" do | ||
let(:item) do | ||
JSON.dump({ | ||
"class" => "ActiveJob", | ||
"wrapped" => "ExcitingJob", | ||
"args" => [{ "arguments" => [42] }], | ||
"jid" => "deadbeef" | ||
}) | ||
end | ||
|
||
it { is_expected.to eq("deadbeef") } | ||
end | ||
|
||
context "with invalid payload" do | ||
let(:item) { "invalid" } | ||
|
||
it { is_expected.to be nil } | ||
end | ||
|
||
context "with invalid serialized payload" do | ||
let(:item) { JSON.dump("invalid") } | ||
|
||
it { is_expected.to be nil } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters