resque-stagger 0.2.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2750c1757a945716e79e0f58ad4f32f15f8d20f5
4
- data.tar.gz: 4b6c3d32a6721d4fa43cce4909f4b38ac79d3cf5
3
+ metadata.gz: 295255a8e4bc398c6ee16a781b2216605a02b805
4
+ data.tar.gz: fccfa6015f9a35d74e85db1247ba33cff46e7fbc
5
5
  SHA512:
6
- metadata.gz: b3cff4560f6019f023f0caab822e1b2700d90c33fe8221914c3dcdcb27758ff024a73fe0914e79278ac8e5e3a350bd37500c262650b602663ad5f1ba60b30cb0
7
- data.tar.gz: 85ab53b25b74eb50856c2cb97060aa565891b841c6cb8f4eac14b0f990c1fac22570a6eb73529cd4c22b121b35d5843fa1bc380a8a833e3e5fa1c0b65402a132
6
+ metadata.gz: 1a2f1f97d40dea588561632468115cbcc610c90d21c8bd75b34f4b27bb585df78b06153077e10946f558d7b2befb72aefb8a247e30a73959939b51d3ae6884a0
7
+ data.tar.gz: 4f6e89328f246286cd4ea444977bcf0808426878bdae07dcbb93b7b62a8040e7743022e2c464015bade0d3acc3749caf74ab447cedb4e585fddff00e584d168f
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # Resque::Stagger
1
+ # Resque Stagger
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/resque/stagger`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ A Resque plugin for adding a stagger effect to enqueuing jobs.
6
4
 
7
5
  ## Installation
8
6
 
@@ -14,7 +12,7 @@ gem 'resque-stagger'
14
12
 
15
13
  And then execute:
16
14
 
17
- $ bundle
15
+ $ bundle install
18
16
 
19
17
  Or install it yourself as:
20
18
 
@@ -22,17 +20,25 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ You can use `Resque::Staggered` to enqueue jobs with a stagger effect.
24
+
25
+ ```ruby
26
+ require "resque/stagger"
26
27
 
27
- ## Development
28
+ staggered_instance = Resque::Staggered.new(per_second: 2, start_from: Time.now + 5.seconds)
29
+ staggered_instance.enqueue(MyJob, "arg1", "arg2")
28
30
 
29
- 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.
31
+ ```
30
32
 
31
- 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).
33
+ The Staggered class takes two optional parameters: :per_second and :start_from.
34
+
35
+ > **:per_second** is the number of jobs to enqueue per second. The default value is nil, which means no limit on the number of jobs to enqueue per second.
36
+
37
+ > **:start_from** is the starting time for enqueuing jobs. The default value is Time.current.
32
38
 
33
39
  ## Contributing
34
40
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/resque-stagger. 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.
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/resque-stagger. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
36
42
 
37
43
  ## License
38
44
 
@@ -40,5 +46,5 @@ The gem is available as open source under the terms of the [MIT License](https:/
40
46
 
41
47
  ## Code of Conduct
42
48
 
43
- Everyone interacting in the Resque::Stagger project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/resque-stagger/blob/master/CODE_OF_CONDUCT.md).
44
- # resque-stagger
49
+ Everyone interacting in the Resque Stagger project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/resque-stagger/blob/master/CODE_OF_CONDUCT.md).
50
+
@@ -1,5 +1,5 @@
1
1
  module Resque
2
2
  module Stagger
3
- VERSION = "0.2.0"
3
+ VERSION = "0.2.1"
4
4
  end
5
5
  end
@@ -1,33 +1,53 @@
1
1
  require "resque/stagger/version"
2
2
 
3
+ # The Resque module contains the Stagger module.
3
4
  module Resque
4
5
  module Stagger
6
+ # The Staggered class is used to enqueue jobs with a stagger effect.
5
7
  class Staggered
8
+ # Initializes the Staggered class with given options.
9
+ #
10
+ # @param options [Hash] the options for the stagger effect.
11
+ # @option options [Time] :start_from starting time for enqueuing jobs.
12
+ # @option options [Integer] :per_second number of jobs to enqueue per second.
6
13
  def initialize(**options)
7
14
  @options = options
8
15
  end
9
16
 
17
+ # Enqueues the given job with a stagger effect.
18
+ #
19
+ # @param klass [Class] the job class to be enqueued.
20
+ # @param args [Array] the arguments for the job.
10
21
  def enqueue(klass, *args)
11
22
  ::Resque.enqueue_at(current_enqueue_at, klass, *args)
12
23
  end
13
24
 
14
25
  private
15
26
 
27
+ # Calculates the time at which the job should be enqueued.
28
+ #
29
+ # @return [Time] the enqueue time for the job.
16
30
  def current_enqueue_at
17
31
  return starting_from if per_second.to_i.zero?
18
- return @last_enqueued_at = starting_from if @last_enqueued_at.blank?
32
+ return @last_enqueued_at = starting_from if @last_enqueued_at.nil?
19
33
 
20
34
  @enqueued_last_second = @enqueued_last_second.to_i + 1
21
- return @last_enqueued_at if @enqueued_last_second < per_second
35
+ return @last_enqueued_at if @enqueued_last_second < per_second.to_i
22
36
 
23
37
  @enqueued_last_second = 0
24
38
  @last_enqueued_at += 1.second
25
39
  end
26
40
 
41
+ # Gets the starting time for enqueuing jobs.
42
+ #
43
+ # @return [Time] the starting time for enqueuing jobs.
27
44
  def starting_from
28
45
  @starting_from ||= @options[:start_from] || Time.current
29
46
  end
30
47
 
48
+ # Gets the number of jobs to enqueue per second.
49
+ #
50
+ # @return [Integer, nil] the number of jobs to enqueue per second.
31
51
  def per_second
32
52
  @options[:per_second] || nil
33
53
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-stagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Parikshit Singh