sidekiq-restart 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 80063a8294300dc8b0ff7b3523b9ff573d84f7f5
4
+ data.tar.gz: 22b2fab8f1b3f69181ecaf0e7367feef0defd5b9
5
+ SHA512:
6
+ metadata.gz: 71738950003b0db441995bb3e7e21d1559e36dce51278d9f46260bd9b8e515e119b99f856f1f948e8f83c53b6ca7adbbfcb0006ec09e199169d44e44c11f5f24
7
+ data.tar.gz: 30b30b5043b2ba986fb9fc02392750e0f3acb0ccecee1bb68394a0cbaf56b4048289b6d07582d67b610c2476d9c5bd633f6f28de18ca3b949c40c5cfa971b656
@@ -0,0 +1,3 @@
1
+ Gemfile.lock
2
+ pkg
3
+ /tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --colour
2
+ -I ./lib
3
+ -r spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sidekiq-restart.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jon Rowe
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ # Sidekiq::Restart
2
+
3
+ Restart workers for Sidekiq via the web interface.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sidekiq-restart'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sidekiq-restart
18
+
19
+ ## Usage
20
+
21
+ Wherever you mount `Sidekiq::Web`
22
+
23
+ ```Ruby
24
+ require 'sidekiq/restart'
25
+ ```
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ begin
5
+ require 'rspec/core/rake_task'
6
+
7
+ desc "Run tests"
8
+ RSpec::Core::RakeTask.new do |t|
9
+ t.ruby_opts = %w[-w]
10
+ end
11
+
12
+ task default: %w[spec]
13
+
14
+ rescue LoadError
15
+ puts "RSpec Unavailable"
16
+ end
@@ -0,0 +1 @@
1
+ require "sidekiq/restart"
@@ -0,0 +1,22 @@
1
+ require "sidekiq/restart/version"
2
+ require "sidekiq/restart/command"
3
+ require "sidekiq/restart/web_extension"
4
+
5
+ begin
6
+ require "sinatra"
7
+ require "sidekiq/web"
8
+
9
+ Sidekiq::Web.register Sidekiq::Restart::WebExtension
10
+ rescue LoadError
11
+ # client-only usage
12
+ end
13
+
14
+ module Sidekiq
15
+ module Restart
16
+ module_function
17
+
18
+ def worker id
19
+ Command.new(Sidekiq).run id
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,26 @@
1
+ module Sidekiq
2
+ module Restart
3
+ class Command
4
+
5
+ def initialize sidekiq
6
+ @sidekiq = sidekiq
7
+ end
8
+
9
+ def run id
10
+ @sidekiq.redis do |conn|
11
+ payload = conn.get("worker:#{id}")
12
+ if payload
13
+ msg = @sidekiq.load_json(payload)['payload']
14
+ Sidekiq::Client.push msg
15
+
16
+ # cleanup redis details about the worker
17
+ conn.srem("workers", id)
18
+ conn.del("worker:#{id}")
19
+ conn.del("worker:#{id}:started")
20
+ end
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ module Restart
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ module Sidekiq
2
+ module Restart
3
+ module WebExtension
4
+
5
+ def self.registered(app)
6
+ app.use Rack::MethodOverride
7
+
8
+ app.helpers do
9
+ def find_template(sidekiq_views, name, engine, &block)
10
+ view_path = File.join(File.expand_path("../../../../web", __FILE__), "views")
11
+ super(view_path, name, engine, &block)
12
+ super(sidekiq_views, name, engine, &block)
13
+ end
14
+ end
15
+
16
+ app.delete "/workers/:id" do |id|
17
+ Restart.worker id
18
+ redirect "#{root_path}workers", 301
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sidekiq/restart/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sidekiq-restart"
8
+ spec.version = Sidekiq::Restart::VERSION
9
+ spec.authors = ["Jon Rowe"]
10
+ spec.email = ["hello@jonrowe.co.uk"]
11
+ spec.license = "MIT"
12
+ spec.description = %q{Allows manual restarts of Sidekiq workers.}
13
+ spec.summary = %q{Allows manual restarts of Sidekiq workers via the Web UI.}
14
+ spec.homepage = "https://github.com/JonRowe/sidekiq-restart/"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(spec)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "sidekiq", "~> 2.17.2"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rack-test"
26
+ spec.add_development_dependency "sinatra"
27
+ spec.add_development_dependency "rspec", "~> 3.0.0.beta1"
28
+ end
@@ -0,0 +1,62 @@
1
+ require 'rack/test'
2
+ require 'logger'
3
+
4
+ describe "integrating with sidekiq web", type: :integration do
5
+ include Rack::Test::Methods
6
+
7
+ before :all do
8
+ require 'sidekiq/restart'
9
+ require 'sidekiq/testing'
10
+ Sidekiq.logger = Logger.new(nil)
11
+ Sidekiq::Testing.disable!
12
+ end
13
+
14
+ let(:app) do
15
+ Sidekiq::Web
16
+ end
17
+
18
+ it "overrides the workers index" do
19
+ get '/workers'
20
+
21
+ expect(last_response).to be_ok
22
+ expect(last_response.body).to match '<th>Controls</th>'
23
+ end
24
+
25
+ context "when there are stuck workers" do
26
+ before do
27
+ hash = { queue: 'test', payload: { class: Class, args: [], queue: 'test' }, run_at: Time.now.to_i }
28
+ Sidekiq.redis do |conn|
29
+ conn.del 'queue:test'
30
+ conn.sadd('workers', 'my_id')
31
+ conn.set("worker:my_id:started", Time.now.to_s)
32
+ conn.set("worker:my_id", Sidekiq.dump_json(hash))
33
+ end
34
+ end
35
+
36
+ it "allows the developer to restart workers" do
37
+ get '/workers'
38
+
39
+ expect(last_response).to be_ok
40
+ expect(last_response.body).to match(
41
+ %r%<form action="/workers/my_id" [^>]*>.*<button[^>]*>Restart</button>.*</form>%m
42
+ )
43
+ end
44
+
45
+ it "will restart a stuck job" do
46
+ delete "/workers/my_id"
47
+ expect(last_response).to be_redirect
48
+ expect(last_response.location).to eq 'http://example.org/workers'
49
+ Sidekiq.redis do |conn|
50
+ expect(conn.llen('queue:test')).to eq 1
51
+ end
52
+ end
53
+ end
54
+
55
+ context "when there are no stuck workers" do
56
+ it "is idemoptent against non existant jobs" do
57
+ delete "/workers/my_id"
58
+ expect(last_response).to be_redirect
59
+ expect(last_response.location).to eq 'http://example.org/workers'
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,8 @@
1
+ #loaded by .rspec
2
+
3
+ RSpec.configure do |config|
4
+ config.register_ordering :global do |examples|
5
+ unit, integration = examples.partition { |example| example.metadata[:type] != :integration }
6
+ unit.shuffle + integration.shuffle
7
+ end
8
+ end
@@ -0,0 +1,56 @@
1
+ require 'sidekiq/restart/command'
2
+
3
+ describe "Sidekiq::Restart::Command" do
4
+ let(:sidekiq) { double "Sidekiq" }
5
+ let(:command) { Sidekiq::Restart::Command.new sidekiq }
6
+
7
+ describe '#run' do
8
+ let(:id) { "my_id" }
9
+
10
+ before do
11
+ allow(sidekiq).to receive(:redis).and_yield(redis)
12
+ end
13
+
14
+ context "theres a worker with id" do
15
+ let(:redis) { instance_double "Redis", get: json, del: nil, srem: nil }
16
+ let(:data) { { 'payload' => msg } }; let(:json) { double }; let(:msg) { double }
17
+
18
+ before do
19
+ class_double("Sidekiq::Client", push: nil).as_stubbed_const
20
+ allow(sidekiq).to receive(:load_json).and_return(data)
21
+ end
22
+
23
+ it 'uses sidekiqs redis instance' do
24
+ expect(sidekiq).to receive(:redis).and_yield(redis)
25
+ command.run id
26
+ end
27
+
28
+ it 'retrieves the job details' do
29
+ expect(redis).to receive(:get).with("worker:my_id")
30
+ expect(sidekiq).to receive(:load_json).with(json)
31
+ command.run id
32
+ end
33
+
34
+ it 'requeues the job' do
35
+ expect(Sidekiq::Client).to receive(:push).with(msg)
36
+ command.run id
37
+ end
38
+
39
+ it 'cleans up the old worker details' do
40
+ expect(redis).to receive(:srem).with("workers", id)
41
+ expect(redis).to receive(:del).with("worker:my_id")
42
+ expect(redis).to receive(:del).with("worker:my_id:started")
43
+ command.run id
44
+ end
45
+ end
46
+
47
+ context "no such worker exists" do
48
+ let(:redis) { instance_double "Redis", get: nil }
49
+
50
+ it 'uses sidekiqs redis instance' do
51
+ expect(sidekiq).to receive(:redis).and_yield(redis)
52
+ command.run id
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,51 @@
1
+ <div class="row header">
2
+ <div class="col-sm-7">
3
+ <h3><%= t('Workers') %></h3>
4
+ </div>
5
+ </div>
6
+
7
+ <table class="workers table table-hover table-bordered table-striped table-white">
8
+ <thead>
9
+ <th><%= t('Worker') %></th>
10
+ <th><%= t('Queue') %></th>
11
+ <th><%= t('Class') %></th>
12
+ <th><%= t('Arguments') %></th>
13
+ <th><%= t('Started') %></th>
14
+ <th><%= t('Controls') %></th>
15
+ </thead>
16
+ <% workers.each_with_index do |(worker, msg), index| %>
17
+ <tr>
18
+ <td><%= worker %></td>
19
+ <td>
20
+ <a href="<%= root_path %>queues/<%= msg['queue'] %>"><%= msg['queue'] %></a>
21
+ </td>
22
+
23
+ <td><%= msg['payload']['class'] %></td>
24
+ <td>
25
+ <% if msg['payload']['args'].to_s.size > 100 %>
26
+ <%= h(msg['payload']['args'].inspect[0..100]) + "... " %>
27
+ <button data-toggle="collapse" data-target="#worker_<%= index %>" class="btn btn-default btn-xs"><%= t('ShowAll') %></button>
28
+ <div class="toggle" id="worker_<%= index %>" style="display: none;max-width: 750px;"><%= h(msg['payload']['args']) %></div>
29
+ <% else %>
30
+ <%= h(msg['payload']['args']) %>
31
+ <% end %>
32
+ </td>
33
+ <td><%= relative_time(msg['run_at'].is_a?(Numeric) ? Time.at(msg['run_at']) : Time.parse(msg['run_at'])) %></td>
34
+ <td>
35
+ <form action="<%= root_path %>workers/<%= worker %>" method="post">
36
+ <input name="_method" type="hidden" value="delete" />
37
+ <button class="btn btn-primary btn-block" type="submit">Restart</button>
38
+ </form>
39
+ </td>
40
+ </tr>
41
+ <% end %>
42
+ </table>
43
+ <% if workers_size > 0 %>
44
+ <div class="row">
45
+ <div class="col-sm-2 pull-right">
46
+ <form action="<%= root_path %>reset" method="post">
47
+ <button class="btn btn-primary btn-block" type="submit"><%= t('ClearWorkerList') %></button>
48
+ </form>
49
+ </div>
50
+ </div>
51
+ <% end %>
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-restart
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jon Rowe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sidekiq
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.17.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.17.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sinatra
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 3.0.0.beta1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 3.0.0.beta1
97
+ description: Allows manual restarts of Sidekiq workers.
98
+ email:
99
+ - hello@jonrowe.co.uk
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/sidekiq-restart.rb
111
+ - lib/sidekiq/restart.rb
112
+ - lib/sidekiq/restart/command.rb
113
+ - lib/sidekiq/restart/version.rb
114
+ - lib/sidekiq/restart/web_extension.rb
115
+ - sidekiq-restart.gemspec
116
+ - spec/integration/sidekiq_web_integration_spec.rb
117
+ - spec/spec_helper.rb
118
+ - spec/unit/sidekiq/restart/command_spec.rb
119
+ - web/views/index.erb
120
+ homepage: https://github.com/JonRowe/sidekiq-restart/
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.2.0
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Allows manual restarts of Sidekiq workers via the Web UI.
144
+ test_files:
145
+ - spec/integration/sidekiq_web_integration_spec.rb
146
+ - spec/spec_helper.rb
147
+ - spec/unit/sidekiq/restart/command_spec.rb