Skip to content

Commit

Permalink
Added feature to Force kill instance (#15)
Browse files Browse the repository at this point in the history
* Start force kill

* Force kill an instance

* Check if process exists

* Added changelog

* Fix tests

* Fix rubocop warnings
  • Loading branch information
dannnylo authored Oct 12, 2024
1 parent 7df25e9 commit bd5239b
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## [Unreleased]
## [0.3.6] - 2024-09-27

- Added force kill to instances

## [0.3.5] - 2024-09-27

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.5)
sidekiq-belt (0.3.6)
sidekiq (> 7.1.4)

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

require_relative "run_job"
require_relative "top_label"
require_relative "force_kill"

module Sidekiq
module Belt
Expand All @@ -12,6 +13,7 @@ module Files
def self.use!(options = [:all])
Sidekiq::Belt::Community::RunJob.use! if should_use?(:run_job, options)
Sidekiq::Belt::Community::TopLabel.use! if should_use?(:top_label, options)
Sidekiq::Belt::Community::ForceKill.use! if should_use?(:force_kill, options)

true
end
Expand Down
48 changes: 48 additions & 0 deletions lib/sidekiq/belt/community/force_kill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

require "sidekiq/web"
require "sidekiq/web/helpers"
require "byebug"

module Sidekiq
module Belt
module Community
module ForceKill
def kill!
signal("KILL")
end

module SidekiqForceKill
def self.registered(app)
app.replace_content("/busy") do |content|
content.gsub!("<%= t('Stop') %></button>") do
"<%= t('Stop') %></button>" \
"<% if process.stopping? %>" \
"<a href=\"<%= root_path %>/force_kill/<%= process['identity'] %>/kill\" " \
"class=\"btn btn-xs btn-danger\" data-confirm=\"<%= t('AreYouSure') %>\">" \
"<%= t('Kill') %></a>" \
"<% end %>"
end
end

app.get("/force_kill/:identity/kill") do
process = Sidekiq::ProcessSet[params["identity"]]

if process
process.stop!
process.kill!
end

return redirect "#{root_path}busy"
end
end
end

def self.use!
Sidekiq::Web.register(Sidekiq::Belt::Community::ForceKill::SidekiqForceKill)
Sidekiq::Process.prepend(Sidekiq::Belt::Community::ForceKill)
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.5"
VERSION = "0.3.6"
end
end
26 changes: 18 additions & 8 deletions lib/sidekiq/web_action_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,28 @@

module Sidekiq
module WebActionHelper
def render(engine, content, options = {})
path_info = ::Rack::Utils.unescape(env["PATH_INFO"])
class ERB < ::ERB
def initialize(content)
replace_views = Sidekiq::Config::DEFAULTS[:replace_views] || {}

replace_views = Sidekiq::Config::DEFAULTS[:replace_views] || {}
replace_views.each do |key, content_blocks|
next if WebRoute.new("", key, true).match("", self.class.path_info).nil?

replace_views.each do |key, content_blocks|
next if WebRoute.new("", key, true).match("", path_info).nil?

content_blocks.each do |content_block|
content_block.call(content)
content_blocks.each do |content_block|
content_block.call(content)
end
end

super
end

class << self
attr_accessor :path_info
end
end

def erb(content, options = {})
ERB.path_info = ::Rack::Utils.unescape(env["PATH_INFO"])

super
end
Expand Down
8 changes: 5 additions & 3 deletions spec/sidekiq/web_action_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: false

RSpec.describe(Sidekiq::WebActionHelper) do
describe ".render" do
describe "ERB" do
let(:dummy_web_action) do
Class.new do
def env
Expand Down Expand Up @@ -30,7 +30,8 @@ def render(_engine, content, _options = {})
end

it "replaces the page content with the block content" do
expect(dummy_web_action.new.render("erb", "<a>Sidekiq default<a>")).to eq("<a>Sidekiq replaced twice<a>")
dummy_web_action::ERB.path_info = "/"
expect(dummy_web_action::ERB.new("<a>Sidekiq default<a>").result).to eq("<a>Sidekiq replaced twice<a>")
end
end

Expand All @@ -40,7 +41,8 @@ def render(_engine, content, _options = {})
end

it "replaces the page content with the block content" do
expect(dummy_web_action.new.render("erb", "<a>Sidekiq default<a>")).to eq("<a>Sidekiq default<a>")
dummy_web_action::ERB.path_info = "/"
expect(dummy_web_action::ERB.new("<a>Sidekiq default<a>").result).to eq("<a>Sidekiq default<a>")
end
end
end
Expand Down

0 comments on commit bd5239b

Please sign in to comment.