resque-balancer 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
+ SHA1:
3
+ metadata.gz: c5cd7870655fb2f02e588f185773b0422b9a9f75
4
+ data.tar.gz: 6883c5a68179c755ae0f6ef9ed94708c59e9ff54
5
+ SHA512:
6
+ metadata.gz: 9f5f9fb4af866c955f175bf9d39d253724ef159b04248cb0c6450f7ce0964143a9db652c67e456a732354bd2160f49475bbc2e814ab6d73374abdca32e3fc48c
7
+ data.tar.gz: f60ff4dc15610aea60140708dfff3d583b98f2f360df38b17881451c7c46790e207dc4df6dacea11c66690217c0a1a220b7c758638af45ff308fe537735b7b51
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2013 Michael Grosser <michael@grosser.it>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,2 @@
1
+ # helper file to make bundler requires easy
2
+ require 'resque/plugins/balancer'
@@ -0,0 +1,5 @@
1
+ module Resque
2
+ module Balancer
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,58 @@
1
+ require 'resque'
2
+
3
+ module Resque
4
+ module Plugins
5
+ module Balancer
6
+ # override
7
+ # record start time
8
+ def working_on(job)
9
+ @balancer_start = [job, Time.now]
10
+ super
11
+ end
12
+
13
+ # override
14
+ # record how much time the job used
15
+ def done_working
16
+ job, start = @balancer_start
17
+ usage = (Time.now.to_f - start.to_f) / balancer_weights[job.queue]
18
+ balancer_usage[job.queue] += usage
19
+ super
20
+ end
21
+
22
+ # override
23
+ # - prefer queues that were used the least
24
+ # - prefer queues with high weight
25
+ def queues
26
+ balaner_reset_usage_after_interval
27
+ super.sort_by { |q| [balancer_usage[q], -balancer_weights[q]] }
28
+ end
29
+
30
+ def balancer_usage
31
+ @balancer_usage ||= Hash.new(0.0)
32
+ end
33
+
34
+ def balancer_weights
35
+ @balancer_weights ||= begin
36
+ weights = ENV['BALANCER_WEIGHTS'].to_s.split(',')
37
+ weights.each_with_object(Hash.new(1)) do |w, all|
38
+ name, weight = w.split(':', 2)
39
+ all[name] = weight.to_f if weight
40
+ end
41
+ end
42
+ end
43
+
44
+ # every x seconds reset the usage so busy jobs get a new chance of behaving
45
+ def balaner_reset_usage_after_interval
46
+ now = Time.now.to_f
47
+ @balancer_last_reset ||= now
48
+ interval = (ENV['BALANCER_RESET_INTERVAL'] || '600').to_f
49
+ if @balancer_last_reset + interval < now
50
+ balancer_usage.clear
51
+ @balancer_last_reset = now
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ Resque::Worker.send(:prepend, Resque::Plugins::Balancer)
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-balancer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Grosser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: resque
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.26.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.26.0
27
+ description:
28
+ email: michael@grosser.it
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - MIT-LICENSE
34
+ - lib/resque-balancer.rb
35
+ - lib/resque/balancer/version.rb
36
+ - lib/resque/plugins/balancer.rb
37
+ homepage: https://github.com/grosser/resque-balancer
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.0.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.5.1
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Balances queues by allotted time, prevents 1 queue from starving all others.
61
+ test_files: []