resque-roulette 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 05cc1d80c24b00965ae14a24f8b78e73ea9008e6826026405be6cd163818b537
4
+ data.tar.gz: 81caf598215dedef4040e3a2defe01c43b4a90dd905c75bbad90a46cc8c1a50d
5
+ SHA512:
6
+ metadata.gz: '0319ef58c6365f05fe8658e3879bb31d56140ee96b8ef259a7f29b9068623b8781bf2f134c52a5a519042e1958fd9d14f118b653e1dcea706795d655037fc339'
7
+ data.tar.gz: 3398fb601d49ec79ae2e34dd5eff07d5c7c4b9d5cbbce5e9ef7f5692746b5e8198ca9a725715cad5ca2e6b0a4ad0e934f502e333ce86fcb2fec41b64b4fc2312
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 RA Trustvox
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ resque-roulette
2
+ ===============
3
+
4
+ ### Why?
5
+
6
+ I've many resque workers created in the same way using splat operator to run for every queue (QUEUE=*), but to don't wait a given queue process all your jobs,
7
+ I've been looking for a plugin to make that shuffle to me, and then I've found this magic plugin [resque-unfairly](https://github.com/seomoz/resque-unfairly)
8
+ but this is a old project and it's not in published at rubygems, so I've forked the repo and make some changes to could be published and used from rubygems instead of git directly.
9
+
10
+ Thanks for the awesome job Evan! :beers:
11
+
12
+ Usually Resque workers work on queues in the given order (if there is something in the first, work it, otherwise if the there is something in the second, work on it, and
13
+ so on). This plugin randomizes the order of the queues based on weights, so that a given queue will be the first queue to try based on a probability weight. Given queues A, B, C, D and
14
+ weights 4, 3, 2, 1, repsectively, A will be first 40% of the time, B 30%, C 20%, and D 10%. In addition, when B is first, A will be second 4/7ths of the time (4 / [4+2+1]), and so on. The
15
+ project is inspired by [resque-fairly](https://github.com/pezra/resque-fairly) by Peter Williams, which unfortunately mathematically does not give you this control over the weights.
16
+
17
+ ### Example usage
18
+
19
+ ``` ruby
20
+ require 'resque'
21
+ require 'resque/plugins/roulette'
22
+
23
+ Resque::Plugins::Roulette.prioritize('myqueue' 1)
24
+ Resque::Plugins::Roulette.prioritize('myotherqueue', 3)
25
+ Resque::Plugins::Roulette.prioritize('someotherqueue', 6)
26
+ ```
27
+
28
+ Now, workers processing all three queues will (assuming all queues have jobs) take jobs from someotherqueue 60% of the time, myotherqueue 30% of the time, and myqueue 10% of the time. This is achieved
29
+ by reordering the queues, so if someotherqueue is empty, the workers will take jobs from myotherqueue 75% (3/4) of the time.
30
+
31
+
32
+ ### Authors
33
+
34
+ - **Mantainer** - Bruno Casali @brunoocasali
35
+ - **Creator** - Evan Battaglia @evanbattaglia
@@ -0,0 +1,2 @@
1
+ require 'resque'
2
+ require File.expand_path('resque/plugins/roulette', File.dirname(__FILE__))
@@ -0,0 +1,67 @@
1
+ require 'resque/worker'
2
+
3
+ module Resque::Plugins
4
+ module Roulette
5
+ # Define an 'unfair' priority multiplier to queues
6
+ def self.prioritize(queue, value)
7
+ priorities[queue] = value
8
+ end
9
+
10
+ def self.priorities
11
+ @priorities ||= {}
12
+ end
13
+
14
+ # Returns a list of queues to use when searching for a job. A
15
+ # splat ("*") means you want every queue
16
+ #
17
+ # The queues will be ordered randomly and the order will change
18
+ # with every call. This prevents any particular queue for being
19
+ # starved. Workers will process queues in a random order each
20
+ # time the poll for new work.
21
+ #
22
+ # If priorities have been established, the randomness of the order
23
+ # will be weighted according to the multipliers
24
+ def queues_randomly_ordered
25
+ queue_priorities = queues_orig_ordered.map { |q| Roulette.priorities[q] }
26
+ if queue_priorities.compact.any?
27
+ weighted_order(queues_orig_ordered, queue_priorities.map { |p| p || 1 })
28
+ else
29
+ queues_orig_ordered
30
+ end
31
+ end
32
+
33
+ def self.included(klass)
34
+ klass.instance_eval do
35
+ alias queues_orig_ordered queues
36
+ alias queues queues_randomly_ordered
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ # Give the index of an item in weights based on the weights
43
+ def weighted_rand_index(weights)
44
+ rand_val = rand * weights.inject(&:+)
45
+ weights.each_with_index do |weight, index|
46
+ return index if (rand_val -= weight).negative?
47
+ end
48
+ 0
49
+ end
50
+
51
+ def weighted_order(vals, weights)
52
+ vals = vals.dup
53
+ weights = weights.dup
54
+
55
+ [].tap do |results|
56
+ until vals.empty?
57
+ val_index = weighted_rand_index(weights)
58
+ results << vals[val_index]
59
+ vals.delete_at val_index
60
+ weights.delete_at val_index
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ Resque::Worker.send(:include, Roulette)
67
+ end
@@ -0,0 +1,8 @@
1
+ module Resque
2
+ module Plugins
3
+ module Roulette
4
+ VERSION = '0.0.1'
5
+ end
6
+ end
7
+ end
8
+
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-roulette
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Evan Battaglia
8
+ - Bruno Casali
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2019-01-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: resque
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '12.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '12.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '3.8'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '3.8'
56
+ description: Usually Resque workers work on queues in the given order (if there is
57
+ something in the first, work it, otherwise if the there is something in the second,
58
+ work on it, and so on). This plugin randomizes the order of the queues based on
59
+ weights, so that a given queue will be the first queue to try based on a probability
60
+ weight. Given queues A, B, C, D and weights 4, 3, 2, 1, repsectively, A will be
61
+ first 40% of the time, B 30%, C 20%, and D 10%. In addition, when B is first, A
62
+ will be second 4/7ths of the time (4 / [4+2+1]), and so on. The project is inspired
63
+ by resque-fairly, which unfortunately mathematically does not give you this control
64
+ over the weights.
65
+ email:
66
+ - evan@seomoz.org
67
+ - bruno@trustvox.com.br
68
+ executables: []
69
+ extensions: []
70
+ extra_rdoc_files: []
71
+ files:
72
+ - LICENSE
73
+ - README.md
74
+ - lib/resque-roulette.rb
75
+ - lib/resque/plugins/roulette.rb
76
+ - lib/resque/plugins/roulette/version.rb
77
+ homepage: https://github.com/trustvox/resque-roulette
78
+ licenses: []
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.0.1
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Order Resque queues probabilistically given a list of weights
99
+ test_files: []