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 +4 -4
- data/README.md +18 -12
- data/lib/resque/stagger/version.rb +1 -1
- data/lib/resque/stagger.rb +22 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 295255a8e4bc398c6ee16a781b2216605a02b805
|
4
|
+
data.tar.gz: fccfa6015f9a35d74e85db1247ba33cff46e7fbc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a2f1f97d40dea588561632468115cbcc610c90d21c8bd75b34f4b27bb585df78b06153077e10946f558d7b2befb72aefb8a247e30a73959939b51d3ae6884a0
|
7
|
+
data.tar.gz: 4f6e89328f246286cd4ea444977bcf0808426878bdae07dcbb93b7b62a8040e7743022e2c464015bade0d3acc3749caf74ab447cedb4e585fddff00e584d168f
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
|
-
# Resque
|
1
|
+
# Resque Stagger
|
2
2
|
|
3
|
-
|
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
|
-
|
23
|
+
You can use `Resque::Staggered` to enqueue jobs with a stagger effect.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require "resque/stagger"
|
26
27
|
|
27
|
-
|
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
|
-
|
31
|
+
```
|
30
32
|
|
31
|
-
|
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
|
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
|
44
|
-
|
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
|
+
|
data/lib/resque/stagger.rb
CHANGED
@@ -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.
|
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
|