resque-disable-job 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: acc1e95ce6c23efb0f9a1f6e4f83dbc4c791ff89
4
+ data.tar.gz: 1058c2273ec04f487a0f89a44f6cd82aa4fa95ac
5
+ SHA512:
6
+ metadata.gz: fe0543e6cecac2551a46e9b9634c47672cfc74617626ab3cf0365bc3b263f0c22d0c4033e9b5ef6940afd657f17e88b193abacb1bc359963551dbf541b80f81a
7
+ data.tar.gz: bf8a921179b2f961879bab400f1a2ccde087a9233cf266ded058ffa968f0fec50164fbbfebe15ffd470cd6efe2519118df86a641869cd3c9b9515efd4b74a1c9
@@ -0,0 +1,15 @@
1
+ # Created by .ignore support plugin (hsz.mobi)
2
+ ### Ruby template
3
+ /coverage/
4
+ /tmp/
5
+
6
+ # Used by dotenv library to load environment variables.
7
+ # .env
8
+
9
+ ## Documentation cache and generated files:
10
+ ## Environment normalization:
11
+ # for a library or gem, you might want to ignore these files since the code is
12
+ # intended to run in multiple environments; otherwise, check them in:
13
+ Gemfile.lock
14
+ .ruby-version
15
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
@@ -0,0 +1,67 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ DisplayCopNames: true
5
+ CacheRootDirectory: tmp
6
+
7
+ # Configured cops
8
+ Layout/AlignParameters:
9
+ EnforcedStyle: with_fixed_indentation
10
+ Layout/CaseIndentation:
11
+ EnforcedStyle: end
12
+ Layout/FirstParameterIndentation:
13
+ EnforcedStyle: consistent
14
+ Layout/IndentArray:
15
+ EnforcedStyle: consistent
16
+ Layout/IndentHash:
17
+ EnforcedStyle: consistent
18
+ Layout/MultilineMethodCallIndentation:
19
+ EnforcedStyle: indented
20
+ Layout/MultilineOperationIndentation:
21
+ EnforcedStyle: indented
22
+
23
+ Lint/EndAlignment:
24
+ EnforcedStyleAlignWith: variable
25
+
26
+ Style/EmptyMethod:
27
+ EnforcedStyle: expanded
28
+ Style/FormatString:
29
+ EnforcedStyle: percent
30
+ Style/ClassAndModuleChildren:
31
+ Exclude:
32
+ - 'spec/**/*'
33
+
34
+ # Disabled cops
35
+
36
+ Lint/AssignmentInCondition:
37
+ Enabled: false
38
+
39
+ Metrics:
40
+ Enabled: false
41
+
42
+ Style/BlockDelimiters:
43
+ Enabled: false
44
+ Style/BracesAroundHashParameters:
45
+ Enabled: false
46
+ Style/ClassVars:
47
+ Enabled: false
48
+ Style/EmptyElse:
49
+ Enabled: false
50
+ Style/EmptyLiteral:
51
+ Enabled: false
52
+ Style/GuardClause:
53
+ Enabled: false
54
+ Style/IfUnlessModifier:
55
+ Enabled: false
56
+ Style/Lambda:
57
+ Enabled: false
58
+ Style/NumericLiterals:
59
+ Enabled: false
60
+ Style/SafeNavigation:
61
+ Enabled: false
62
+ Style/SymbolArray:
63
+ Enabled: false
64
+ Style/WordArray:
65
+ Enabled: false
66
+ Style/MutableConstant:
67
+ Enabled: false
@@ -0,0 +1,14 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2017-11-09 09:31:15 +0100 using RuboCop version 0.51.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ # Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
11
+ # AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
12
+ Naming/FileName:
13
+ Exclude:
14
+ - 'lib/resque-disable-job.rb'
@@ -0,0 +1,25 @@
1
+ language: ruby
2
+
3
+ cache: bundler
4
+
5
+ branches:
6
+ only: master
7
+
8
+ rvm:
9
+ - 2.2.4
10
+ - 2.2.7
11
+ - 2.3.4
12
+ - 2.4.2
13
+ env:
14
+ - COMMAND=rake
15
+ matrix:
16
+ include:
17
+ - rvm: 2.4.2
18
+ env: COMMAND="rubocop -d"
19
+
20
+ services:
21
+ - redis-server
22
+
23
+ script:
24
+ - bundle exec rubocop -V
25
+ - bundle exec $COMMAND
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at abalcanasu@zendesk.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in resque-disable-job.gemspec
8
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,106 @@
1
+ # Resque::Disable::Job
2
+
3
+ [![Build Status](https://travis-ci.org/zendesk/resque-disable-job.svg?branch=master)](https://travis-ci.org/zendesk/resque-disable-job)
4
+ [![codecov](https://codecov.io/gh/zendesk/resque-disable-job/branch/master/graph/badge.svg)](https://codecov.io/gh/zendesk/resque-disable-job)
5
+
6
+
7
+ This is a Resque plugin that allows us to disable jobs from being processed, by using the job class name and arguments.
8
+ It uses some Redis data structures to keep a record of what jobs need to be disabled and how many jobs were disabled for that rule.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'resque-disable-job'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install resque-disable-job
25
+
26
+ ## Usage
27
+
28
+ ### Job
29
+ You can add it to your job like any other Resque plugin:
30
+
31
+ ```ruby
32
+ class TestJob
33
+ extend Resque::Plugins::DisableJob
34
+ @queue = :test
35
+
36
+ def self.perform(some_id, other_id)
37
+ end
38
+ end
39
+ ```
40
+ The plugin will add the `before_perform_allow_disable_job` Resque hook. This will check if the current job needs to be stopped and it calls the `disable_job_handler` method.
41
+ By default this will just raise `Resque::Job::DontPerform`. If you want to do more you can override it in your job or base class.
42
+
43
+ ### Disabling a Job
44
+
45
+ In your application's console you can use the `Resque::Plugins::DisableJob.disable_job` method to disable a job.
46
+
47
+ `Resque::Plugins::DisableJob.disable_job(job_name, matching_arguments, ttl = 3600)`
48
+
49
+ Note: By default, each rule has a ttl of 1 hour. This is because disabling a job should be a temporary action.
50
+
51
+ ```ruby
52
+ # Disable all the jobs of that class:
53
+ TestJob.disable
54
+ # Disable all TestJob jobs with the first argument `65`
55
+ TestJob.disable([65])
56
+ # Disable all SampleJob jobs that have the argument a == 5
57
+ SampleJob.disable({a: 5})
58
+
59
+ # Disable a job for one hour
60
+ SampleJob.disable({a: 1}, 3600)
61
+
62
+ # Re-enable jobs:
63
+ TestJob.enable()
64
+ TestJob.enable([65])
65
+
66
+ # Simple kill-switch to remove all the rules for the job
67
+ TestJob.enable_all
68
+
69
+ # Kill-switch to remove all the jobs and their rules
70
+ Resque::Plugins::DisableJob::Job.enable_all!
71
+ ```
72
+
73
+ **Note**: You can disable many arguments for one job type, but for performance reasons we look at only 10 rules.
74
+
75
+ ### Operations
76
+
77
+ `Resque::Plugins::DisableJob::Stats` comes with a a few methods that will help you keep track of actively disabled jobs and how many times the rule was matched.
78
+
79
+ * `all_disabled_jobs` - returns a hash of all the disabled jobs and their rules
80
+ * `job_disabled_rules(job_name)` - returns a hash of all the rules for one particular job
81
+ * `disabled_stats` - returns an array of all the disabled jobs, their rules, and the counter of how many times it was matched
82
+
83
+ ## Development
84
+
85
+ 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.
86
+
87
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/zendesk/resque-disable-job. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
92
+
93
+ ## Code of Conduct
94
+
95
+ Everyone interacting in the Resque::Disable::Job project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/zendesk/resque-disable-job/blob/master/CODE_OF_CONDUCT.md).
96
+
97
+ ## Copyright and license
98
+
99
+ Copyright 2017 Zendesk, Inc.
100
+
101
+ Licensed under the [Apache License, Version 2.0](https://opensource.org/licenses/Apache-2.0) (the "License"); you may not use this file except in compliance with the License.
102
+
103
+ You may obtain a copy of the License at
104
+ http://www.apache.org/licenses/LICENSE-2.0
105
+
106
+ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+ require 'rubocop/rake_task'
6
+
7
+ RuboCop::RakeTask.new
8
+
9
+ Rake::TestTask.new(:spec) do |test|
10
+ test.pattern = 'spec/**/*_spec.rb'
11
+ test.verbose = true
12
+ test.warning = false
13
+ end
14
+
15
+ task default: :spec
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'resque/disable_job/version'
4
+ require 'resque/plugins/disable_job/rule'
5
+ require 'resque/plugins/disable_job/job'
6
+ require 'resque/plugins/disable_job/stats'
7
+ require 'resque/plugins/disable_job'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Resque
4
+ module DisableJob
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'resque/plugins/disable_job/job'
4
+ require 'resque/plugins/disable_job/rule'
5
+
6
+ module Resque
7
+ module Plugins
8
+ # DisableJob
9
+ #
10
+ # This class handles the main logic of the DisableJob plugin.
11
+ # We can configure a job to be allowed to be disabled, set a job to be disabled or enable a job, and
12
+ # we can see the status of the currently disabled jobs.
13
+ module DisableJob
14
+ MAX_JOB_RULES = 10
15
+ DEFAULT_TIMEOUT = 3600 # seconds
16
+
17
+ def before_perform_allow_disable_job(*args)
18
+ if Job.disabled?(name, args)
19
+ disable_job_handler("Skipped running job #{name}(#{args})", args)
20
+ end
21
+ end
22
+
23
+ # Override this if you want custom processing
24
+ def disable_job_handler(message, *_args)
25
+ raise Resque::Job::DontPerform, message
26
+ end
27
+
28
+ def disable(specific_args = [], timeout = DEFAULT_TIMEOUT)
29
+ Job.disable_job(name, specific_args: specific_args, timeout: timeout)
30
+ end
31
+
32
+ def enable(specific_args = [])
33
+ Job.enable_job(name, specific_args: specific_args)
34
+ end
35
+
36
+ def enable_all
37
+ Job.enable_all(name)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'rule'
4
+
5
+ module Resque
6
+ module Plugins
7
+ module DisableJob
8
+ # The Job class contains the logic that determines if the current job is disabled,
9
+ # and methods to disable and enable a specific job.
10
+ class Job
11
+ # disabled? checks if the job and it's arguments is disabled
12
+ def self.disabled?(job_name, job_args)
13
+ # We get all the rules for the current job
14
+ rules = get_all_rules(job_name)
15
+ # We limit this to 10 rules for performance reasons. Each check delays the job from being performed
16
+ matched_rule = match_rules(job_name, job_args, rules)
17
+
18
+ if !matched_rule.nil?
19
+ # if we found a matched rule, we record this and return true
20
+ record_matched_rule(job_name, job_args, matched_rule)
21
+ true
22
+ else
23
+ false
24
+ end
25
+ end
26
+
27
+ def self.match_rules(job_name, job_args, rules)
28
+ rules.take(MAX_JOB_RULES).detect do |specific_rule|
29
+ begin
30
+ # if the rule is not expired
31
+ if !expired?(specific_rule)
32
+ # if the arguments received and the ones from the rule ar of the same type, we can check for a match
33
+ # if they match, that means that we need to disable the current job
34
+ can_check_rule?(job_name, job_args, specific_rule) ? args_match(job_args, specific_rule.arguments) : false
35
+ else
36
+ # we remove the rule if it's expired
37
+ remove_specific_rule(specific_rule)
38
+ false
39
+ end
40
+ rescue StandardError => e
41
+ Resque.logger.error "Failed to parse AllowDisableJob rules for #{job_name}: #{specific_rule.serialized_arguments}. Error: #{e.message}"
42
+ false
43
+ end
44
+ end
45
+ end
46
+
47
+ # The rule is expired if the TTL of the rule key is -1.
48
+ def self.expired?(rule)
49
+ Resque.redis.ttl(rule.rule_key) < 0 # .negative? only works in Ruby 2.3 and above
50
+ end
51
+
52
+ # To disable a job we need to add it in 3 data structures:
53
+ # - we need to add the job name to the main set so we know what jobs have rules
54
+ # - we add the arguments to the job's rule hash
55
+ # - we create a counter for the individual rule that will keep track of how many times it was matched
56
+ def self.disable_job(name, specific_args: {}, timeout: DEFAULT_TIMEOUT)
57
+ rule = Rule.new(name, specific_args)
58
+ Resque.redis.multi do
59
+ Resque.redis.sadd rule.main_set, rule.job_name
60
+ Resque.redis.hset(rule.all_rules_key, rule.digest, rule.serialized_arguments)
61
+ Resque.redis.set(rule.rule_key, 0)
62
+ Resque.redis.expire(rule.rule_key, timeout)
63
+ end
64
+ end
65
+
66
+ # To enable a job, we just need to remove it
67
+ def self.enable_job(name, specific_args: {})
68
+ remove_specific_rule(Rule.new(name, specific_args))
69
+ end
70
+
71
+ def self.enable_all(job_name)
72
+ get_all_rules(job_name).map { |r| remove_specific_rule(r) }
73
+ end
74
+
75
+ def self.enable_all!
76
+ disabled_jobs.map { |job_name| enable_all(job_name) }
77
+ end
78
+
79
+ def self.disabled_jobs
80
+ Resque.redis.smembers(Rule::JOBS_SET)
81
+ end
82
+
83
+ # To remove a job we need to delete its counter, the entry from the rules hash and
84
+ # if the job has no more rules, we can remove the job's entry in the main set
85
+ def self.remove_specific_rule(rule)
86
+ Resque.redis.del(rule.rule_key)
87
+ Resque.redis.hdel(rule.all_rules_key, rule.digest)
88
+ if Resque.redis.hlen(rule.all_rules_key).zero?
89
+ Resque.redis.srem(rule.main_set, rule.job_name)
90
+ end
91
+ end
92
+
93
+ # To set the arguments to block we need to keep in mind the parameter order and that
94
+ # if we don't specify anything, that means we are blocking everything.
95
+ # The rule is from generic to specific.
96
+ def self.args_match(args, set_args)
97
+ return true if args == set_args
98
+ should_block = args.to_a.map.with_index do |a, i|
99
+ # We check each parameter (65) or parameters set (["account_id", 65]) in the job_args with the job_args to be blocked
100
+ # if it's nil, then we match, if it's specified, we check for equality (65 == 65 or ["account_id", 65] == ["account_id", 65])
101
+ set_args[i].nil? || a == set_args[i]
102
+ end
103
+ # if all params are matched [reduce(:&)]
104
+ should_block.reduce(:&)
105
+ end
106
+
107
+ # Support functions for disabled?
108
+
109
+ def self.can_check_rule?(job_name, job_args, rule)
110
+ if (rule.arguments.is_a?(Array) && job_args.is_a?(Array)) || (rule.arguments.is_a?(Hash) && job_args.is_a?(Hash))
111
+ true
112
+ else
113
+ Resque.logger.error "TYPE MISMATCH while checking disable rule #{rule.digest} (#{rule.serialized_arguments}) for #{job_name}: \
114
+ job_args is a #{job_args.class} & set_args is a #{rule.arguments.class}"
115
+ false
116
+ end
117
+ end
118
+
119
+ def self.get_specific_rule(job_name, set_args, digest)
120
+ rule = Rule.new(job_name, set_args)
121
+ Resque.logger.error 'The DIGEST does not match' if rule.digest != digest
122
+ rule
123
+ end
124
+
125
+ def self.get_all_rules(job_name)
126
+ Resque.redis.hgetall(Rule.new(job_name).all_rules_key).map do |digest, set_args|
127
+ get_specific_rule(job_name, set_args, digest)
128
+ end
129
+ end
130
+
131
+ def self.record_matched_rule(job_name, job_args, rule)
132
+ Resque.redis.incr rule.rule_key
133
+ Resque.logger.info "Matched running job #{job_name}(#{job_args}) because it was disabled by #{rule}"
134
+ end
135
+
136
+ private_class_method :can_check_rule?, :record_matched_rule, :get_all_rules, :get_specific_rule
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'digest/sha1'
4
+ require 'json'
5
+
6
+ module Resque
7
+ module Plugins
8
+ module DisableJob
9
+ # Rule
10
+ #
11
+ # This class handles the references (aka redis key names) and some logic to interact
12
+ # with the DisableJob rules data structures
13
+ #
14
+ # We use a few Redis structures:
15
+ # * `main_set` - is Redis Set where we store all the jobs that are disabled at one time
16
+ # * `all_rules_key` - is the Redis Hash that stores all the arguments we disable for one job
17
+ # * `rule_key` - a Redis Counter that stores how many times we disabled that job with the specific parameters
18
+ # This key has a TTL equal with the timeout set, so if it's gone we won't disable the job
19
+ # * `serialized_arguments` - the job's specified arguments in JSON format
20
+ # * `arguments` - the job's specified arguments
21
+ # * `digest` - the job's specified arguments as a digest; used to identify the job rules in the key
22
+ class Rule
23
+ JOBS_SET = 'disabled_jobs'
24
+ attr_reader :job_name
25
+
26
+ def initialize(job_name, arguments = [], digest = '')
27
+ @job_name = job_name
28
+ if arguments.is_a?(Enumerable)
29
+ @arguments = arguments
30
+ else
31
+ @serialized_args = arguments
32
+ end
33
+ @rule_digest = digest unless digest.empty?
34
+ end
35
+
36
+ def main_set
37
+ JOBS_SET
38
+ end
39
+
40
+ def all_rules_key
41
+ @all_rules_key ||= main_set + ':' + @job_name
42
+ end
43
+
44
+ def rule_key
45
+ @rule_key ||= all_rules_key + ':' + digest
46
+ end
47
+
48
+ def serialized_arguments
49
+ @serialized_args ||= @arguments.to_json
50
+ end
51
+
52
+ def arguments
53
+ @arguments ||= JSON.parse(@serialized_args)
54
+ end
55
+
56
+ def digest
57
+ @rule_digest ||= Digest::SHA1.hexdigest(serialized_arguments)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'rule'
4
+
5
+ module Resque
6
+ module Plugins
7
+ module DisableJob
8
+ # Stats
9
+ # These are methods that inspect the rules
10
+ class Stats
11
+ def self.all_disabled_jobs
12
+ Hash[Job.disabled_jobs.map { |name| [name, job_disabled_rules(name)] }]
13
+ end
14
+
15
+ def self.job_disabled_rules(name)
16
+ Resque.redis.hgetall(Rule.new(name).all_rules_key)
17
+ end
18
+
19
+ def self.disabled_stats
20
+ counts = all_disabled_jobs.map do |name, rules|
21
+ rules.map do |digest, arguments|
22
+ {
23
+ job_name: name,
24
+ digest: digest,
25
+ arguments: arguments,
26
+ count: Resque.redis.get(Rule.new(name, arguments, digest).rule_key)
27
+ }
28
+ end
29
+ end
30
+ counts.flatten
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'resque/disable_job/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'resque-disable-job'
9
+ spec.version = Resque::DisableJob::VERSION
10
+ spec.authors = ['Andrei Balcanasu']
11
+ spec.email = ['abalcanasu@zendesk.com']
12
+
13
+ spec.summary = 'Resque plugin that can disable jobs from being processed.'
14
+ spec.description = 'This is a Resque plugin that allows us to disable jobs from being processed, by using the job class name and arguments.
15
+ It uses some Redis data structures to keep a record of what jobs need to be disabled and how many jobs were disabled for that rule.'
16
+ spec.homepage = 'https://github.com/zendesk/resque-disable-job'
17
+ spec.license = 'Apache-2.0'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features|docs)/})
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_runtime_dependency 'resque', '~> 1.25'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.16'
29
+ spec.add_development_dependency 'codecov', '~> 0'
30
+ spec.add_development_dependency 'minitest'
31
+ spec.add_development_dependency 'minitest-rg', '~> 0'
32
+ spec.add_development_dependency 'mocha', '~> 0'
33
+ spec.add_development_dependency 'pry', '~> 0'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rubocop', '0.51.0'
36
+ spec.add_development_dependency 'simplecov', '~> 0'
37
+ end
metadata ADDED
@@ -0,0 +1,202 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-disable-job
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrei Balcanasu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: resque
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.25'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.25'
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.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: codecov
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: minitest
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: minitest-rg
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: mocha
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
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '10.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '10.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '='
130
+ - !ruby/object:Gem::Version
131
+ version: 0.51.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '='
137
+ - !ruby/object:Gem::Version
138
+ version: 0.51.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: |-
154
+ This is a Resque plugin that allows us to disable jobs from being processed, by using the job class name and arguments.
155
+ It uses some Redis data structures to keep a record of what jobs need to be disabled and how many jobs were disabled for that rule.
156
+ email:
157
+ - abalcanasu@zendesk.com
158
+ executables: []
159
+ extensions: []
160
+ extra_rdoc_files: []
161
+ files:
162
+ - ".gitignore"
163
+ - ".rubocop.yml"
164
+ - ".rubocop_todo.yml"
165
+ - ".travis.yml"
166
+ - CODE_OF_CONDUCT.md
167
+ - Gemfile
168
+ - LICENSE
169
+ - README.md
170
+ - Rakefile
171
+ - lib/resque-disable-job.rb
172
+ - lib/resque/disable_job/version.rb
173
+ - lib/resque/plugins/disable_job.rb
174
+ - lib/resque/plugins/disable_job/job.rb
175
+ - lib/resque/plugins/disable_job/rule.rb
176
+ - lib/resque/plugins/disable_job/stats.rb
177
+ - resque-disable-job.gemspec
178
+ homepage: https://github.com/zendesk/resque-disable-job
179
+ licenses:
180
+ - Apache-2.0
181
+ metadata: {}
182
+ post_install_message:
183
+ rdoc_options: []
184
+ require_paths:
185
+ - lib
186
+ required_ruby_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - ">="
189
+ - !ruby/object:Gem::Version
190
+ version: '0'
191
+ required_rubygems_version: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ requirements: []
197
+ rubyforge_project:
198
+ rubygems_version: 2.4.5.2
199
+ signing_key:
200
+ specification_version: 4
201
+ summary: Resque plugin that can disable jobs from being processed.
202
+ test_files: []