sidekiq_spread 0.1.0

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
+ SHA256:
3
+ metadata.gz: 955eb4626e41453e40bf378c618075a33370e9add9cffec76e3776fa5d18ed30
4
+ data.tar.gz: 7d0fad80c125ee42df6fe474d96ac56c02efaa9cfdc6a77852a9b830212b568a
5
+ SHA512:
6
+ metadata.gz: ecfbec88cf2d1ea7d79955688626e2c32e4380e82dc5c27dffff058202e7c09aa7ca0fb619fcb0f8ecf2a24388d86950d6fe86cbfe3abfedc578b6ad4a78894f
7
+ data.tar.gz: 74387f8bf142915ddfd58bd95530e4b64e4cbf5c983ebc265a597918d2f73a356511b67e1f9e2da9b8bdd4740df777fd0f6082477cf2438e1d3e9dd2cbde8462
@@ -0,0 +1,145 @@
1
+ # Ruby CircleCI 2.0 configuration file
2
+ #
3
+ # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
+ #
5
+ version: 2
6
+
7
+ spec_shared: &spec_shared
8
+ working_directory: ~/sidekiq_spread
9
+ steps:
10
+ - checkout
11
+
12
+ - run:
13
+ name: install dependencies
14
+ command: |
15
+ bundle check --path vendor/bundle || bundle install --jobs=4 --retry=3 --path vendor/bundle
16
+
17
+ - run:
18
+ name: Make test-results
19
+ command: |
20
+ mkdir /tmp/test-results
21
+
22
+ - run:
23
+ name: Run basic specs
24
+ command: |
25
+ bundle exec rspec --format progress \
26
+ --format RspecJunitFormatter \
27
+ --out /tmp/test-results/rspec-basic.xml \
28
+ --format progress \
29
+ spec
30
+
31
+ # collect reports
32
+ - store_test_results:
33
+ path: /tmp/test-results
34
+ - store_artifacts:
35
+ path: /tmp/test-results
36
+ destination: test-results
37
+
38
+ jobs:
39
+ deps:
40
+ docker:
41
+ - image: circleci/ruby:2.5
42
+ working_directory: ~/sidekiq_spread
43
+
44
+ steps:
45
+ - checkout
46
+
47
+ # Download and cache dependencies
48
+ - restore_cache:
49
+ keys:
50
+ - v1-dependencies-{{ checksum "sidekiq_spread.gemspec" }}
51
+
52
+ - run:
53
+ name: install dependencies
54
+ command: |
55
+ bundle check --path vendor/bundle || bundle install --jobs=4 --retry=3 --path vendor/bundle
56
+
57
+ - save_cache:
58
+ paths:
59
+ - vendor/bundle
60
+ key: v1-dependencies-{{ checksum "sidekiq_spread.gemspec" }}
61
+
62
+ - run:
63
+ name: dependencies security audit
64
+ command: |
65
+ bundle exec bundle-audit check --update
66
+
67
+ - run:
68
+ name: Rubocop
69
+ command: bundle exec rubocop
70
+
71
+ ruby-2.5:
72
+ <<: *spec_shared
73
+ docker:
74
+ - image: circleci/ruby:2.5
75
+
76
+ ruby-2.4:
77
+ <<: *spec_shared
78
+ docker:
79
+ - image: circleci/ruby:2.4
80
+
81
+ ruby-2.3:
82
+ <<: *spec_shared
83
+ docker:
84
+ - image: circleci/ruby:2.3
85
+
86
+ ruby-2.2:
87
+ <<: *spec_shared
88
+ docker:
89
+ - image: circleci/ruby:2.2
90
+
91
+ generate_docs:
92
+ docker:
93
+ - image: circleci/ruby:2.5
94
+ working_directory: ~/sidekiq_spread
95
+
96
+ steps:
97
+ - checkout
98
+
99
+ - restore_cache:
100
+ keys:
101
+ - v1-dependencies-{{ checksum "sidekiq_spread.gemspec" }}
102
+
103
+ - run:
104
+ name: Setup bundler path
105
+ command: |
106
+ bundle check --path vendor/bundle
107
+
108
+ - run:
109
+ name: Generate Yard docs
110
+ command: |
111
+ bundle exec yard --output-dir /tmp/workspace/docs/yard
112
+
113
+ - run:
114
+ name: Num Docs
115
+ command: |
116
+ ls -al /tmp/workspace/docs/yard | wc -l
117
+
118
+ - persist_to_workspace:
119
+ root: /tmp/workspace/docs
120
+ paths:
121
+ - yard
122
+
123
+ workflows:
124
+ version: 2
125
+ build:
126
+ jobs:
127
+ - deps
128
+ - ruby-2.5:
129
+ requires:
130
+ - deps
131
+ - ruby-2.4:
132
+ requires:
133
+ - deps
134
+ - ruby-2.3:
135
+ requires:
136
+ - deps
137
+ - ruby-2.2:
138
+ requires:
139
+ - deps
140
+ - generate_docs:
141
+ requires:
142
+ - ruby-2.5
143
+ - ruby-2.4
144
+ - ruby-2.3
145
+ - ruby-2.2
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,15 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5.1
3
+
4
+ Metrics:
5
+ Enabled: false
6
+
7
+ Metrics/LineLength:
8
+ Max: 99
9
+ Exclude:
10
+ - "spec/**/*"
11
+ - "*.gemspec"
12
+
13
+ # to support Ruby 2.2
14
+ Style/NumericPredicate:
15
+ Enabled: false
@@ -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 les.fletcher@gmail.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 sidekiq_spread.gemspec
8
+ gemspec
@@ -0,0 +1,89 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sidekiq_spread (0.1.0)
5
+ sidekiq
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (5.2.1)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 0.7, < 2)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ ast (2.4.0)
16
+ bundler-audit (0.6.0)
17
+ bundler (~> 1.2)
18
+ thor (~> 0.18)
19
+ concurrent-ruby (1.1.3)
20
+ connection_pool (2.2.2)
21
+ diff-lcs (1.3)
22
+ fakeredis (0.7.0)
23
+ redis (>= 3.2, < 5.0)
24
+ i18n (1.1.1)
25
+ concurrent-ruby (~> 1.0)
26
+ jaro_winkler (1.5.1)
27
+ minitest (5.11.3)
28
+ parallel (1.12.1)
29
+ parser (2.5.3.0)
30
+ ast (~> 2.4.0)
31
+ powerpack (0.1.2)
32
+ rack (2.0.6)
33
+ rack-protection (2.0.4)
34
+ rack
35
+ rainbow (3.0.0)
36
+ rake (10.5.0)
37
+ redis (4.0.3)
38
+ rspec (3.8.0)
39
+ rspec-core (~> 3.8.0)
40
+ rspec-expectations (~> 3.8.0)
41
+ rspec-mocks (~> 3.8.0)
42
+ rspec-core (3.8.0)
43
+ rspec-support (~> 3.8.0)
44
+ rspec-expectations (3.8.2)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.8.0)
47
+ rspec-mocks (3.8.0)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.8.0)
50
+ rspec-support (3.8.0)
51
+ rspec_junit_formatter (0.4.1)
52
+ rspec-core (>= 2, < 4, != 2.12.0)
53
+ rubocop (0.60.0)
54
+ jaro_winkler (~> 1.5.1)
55
+ parallel (~> 1.10)
56
+ parser (>= 2.5, != 2.5.1.1)
57
+ powerpack (~> 0.1)
58
+ rainbow (>= 2.2.2, < 4.0)
59
+ ruby-progressbar (~> 1.7)
60
+ unicode-display_width (~> 1.4.0)
61
+ ruby-progressbar (1.10.0)
62
+ sidekiq (5.2.3)
63
+ connection_pool (~> 2.2, >= 2.2.2)
64
+ rack-protection (>= 1.5.0)
65
+ redis (>= 3.3.5, < 5)
66
+ thor (0.20.0)
67
+ thread_safe (0.3.6)
68
+ tzinfo (1.2.5)
69
+ thread_safe (~> 0.1)
70
+ unicode-display_width (1.4.0)
71
+ yard (0.9.16)
72
+
73
+ PLATFORMS
74
+ ruby
75
+
76
+ DEPENDENCIES
77
+ activesupport (> 3.0)
78
+ bundler (~> 1.16)
79
+ bundler-audit
80
+ fakeredis
81
+ rake (~> 10.0)
82
+ rspec (~> 3.0)
83
+ rspec_junit_formatter (~> 0.2)
84
+ rubocop (= 0.60.0)
85
+ sidekiq_spread!
86
+ yard
87
+
88
+ BUNDLED WITH
89
+ 1.17.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Les Fletcher
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.
@@ -0,0 +1,122 @@
1
+ # SidekiqSpread
2
+
3
+ Add 'perform_spread' method to workers to allow for scheduling jobs over an interval of time
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sidekiq_spread'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sidekiq_spread
20
+
21
+ ## Usage
22
+
23
+ You can find the docs on [rubdoc.info](https://www.rubydoc.info/github/Latermedia/sidekiq_spread/master).
24
+
25
+ ### Basic usage
26
+
27
+ This will randomly scheduled a job sometime with in the next hour.
28
+
29
+ ```ruby
30
+ class MyWorker
31
+ include Sidekiq::Worker
32
+ include SidekiqSpread
33
+
34
+ def perform(arg)
35
+ # ...
36
+ end
37
+ end
38
+
39
+ MyWorker.perform_spread('arg1')
40
+ ```
41
+
42
+ ### Parameters
43
+
44
+ * `spread_duration` - Size of window, in seconds, to spread jobs out over, defaults to 1 hour, will convert to seconds via `to_i`
45
+ * `spread_in` - Start of window offset from now, in seconds, defaults to 0, will convert to seconds via `to_i`
46
+ * `spread_at` - Start of window offset timestamp, defaults to now
47
+ * `spread_method` - Perform either a random or modulo spread, defaults to `rand`
48
+ * `spread_mod_value` - Value to use for determining mod offset, defaults to cast first argument to an Integer via `to_i`
49
+
50
+
51
+ `spread_duation` and `spread_method` can be set via `sidekiq_opions` in the worker class. If the same option is passed via the `perform_spread` method, it will override the `sidekiq_options` value for that job.
52
+
53
+ ```ruby
54
+ class MyWorker
55
+ include Sidekiq::Worker
56
+ include SidekiqSpread
57
+
58
+ sidekiq_options queue: :task, spread_duration: 2.hours, spread_method: :mod
59
+
60
+ def perform(arg)
61
+ # ...
62
+ end
63
+ end
64
+ ```
65
+
66
+ ### Other Exmaples
67
+
68
+ ```ruby
69
+ # Will schedule this job sometime within the next day
70
+ MyWorker.perform_spread('arg1', spread_duration: 1.day)
71
+
72
+ # Will schedule this job 1056 seconds from now:
73
+ # first argument cast to an integer modulo the `spread_duration` (1 hour) in seconds (3600)
74
+ # 123456 % 3600 = 1056
75
+ MyWorker.perform_spread('123456', spread_method: :mod)
76
+
77
+ # Will schedule this job 30 seconds from now
78
+ # 3630 % 3600 = 30
79
+ MyWorker.perform_spread(123456, spread_method: :mod, spread_mod_value: 3630)
80
+
81
+ # Will schedule this job 1116 seconds from now:
82
+ # first argument cast to an integer modulo the `spread_duration` (1 hour) in seconds (3600),
83
+ # plus 1 minute in seconds (60)
84
+ # 123456 % 3600 + 60 = 1116
85
+ MyWorker.perform_spread('123456', spread_method: :mod, spread_in: 1.minute)
86
+ ```
87
+
88
+ ### Name Arguments
89
+
90
+ Sidkiq doesn't place nicely with [named arguments](https://github.com/mperham/sidekiq/wiki/Best-Practices) and they will mess up some some of the argument parsing going on in this gem. Therefore this gem raises and `ArgumentError` if you try to use named arguments.
91
+
92
+ ## Development
93
+
94
+ 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.
95
+
96
+ 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).
97
+
98
+ ## Tests
99
+
100
+ To run the tests:
101
+
102
+ ```bash
103
+ rspec spec
104
+ ```
105
+
106
+ Tests are currently run against the following Ruby version:
107
+ - 2.5
108
+ - 2.4
109
+ - 2.3
110
+ - 2.2
111
+
112
+ ## Contributing
113
+
114
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Latermedia/sidekiq_spread. 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.
115
+
116
+ ## License
117
+
118
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
119
+
120
+ ## Code of Conduct
121
+
122
+ Everyone interacting in the SidekiqSpread project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Latermedia/sidekiq_spread/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,8 @@
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
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'sidekiq_spread'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,155 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sidekiq_spread/version'
4
+ require 'sidekiq'
5
+
6
+ # Helpers to spread worker load out over a period of time
7
+ module SidekiqSpread
8
+ PERFORM_SPREAD_OPTS = %i[
9
+ duration
10
+ in
11
+ at
12
+ method
13
+ mod_value
14
+ ].map { |o| "spread_#{o}".to_sym }.freeze
15
+
16
+ def self.included(base)
17
+ base.extend(ClassMethods)
18
+ end
19
+
20
+ # class methods for SidekiqSpread
21
+ module ClassMethods
22
+ # Randomly schedule worker over a window of time.
23
+ # Arguments are keys of the final options hash.
24
+ #
25
+ # @param spread_duration [Number] Size of window to spread workers out over
26
+ # @param spread_in [Number] Start of window offset from now
27
+ # @param spread_at [Number] Start of window offset timestamp
28
+ # @param spread_method [rand|mod] perform either a random or modulo spread,
29
+ # default: *:rand*
30
+ # @param spread_mod_value [Integer] value to use for determining mod offset
31
+ # @return [String] Sidekiq job id
32
+ def perform_spread(*args)
33
+ spread_duration = get_sidekiq_options['spread_duration'] || 1.hour
34
+ spread_in = 0
35
+ spread_at = nil
36
+ spread_method = get_sidekiq_options['spread_method'] || :rand
37
+ spread_mod_value = nil
38
+
39
+ spread_method = spread_method.to_sym if spread_method.present?
40
+
41
+ # process spread_* options
42
+
43
+ has_options = false
44
+
45
+ opts =
46
+ if !args.empty? && args.last.is_a?(::Hash)
47
+ has_options = true
48
+ args.pop
49
+ else
50
+ {}
51
+ end
52
+
53
+ sd = _extract_spread_opt(opts, :duration)
54
+ spread_duration = sd if sd.present?
55
+
56
+ si = _extract_spread_opt(opts, :in)
57
+ spread_in = si if si.present?
58
+
59
+ sa = _extract_spread_opt(opts, :at)
60
+ spread_at = sa if sa.present?
61
+
62
+ sm = _extract_spread_opt(opts, :method)
63
+ spread_method = sm.to_sym if sm.present?
64
+
65
+ smv = _extract_spread_opt(opts, :mod_value)
66
+ spread_mod_value = smv if smv.present?
67
+
68
+ # get left over options / keyword args
69
+ remaining_opts = opts.reject { |o| PERFORM_SPREAD_OPTS.include?(o.to_sym) }
70
+
71
+ # check args
72
+ num_args = args.length
73
+
74
+ # figure out the require params for #perform
75
+ params = new.method(:perform).parameters
76
+
77
+ num_req_args = params.select { |p| p[0] == :req }.length
78
+ num_opt_args = params.select { |p| p[0] == :opt }.length
79
+ num_req_key_args = params.select { |p| p[0] == :keyreq }.length
80
+ num_opt_key_args = params.select { |p| p[0] == :key }.length
81
+
82
+ # Sidekiq doesn't play nicely with named args
83
+ raise ArgumentError, "#{name}#perform should not use keyword args" if num_req_key_args > 0 || num_opt_key_args > 0
84
+
85
+ if has_options
86
+ # if we popped something off to process, push it back on
87
+ # if it contains arguments we need
88
+ if num_args < num_req_args
89
+ args.push(remaining_opts)
90
+ elsif num_args < (num_req_args + num_opt_args) && !remaining_opts.empty?
91
+ args.push(remaining_opts)
92
+ end
93
+ end
94
+
95
+ # if a spread_mod_value is not provided use the first argument,
96
+ # assumes it is an Integer
97
+ spread_mod_value = args.first if spread_mod_value.blank? && spread_method == :mod
98
+
99
+ # validate the spread_* options
100
+ _check_spread_args!(spread_duration, spread_method, spread_mod_value)
101
+
102
+ # calculate the offset for this job
103
+ spread = _set_spread(spread_method, spread_duration.to_i, spread_mod_value)
104
+
105
+ # call the correct perform_* method
106
+ if spread_at.present?
107
+ t = spread_at.to_i + spread
108
+ perform_at(t, *args)
109
+ else
110
+ t = spread_in.to_i + spread
111
+ if t.zero?
112
+ perform_async(*args)
113
+ else
114
+ perform_in(t, *args)
115
+ end
116
+ end
117
+ end
118
+
119
+ # @private
120
+ def _set_spread(spread_method, duration, spread_mod_value)
121
+ return 0 if duration.blank? || duration.zero?
122
+
123
+ case spread_method
124
+ when :rand
125
+ SecureRandom.random_number(duration)
126
+ when :mod
127
+ spread_mod_value % duration
128
+ end
129
+ end
130
+
131
+ # @private
132
+ def _check_spread_args!(duration, spread_method, spread_mod_value)
133
+ raise ArgumentError, 'Duration must be an integer' unless duration.is_a?(::Integer)
134
+ raise ArgumentError, 'Method must be rand or mod' unless %i[rand mod].include?(
135
+ spread_method
136
+ )
137
+ return unless spread_method == :mod
138
+
139
+ e = 'spread_mod_value must be provided or first arg must be an int to use mod'
140
+ raise ArgumentError, e unless spread_mod_value.is_a?(::Integer)
141
+ end
142
+
143
+ # @private
144
+ def _extract_spread_opt(opts, opt)
145
+ str = "spread_#{opt}"
146
+ sym = str.to_sym
147
+
148
+ if opts.key?(sym)
149
+ opts[sym]
150
+ elsif opts.key?(str)
151
+ opts[str]
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqSpread
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'sidekiq_spread/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'sidekiq_spread'
9
+ spec.version = SidekiqSpread::VERSION
10
+ spec.authors = ['Les Fletcher']
11
+ spec.email = ['les@later.com']
12
+
13
+ spec.summary = "Add 'perform_spread' method to workers to allow for scheduling jobs over an interval of time."
14
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
15
+ spec.homepage = 'https://github.com/Latermedia/sidekiq_spread'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency('sidekiq')
26
+
27
+ spec.add_development_dependency 'activesupport', '> 3.0'
28
+ spec.add_development_dependency 'bundler', '~> 1.16'
29
+ spec.add_development_dependency 'bundler-audit'
30
+ spec.add_development_dependency 'fakeredis'
31
+ spec.add_development_dependency 'rake', '~> 10.0'
32
+ spec.add_development_dependency 'rspec', '~> 3.0'
33
+ spec.add_development_dependency 'rspec_junit_formatter', '~> 0.2'
34
+ spec.add_development_dependency 'rubocop', '0.60.0'
35
+ spec.add_development_dependency 'yard'
36
+ end
metadata ADDED
@@ -0,0 +1,200 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq_spread
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Les Fletcher
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-27 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler-audit
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: fakeredis
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: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec_junit_formatter
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.2'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.2'
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.60.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.60.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: yard
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
+ email:
155
+ - les@later.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".circleci/config.yml"
161
+ - ".gitignore"
162
+ - ".rspec"
163
+ - ".rubocop.yml"
164
+ - CODE_OF_CONDUCT.md
165
+ - Gemfile
166
+ - Gemfile.lock
167
+ - LICENSE.txt
168
+ - README.md
169
+ - Rakefile
170
+ - bin/console
171
+ - bin/setup
172
+ - lib/sidekiq_spread.rb
173
+ - lib/sidekiq_spread/version.rb
174
+ - sidekiq_spread.gemspec
175
+ homepage: https://github.com/Latermedia/sidekiq_spread
176
+ licenses:
177
+ - MIT
178
+ metadata: {}
179
+ post_install_message:
180
+ rdoc_options: []
181
+ require_paths:
182
+ - lib
183
+ required_ruby_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ required_rubygems_version: !ruby/object:Gem::Requirement
189
+ requirements:
190
+ - - ">="
191
+ - !ruby/object:Gem::Version
192
+ version: '0'
193
+ requirements: []
194
+ rubyforge_project:
195
+ rubygems_version: 2.7.6
196
+ signing_key:
197
+ specification_version: 4
198
+ summary: Add 'perform_spread' method to workers to allow for scheduling jobs over
199
+ an interval of time.
200
+ test_files: []