sidekiq_job_controller 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cebc12dc71aff628f1242f93184eb2d152c1d2760a24b4b8d5b113f6879d4a7e
4
+ data.tar.gz: 40414d5a91649fc946f26a4b5b42f156879a11f164c58d1eed2b5923289b2151
5
+ SHA512:
6
+ metadata.gz: 5eb45047c7108613ff1e4cadbabb7b8cec81d838d67b6f33a45d1dd9c1be543104a8c7c5fb75690258089c74101b3f9cd2f5d1e38b59810fcdcbe25760a402ee
7
+ data.tar.gz: '0190b71cab0db175b24c9ce95a780435d0b9f36052380283efa4d77abd7bbb1872db93ae1091bb5fd9c96f0d959eefb56f760245aded088e05fc90f8098120bc'
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 2.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.1.0] - 2024-04-12
2
+
3
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement as an Issue in this repository. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Circle.so
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # SidekiqJobController
2
+
3
+ I bet there have been multiple occasions where an specific Job failures go out of control (by an introduced bug, or a broken dependency, external service or whatever) and you didn't have the ability to simply stop processing jobs for that job class or postpone their execution.
4
+
5
+ Sidekiq has a nice retry mechanism, but it is also too aggressive at first, every job wants to be retried very quickly, and sometimes you need to stop the job from being executed for a while.
6
+
7
+ Usually, the solution was to create a new Sidekiq queue in `sidekiq.yml`, deploy the change, which would give the ability to stop it, clear it, or whatever.
8
+ Or edit the job class to early return or avoid the error until you figure out what's going on.
9
+
10
+ That can work, but it can be slow, since you need to deploy the change to your infrastructure.
11
+
12
+ If you need something you can activate quickly, this Sidekiq Middleware is what you are looking for.
13
+
14
+ This middleware works for both Sidekiq workers and ActiveJob jobs.
15
+
16
+ ## Installation
17
+
18
+ `bundle add sidekiq_job_controller`
19
+
20
+ In your `config/initializers/sidekiq.rb`:
21
+
22
+ ```ruby
23
+ config.server_middleware do |chain|
24
+ chain.add SidekiqJobController::ServerMiddleware
25
+ end
26
+ ````
27
+
28
+ ## Usage
29
+
30
+ In brief, we are adding a Sidekiq middleware that checks if a job/worker class has to be skipped all together, or re-queued for later execution.
31
+
32
+ This is useful to run in the console when a job is failing too much (or stressing the database too much), we can skip the execution or delay it for later.
33
+
34
+ **Example**: There’s a bug in production that causes `SomeJob` to fail many times per minute, causing a lot of errors in your error reporting tool and/or stressing your db.
35
+
36
+ You would do:
37
+
38
+ ```ruby
39
+ SidekiqJobController::Controller.new(class_name: SomeJob).requeue_job!(45.minutes)
40
+ ```
41
+
42
+ This will push every single `SomeJob` that Sidekiq wants to perform to be executed in 45 minutes instead of now.
43
+
44
+ Then you have 45 minutes to fix and deploy the changes.
45
+ If you take more than 45 minutes the jobs will be rescheduled for 45 more minutes, no problem.
46
+
47
+ Once you fix the problem and it’s in production, **`MAKE SURE`** to return the Job to its normal state:
48
+
49
+ ```ruby
50
+ SidekiqJobController::Controller.new(class_name: SomeJob).resume_job!
51
+ ```
52
+
53
+ If for any reason you need or prefer to just skip (discard, drop) the jobs execution (equivalent to return nil in the `perform`’s first line of code) you would need to run:
54
+
55
+ ```ruby
56
+ SidekiqJobController::Controller.new(class_name: SomeJob).skip_job!
57
+ ```
58
+
59
+ You can always check the status of a job using:
60
+
61
+ ```ruby
62
+ SidekiqJobController::Controller.new(class_name: SomeJob).status
63
+ ```
64
+
65
+ ## Development
66
+
67
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
68
+
69
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
70
+
71
+ ## Contributing
72
+
73
+ Bug reports and pull requests are welcome on GitHub at https://github.com/circleco/sidekiq_job_controller. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/circleco/sidekiq_job_controller/blob/main/CODE_OF_CONDUCT.md).
74
+
75
+ ## License
76
+
77
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
78
+
79
+ ## Code of Conduct
80
+
81
+ Everyone interacting in the SidekiqJobController project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/circleco/sidekiq_job_controller/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,57 @@
1
+ module SidekiqJobController
2
+ class Controller
3
+ attr_reader :class_name, :job, :payload
4
+
5
+ def initialize(class_name:, job: nil, payload: nil)
6
+ @class_name = class_name
7
+ @job = job
8
+ @payload = payload
9
+ end
10
+
11
+ def resume_job!
12
+ Sidekiq.redis do |conn|
13
+ conn.del(job_controller_key)
14
+ end
15
+ status
16
+ end
17
+
18
+ def skip_job!
19
+ Sidekiq.redis do |conn|
20
+ conn.mapped_hmset(job_controller_key, { action: :skip })
21
+ end
22
+ status
23
+ end
24
+
25
+ def requeue_job!(seconds)
26
+ Sidekiq.redis do |conn|
27
+ conn.mapped_hmset(job_controller_key, { action: :requeue, in: seconds.to_i })
28
+ end
29
+ status
30
+ end
31
+
32
+ def status
33
+ "#{class_name} jobs are #{current_state}"
34
+ end
35
+
36
+ def fetch_execution_action
37
+ Sidekiq.redis do |conn|
38
+ conn.hgetall(job_controller_key)
39
+ end
40
+ end
41
+
42
+ private
43
+ def job_controller_key
44
+ "sidekiq:job_controller:#{class_name}"
45
+ end
46
+
47
+ def current_state
48
+ controller_result = fetch_execution_action
49
+
50
+ return "running" unless controller_result.present?
51
+ return "skipped" if controller_result["action"] == "skip"
52
+ return "requeued in #{controller_result["in"].to_i} seconds" if controller_result["action"] == "requeue"
53
+
54
+ "in unknown state: #{controller_result["action"]}"
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqJobController
4
+ class ServerMiddleware
5
+ def call(job, payload, _queue)
6
+ class_name = payload["wrapped"] || payload["class"]
7
+
8
+ controller_result = SidekiqJobController::Controller.new(
9
+ class_name: class_name, job: job, payload: payload
10
+ ).fetch_execution_action
11
+
12
+ if controller_result.blank?
13
+ yield
14
+ elsif controller_result["action"] == "skip"
15
+ log("FORCED SKIP ACTIVATED: Skipped performing job #{class_name} - #{job.jid}")
16
+ elsif controller_result["action"] == "requeue"
17
+ log("FORCED REQUEUE ACTIVATED: Requeuing job in #{controller_result["in"].to_i} seconds - #{class_name} - #{job.jid}")
18
+ Sidekiq::Client.push(payload.merge("at" => Time.now.to_i + controller_result["in"].to_i))
19
+ else
20
+ log("UNEXPECTED ACTION: #{controller_result["action"]} is not recognized in #{class_name} - #{job.jid}")
21
+ yield
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def log(message)
28
+ if defined?(Rails)
29
+ Rails.logger.info message
30
+ else
31
+ puts message
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqJobController
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sidekiq"
4
+ require "active_job"
5
+
6
+ module SidekiqJobController
7
+ end
8
+
9
+ require "sidekiq_job_controller/controller"
10
+ require "sidekiq_job_controller/server_middleware"
11
+
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/sidekiq_job_controller/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "sidekiq_job_controller"
7
+ spec.version = SidekiqJobController::VERSION
8
+ spec.authors = ["Benjamín Silva"]
9
+
10
+ spec.summary = "Sidekiq middleware to manually skip or delay Sidekiq jobs."
11
+ spec.description = "Sidekiq middleware that allows for immediate control over job processing in Sidekiq, providing tools to skip, delay, or modify job executions directly from the console, ideal for handling job failures or system stress without needing deployment."
12
+ spec.homepage = "https://github.com/circleco/sidekiq_job_controller"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/circleco/sidekiq_job_controller"
20
+ spec.metadata["changelog_uri"] = "https://github.com/circleco/sidekiq_job_controller/blob/main/CHANGELOG.md"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (File.expand_path(f) == __FILE__) ||
27
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Gem dependencies
35
+ spec.add_runtime_dependency "sidekiq", "~> 6.0"
36
+ spec.add_runtime_dependency "activejob", "~> 6.0"
37
+
38
+ # Development dependencies
39
+ spec.add_development_dependency "rspec"
40
+ spec.add_development_dependency "standard"
41
+ spec.add_development_dependency "activesupport"
42
+ spec.add_development_dependency "fakeredis"
43
+
44
+ # For more information and examples about making a new gem, check out our
45
+ # guide at: https://bundler.io/guides/creating_gem.html
46
+ end
@@ -0,0 +1,4 @@
1
+ module SidekiqJobController
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq_job_controller
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Benjamín Silva
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-04-15 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: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activejob
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '6.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '6.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: standard
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: activesupport
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: fakeredis
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Sidekiq middleware that allows for immediate control over job processing
98
+ in Sidekiq, providing tools to skip, delay, or modify job executions directly from
99
+ the console, ideal for handling job failures or system stress without needing deployment.
100
+ email:
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".rspec"
106
+ - ".standard.yml"
107
+ - CHANGELOG.md
108
+ - CODE_OF_CONDUCT.md
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - lib/sidekiq_job_controller.rb
113
+ - lib/sidekiq_job_controller/controller.rb
114
+ - lib/sidekiq_job_controller/server_middleware.rb
115
+ - lib/sidekiq_job_controller/version.rb
116
+ - sidekiq_job_controller.gemspec
117
+ - sig/sidekiq_job_controller.rbs
118
+ homepage: https://github.com/circleco/sidekiq_job_controller
119
+ licenses:
120
+ - MIT
121
+ metadata:
122
+ allowed_push_host: https://rubygems.org
123
+ homepage_uri: https://github.com/circleco/sidekiq_job_controller
124
+ source_code_uri: https://github.com/circleco/sidekiq_job_controller
125
+ changelog_uri: https://github.com/circleco/sidekiq_job_controller/blob/main/CHANGELOG.md
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: 2.6.0
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubygems_version: 3.5.3
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Sidekiq middleware to manually skip or delay Sidekiq jobs.
145
+ test_files: []