Skip to content

Commit

Permalink
Added feature to sort jobs on periodic tab (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
dannnylo authored Feb 1, 2024
1 parent c2328b8 commit 843117b
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/pkg/
/spec/reports/
/tmp/
.vscode

# rspec failure tracking
.rspec_status
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [Unreleased]

## [0.3.2] - 2024-01-07

- Feature to sort periodic list by class name

## [0.3.1] - 2023-11-01

- Added style to pause button
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
sidekiq-belt (0.3.1)
sidekiq-belt (0.3.2)
sidekiq (> 7.1.4)

GEM
Expand Down
12 changes: 8 additions & 4 deletions lib/sidekiq/belt/ent/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require_relative "periodic_pause"
require_relative "periodic_run"
require_relative "periodic_sort"

module Sidekiq
module Belt
Expand All @@ -12,13 +13,16 @@ module Files
def self.use!(options = [:all])
return unless Sidekiq.ent?

all = options.include?(:all)

Sidekiq::Belt::Ent::PeriodicPause.use! if all || options.include?(:periodic_pause)
Sidekiq::Belt::Ent::PeriodicRun.use! if all || options.include?(:periodic_run)
Sidekiq::Belt::Ent::PeriodicPause.use! if should_use?(:periodic_pause, options)
Sidekiq::Belt::Ent::PeriodicRun.use! if should_use?(:periodic_run, options)
Sidekiq::Belt::Ent::PeriodicSort.use! if should_use?(:periodic_sort, options)

true
end

def self.should_use?(key, options)
options.include?(:all) || options.include?(key)
end
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions lib/sidekiq/belt/ent/periodic_sort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require "sidekiq/web/helpers"

module Sidekiq
module Belt
module Ent
module PeriodicSort
module SidekiqLoopsPeriodicSort
def each(&block)
@lids.map { |lid| Sidekiq::Periodic::Loop.new(lid) }.sort_by(&:klass).each(&block)
end
end

def self.use!
require("sidekiq-ent/periodic")
require("sidekiq-ent/periodic/static_loop")

Sidekiq::Periodic::LoopSet.prepend(Sidekiq::Belt::Ent::PeriodicSort::SidekiqLoopsPeriodicSort)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/sidekiq/belt/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Sidekiq
module Belt
VERSION = "0.3.1"
VERSION = "0.3.2"
end
end
4 changes: 3 additions & 1 deletion lib/sidekiq/web_action_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def render(engine, content, options = {})

path_info ||= ::Rack::Utils.unescape(env["PATH_INFO"])

Sidekiq::Config::DEFAULTS[:replace_views].fetch(path_info.to_s, []).each do |content_block|
replace_views = Sidekiq::Config::DEFAULTS[:replace_views] || {}

replace_views.fetch(path_info.to_s, []).each do |content_block|
content_block.call(content)
end

Expand Down
1 change: 1 addition & 0 deletions spec/sidekiq/belt/ent/files_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
before do
allow(Sidekiq::Belt::Ent::PeriodicPause).to receive(:use!).and_return(true)
allow(Sidekiq::Belt::Ent::PeriodicRun).to receive(:use!).and_return(true)
allow(Sidekiq::Belt::Ent::PeriodicSort).to receive(:use!).and_return(true)
end

context "when Sidekiq is not Ent" do
Expand Down
59 changes: 59 additions & 0 deletions spec/sidekiq/belt/ent/periodic_sort_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

require "sidekiq"
require "sidekiq/web"

RSpec.describe(Sidekiq::Belt::Ent::PeriodicSort) do
describe ".each" do
let(:dummy_class) do
Class.new do
def initialize(lids)
@lids = lids
end
end
end

let(:jobs) do
[
Sidekiq::Periodic::Loop.new(1, "BClass"),
Sidekiq::Periodic::Loop.new(2, "DClass"),
Sidekiq::Periodic::Loop.new(3, "CClass"),
Sidekiq::Periodic::Loop.new(4, "AClass"),
Sidekiq::Periodic::Loop.new(5, "ZClass")
]
end

before do
dummy_class.prepend(described_class::SidekiqLoopsPeriodicSort)
stub_const("Sidekiq::Periodic", Module.new)
stub_const("Sidekiq::Periodic::Loop", Struct.new(:id, :klass))

allow(Sidekiq::Periodic::Loop).to receive(:new).and_return(jobs[0], jobs[1], jobs[2], jobs[3], jobs[4])
end

it "sort periodic jobs by class name" do
expect(dummy_class.new(
[1, 2, 3, 4, 5]
).each.map(&:klass)).to eq(%w[AClass BClass CClass DClass ZClass])
end
end

describe ".use!" do
before do
stub_const("Sidekiq::Periodic", Module.new)
stub_const("Sidekiq::Periodic::LoopSet", Class.new)

allow(described_class).to receive(:require).and_return(true)
allow(Sidekiq::Periodic::LoopSet).to receive(:prepend)
end

it "injects the code" do
described_class.use!

expect(described_class).to have_received(:require).with("sidekiq-ent/periodic").once
expect(described_class).to have_received(:require).with("sidekiq-ent/periodic/static_loop").once

expect(Sidekiq::Periodic::LoopSet).to have_received(:prepend).with(described_class::SidekiqLoopsPeriodicSort)
end
end
end

0 comments on commit 843117b

Please sign in to comment.