diff --git a/.gitignore b/.gitignore index 535f984..a742c1b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ /pkg/ /spec/reports/ /tmp/ +.vscode # rspec failure tracking .rspec_status diff --git a/CHANGELOG.md b/CHANGELOG.md index f9c4008..a318091 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 89e96f8..e4cf363 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - sidekiq-belt (0.3.1) + sidekiq-belt (0.3.2) sidekiq (> 7.1.4) GEM diff --git a/lib/sidekiq/belt/ent/files.rb b/lib/sidekiq/belt/ent/files.rb index 63ecee5..a1f2596 100644 --- a/lib/sidekiq/belt/ent/files.rb +++ b/lib/sidekiq/belt/ent/files.rb @@ -4,6 +4,7 @@ require_relative "periodic_pause" require_relative "periodic_run" +require_relative "periodic_sort" module Sidekiq module Belt @@ -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 diff --git a/lib/sidekiq/belt/ent/periodic_sort.rb b/lib/sidekiq/belt/ent/periodic_sort.rb new file mode 100644 index 0000000..67f9496 --- /dev/null +++ b/lib/sidekiq/belt/ent/periodic_sort.rb @@ -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 diff --git a/lib/sidekiq/belt/version.rb b/lib/sidekiq/belt/version.rb index 07c27ba..8afac9e 100644 --- a/lib/sidekiq/belt/version.rb +++ b/lib/sidekiq/belt/version.rb @@ -2,6 +2,6 @@ module Sidekiq module Belt - VERSION = "0.3.1" + VERSION = "0.3.2" end end diff --git a/lib/sidekiq/web_action_helper.rb b/lib/sidekiq/web_action_helper.rb index 36cc65d..bf940d3 100644 --- a/lib/sidekiq/web_action_helper.rb +++ b/lib/sidekiq/web_action_helper.rb @@ -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 diff --git a/spec/sidekiq/belt/ent/files_spec.rb b/spec/sidekiq/belt/ent/files_spec.rb index c0adf58..efd4333 100644 --- a/spec/sidekiq/belt/ent/files_spec.rb +++ b/spec/sidekiq/belt/ent/files_spec.rb @@ -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 diff --git a/spec/sidekiq/belt/ent/periodic_sort_spec.rb b/spec/sidekiq/belt/ent/periodic_sort_spec.rb new file mode 100644 index 0000000..741901e --- /dev/null +++ b/spec/sidekiq/belt/ent/periodic_sort_spec.rb @@ -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