resque-stagger 0.1.2 → 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: a856cad256333d27eab77f6a0da0beb30296d44e
4
- data.tar.gz: ed65145aa9a584c2b9a7063234532356b30bf198
3
+ metadata.gz: 295255a8e4bc398c6ee16a781b2216605a02b805
4
+ data.tar.gz: fccfa6015f9a35d74e85db1247ba33cff46e7fbc
5
5
  SHA512:
6
- metadata.gz: e860025fb48a92cf59fbccced17cb37744a8363220556d87c8cbf8e41bf2ba0b6f989b882df86d5bb3fd1b6e912ecaf1e6c604f7f3cd010966e1b3667b069d30
7
- data.tar.gz: 5bc5ce9c16d12460f3bda711851656ddaac60692acb81268a8dfef9c3f119b5eeece5950dadae4a0dcc78ddf8f4dc0e2d741f148487fdbc83720968660e032c1
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.1.2"
3
+ VERSION = "0.2.1"
4
4
  end
5
5
  end
@@ -1,34 +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
- # ::Resque.enqueue_at(current_enqueue_at, klass, *args)
12
- puts current_enqueue_at
22
+ ::Resque.enqueue_at(current_enqueue_at, klass, *args)
13
23
  end
14
24
 
15
25
  private
16
26
 
27
+ # Calculates the time at which the job should be enqueued.
28
+ #
29
+ # @return [Time] the enqueue time for the job.
17
30
  def current_enqueue_at
18
31
  return starting_from if per_second.to_i.zero?
19
- return @last_enqueued_at = starting_from if @last_enqueued_at.blank?
32
+ return @last_enqueued_at = starting_from if @last_enqueued_at.nil?
20
33
 
21
34
  @enqueued_last_second = @enqueued_last_second.to_i + 1
22
- return @last_enqueued_at if @enqueued_last_second < per_second
35
+ return @last_enqueued_at if @enqueued_last_second < per_second.to_i
23
36
 
24
37
  @enqueued_last_second = 0
25
38
  @last_enqueued_at += 1.second
26
39
  end
27
40
 
41
+ # Gets the starting time for enqueuing jobs.
42
+ #
43
+ # @return [Time] the starting time for enqueuing jobs.
28
44
  def starting_from
29
45
  @starting_from ||= @options[:start_from] || Time.current
30
46
  end
31
47
 
48
+ # Gets the number of jobs to enqueue per second.
49
+ #
50
+ # @return [Integer, nil] the number of jobs to enqueue per second.
32
51
  def per_second
33
52
  @options[:per_second] || nil
34
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.1.2
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Parikshit Singh