autoscaler 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ module Autoscaler
2
+ # This is a sort of middleware that keeps the last worker up for a minimum amount of time
3
+ class DelayedShutdown
4
+ # @param [ScalingStrategy] strategy object that makes most decisions
5
+ # @param [Numeric] timeout time to stay up after base strategy says zero
6
+ def initialize(strategy, timeout)
7
+ @strategy = strategy
8
+ @timeout = timeout
9
+ active_now!
10
+ end
11
+
12
+ # @param [QueueSystem] system interface to the queuing system
13
+ # @param [Numeric] event_idle_time number of seconds since a job related event
14
+ # @return [Integer] target number of workers
15
+ def call(system, event_idle_time)
16
+ target_workers = strategy.call(system, event_idle_time)
17
+ if target_workers > 0
18
+ active_now!
19
+ target_workers
20
+ elsif time_left?(event_idle_time)
21
+ 1
22
+ else
23
+ 0
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :strategy
30
+ attr_reader :timeout
31
+
32
+ def active_now!
33
+ @activity = Time.now
34
+ end
35
+
36
+ def level_idle_time
37
+ Time.now - @activity
38
+ end
39
+
40
+ def time_left?(event_idle_time)
41
+ [event_idle_time, level_idle_time].min < timeout
42
+ end
43
+ end
44
+ end
@@ -1,4 +1,4 @@
1
1
  module Autoscaler
2
2
  # version number
3
- VERSION = "0.4.0"
3
+ VERSION = "0.4.1"
4
4
  end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+ require 'test_system'
3
+ require 'autoscaler/delayed_shutdown'
4
+
5
+ describe Autoscaler::DelayedShutdown do
6
+ let(:cut) {Autoscaler::DelayedShutdown}
7
+
8
+ it "returns normal values" do
9
+ strategy = cut.new(lambda{|s,t| 2}, 0)
10
+ strategy.call(nil, 1).should == 2
11
+ end
12
+
13
+ it "delays zeros" do
14
+ strategy = cut.new(lambda{|s,t| 0}, 60)
15
+ strategy.call(nil, 1).should == 1
16
+ end
17
+
18
+ it "eventually returns zero" do
19
+ strategy = cut.new(lambda{|s,t| 0}, 60)
20
+ strategy.stub(:level_idle_time).and_return(61)
21
+ strategy.call(nil, 61).should == 0
22
+ end
23
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autoscaler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-06-21 00:00:00.000000000 Z
13
+ date: 2013-06-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sidekiq
@@ -133,6 +133,7 @@ extensions: []
133
133
  extra_rdoc_files: []
134
134
  files:
135
135
  - lib/autoscaler/binary_scaling_strategy.rb
136
+ - lib/autoscaler/delayed_shutdown.rb
136
137
  - lib/autoscaler/heroku_scaler.rb
137
138
  - lib/autoscaler/sidekiq/activity.rb
138
139
  - lib/autoscaler/sidekiq/celluloid_monitor.rb
@@ -152,6 +153,7 @@ files:
152
153
  - examples/simple.rb
153
154
  - Guardfile
154
155
  - spec/autoscaler/binary_scaling_strategy_spec.rb
156
+ - spec/autoscaler/delayed_shutdown_spec.rb
155
157
  - spec/autoscaler/heroku_scaler_spec.rb
156
158
  - spec/autoscaler/sidekiq/activity_spec.rb
157
159
  - spec/autoscaler/sidekiq/celluloid_monitor_spec.rb
@@ -190,6 +192,7 @@ summary: Start/stop Sidekiq workers on Heroku
190
192
  test_files:
191
193
  - Guardfile
192
194
  - spec/autoscaler/binary_scaling_strategy_spec.rb
195
+ - spec/autoscaler/delayed_shutdown_spec.rb
193
196
  - spec/autoscaler/heroku_scaler_spec.rb
194
197
  - spec/autoscaler/sidekiq/activity_spec.rb
195
198
  - spec/autoscaler/sidekiq/celluloid_monitor_spec.rb