heroku-scalr 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- heroku-scalr (0.2.3)
4
+ heroku-scalr (0.2.4)
5
5
  heroku-api (~> 0.3.8)
6
6
  timers
7
7
 
data/heroku-scalr.gemspec CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.name = File.basename(__FILE__, '.gemspec')
8
8
  s.summary = "Watch and scale your dynos!"
9
9
  s.description = "Issues recurring 'pings' to your Heroku apps and scales dynos up or down depending on pre-defined rules"
10
- s.version = "0.2.3"
10
+ s.version = "0.2.4"
11
11
 
12
12
  s.authors = ["Black Square Media"]
13
13
  s.email = "info@blacksquaremedia.com"
@@ -12,7 +12,7 @@ class Heroku::Scalr::App
12
12
  min_frequency: 60
13
13
  }.freeze
14
14
 
15
- attr_reader :name, :url, :api, :interval, :min_dynos, :max_dynos,
15
+ attr_reader :name, :url, :api_key, :interval, :min_dynos, :max_dynos,
16
16
  :metric, :wait_low, :wait_high, :ping_low, :ping_high,
17
17
  :min_frequency, :last_scaled_at
18
18
 
@@ -41,9 +41,8 @@ class Heroku::Scalr::App
41
41
  fail("max_dynos must be at least 1") unless opts[:max_dynos] >= 1
42
42
  fail("interval must be at least 10") unless opts[:interval] >= 10
43
43
 
44
- @url = opts[:url] || "http://#{@name}.herokuapp.com/robots.txt"
45
- @api = Heroku::API.new api_key: opts[:api_key]
46
-
44
+ @url = opts[:url] || "http://#{@name}.herokuapp.com/robots.txt"
45
+ @api_key = opts[:api_key]
47
46
  @interval = opts[:interval].to_i
48
47
  @min_dynos = opts[:min_dynos].to_i
49
48
  @max_dynos = opts[:max_dynos].to_i
@@ -59,8 +58,9 @@ class Heroku::Scalr::App
59
58
  # Scales the app
60
59
  def scale!
61
60
  scale_at = next_scale_attempt
62
- if Time.now < scale_at
63
- log :debug, "skip scaling, next attempt in #{(scale_at - Time.now).to_i}s"
61
+ now = Time.now
62
+ if now < scale_at
63
+ log :debug, "skip check, next attempt in #{(scale_at - now).to_i}s"
64
64
  return
65
65
  end
66
66
 
@@ -89,6 +89,7 @@ class Heroku::Scalr::App
89
89
  def do_scale(by)
90
90
  return if by.zero?
91
91
 
92
+ api = Heroku::API.new(api_key: api_key)
92
93
  info = api.get_app(name)
93
94
  unless info.status == 200
94
95
  log :warn, "error fetching app info, responded with #{info.status}"
@@ -0,0 +1,11 @@
1
+ class Logger
2
+
3
+ def reopen
4
+ return unless @logdev && @logdev.filename
5
+
6
+ @logdev.dev.reopen(@logdev.filename, "a")
7
+ @logdev.dev.sync = true
8
+ self
9
+ end
10
+
11
+ end unless Logger.public_instance_methods.include?(:reopen)
@@ -23,6 +23,7 @@ class Heroku::Scalr::Runner
23
23
  SIGNALS.each do |sig|
24
24
  Signal.trap(sig) { stop! }
25
25
  end
26
+ Signal.trap("USR1") { Heroku::Scalr.logger.reopen }
26
27
 
27
28
  loop { timers.wait }
28
29
  end
data/lib/heroku/scalr.rb CHANGED
@@ -28,6 +28,6 @@ module Heroku::Scalr
28
28
 
29
29
  end
30
30
 
31
- %w|config app runner metric|.each do |name|
31
+ %w|core_ext config app runner metric|.each do |name|
32
32
  require "heroku/scalr/#{name}"
33
33
  end
@@ -10,7 +10,7 @@ describe Heroku::Scalr::App do
10
10
 
11
11
  its(:name) { should == 'name' }
12
12
  its(:url) { should == 'http://name.herokuapp.com/robots.txt' }
13
- its(:api) { should be_instance_of(Heroku::API) }
13
+ its(:api_key) { should == 'key' }
14
14
  its(:metric) { should be_instance_of(Heroku::Scalr::Metric::Ping) }
15
15
 
16
16
  its(:interval) { should be(30) }
@@ -43,8 +43,13 @@ describe Heroku::Scalr::App do
43
43
  end
44
44
 
45
45
  describe "scaling" do
46
+
47
+ let :mock_api do
48
+ mock "Heroku::API", get_app: mock_response(200, { "dynos" => 2 }), post_ps_scale: mock_response(200, "")
49
+ end
50
+
46
51
  before do
47
- subject.api.stub get_app: mock_response(200, { "dynos" => 2 }), post_ps_scale: mock_response(200, "")
52
+ Heroku::API.stub new: mock_api
48
53
  subject.metric.stub by: -1
49
54
  end
50
55
 
@@ -54,7 +59,7 @@ describe Heroku::Scalr::App do
54
59
  end
55
60
 
56
61
  it "should log errors" do
57
- subject.api.should_receive(:get_app).and_raise(RuntimeError, "API Error")
62
+ mock_api.should_receive(:get_app).and_raise(RuntimeError, "API Error")
58
63
  Heroku::Scalr.logger.should_receive(:error)
59
64
  subject.scale!.should be_nil
60
65
  end
@@ -70,20 +75,20 @@ describe Heroku::Scalr::App do
70
75
  end
71
76
 
72
77
  it "should check current number of dynos" do
73
- subject.api.should_receive(:get_app).with("name").and_return mock_response(200, { "dynos" => 2 })
78
+ mock_api.should_receive(:get_app).with("name").and_return mock_response(200, { "dynos" => 2 })
74
79
  subject.scale!.should == 1
75
80
  end
76
81
 
77
82
  context "down" do
78
83
 
79
84
  it "should return the new number of dynos" do
80
- subject.api.should_receive(:post_ps_scale).with("name", "web", 1).and_return mock_response(200, "")
85
+ mock_api.should_receive(:post_ps_scale).with("name", "web", 1).and_return mock_response(200, "")
81
86
  subject.scale!.should == 1
82
87
  end
83
88
 
84
89
  it "should skip if min number of dynos reached" do
85
- subject.api.should_receive(:get_app).with("name").and_return mock_response(200, { "dynos" => 1 })
86
- subject.api.should_not_receive(:post_ps_scale)
90
+ mock_api.should_receive(:get_app).with("name").and_return mock_response(200, { "dynos" => 1 })
91
+ mock_api.should_not_receive(:post_ps_scale)
87
92
  subject.scale!.should be_nil
88
93
  end
89
94
 
@@ -94,13 +99,13 @@ describe Heroku::Scalr::App do
94
99
  before { subject.metric.stub by: 1 }
95
100
 
96
101
  it "should return the new number of dynos" do
97
- subject.api.should_receive(:post_ps_scale).with("name", "web", 3).and_return mock_response(200, "")
102
+ mock_api.should_receive(:post_ps_scale).with("name", "web", 3).and_return mock_response(200, "")
98
103
  subject.scale!.should == 3
99
104
  end
100
105
 
101
106
  it "should skip if max number of dynos reached" do
102
- subject.api.should_receive(:get_app).with("name").and_return mock_response(200, { "dynos" => 3 })
103
- subject.api.should_not_receive(:post_ps_scale)
107
+ mock_api.should_receive(:get_app).with("name").and_return mock_response(200, { "dynos" => 3 })
108
+ mock_api.should_not_receive(:post_ps_scale)
104
109
  subject.scale!.should be_nil
105
110
  end
106
111
 
@@ -11,7 +11,7 @@ describe Heroku::Scalr::Config do
11
11
  it 'should merge defaults into app configurations' do
12
12
  app = subject.apps.first
13
13
  app.should be_instance_of(Heroku::Scalr::App)
14
- app.api.instance_variable_get(:@api_key).should == "API_KEY"
14
+ app.api_key.should == "API_KEY"
15
15
  end
16
16
 
17
17
  end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+ require 'tempfile'
3
+
4
+ describe Logger do
5
+
6
+ describe "#reopen" do
7
+
8
+ let(:path) { Tempfile.new(["heroku-scalr", "logger"]).path }
9
+ subject { Logger.new(path) }
10
+
11
+ it "should reopen files" do
12
+ subject.info "Line 1"
13
+ FileUtils.mv(path, "#{path}.1")
14
+ subject.info "Line 2"
15
+ subject.reopen.should be(subject)
16
+ subject.info "Line 3"
17
+
18
+ File.read("#{path}.1").should have(2).lines
19
+ File.read(path).should have(1).lines
20
+ end
21
+
22
+ it "should skip streams" do
23
+ logger = Logger.new(STDERR)
24
+ logger.reopen.should be_nil
25
+ end
26
+
27
+ end
28
+
29
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: heroku-scalr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-15 00:00:00.000000000 Z
12
+ date: 2013-05-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: timers
@@ -142,11 +142,13 @@ files:
142
142
  - lib/heroku/scalr/app.rb
143
143
  - lib/heroku/scalr/cli.rb
144
144
  - lib/heroku/scalr/config.rb
145
+ - lib/heroku/scalr/core_ext.rb
145
146
  - lib/heroku/scalr/metric.rb
146
147
  - lib/heroku/scalr/runner.rb
147
148
  - spec/fixtures/config_a.rb
148
149
  - spec/heroku/scalr/app_spec.rb
149
150
  - spec/heroku/scalr/config_spec.rb
151
+ - spec/heroku/scalr/core_ext_spec.rb
150
152
  - spec/heroku/scalr/metric_spec.rb
151
153
  - spec/heroku/scalr/runner_spec.rb
152
154
  - spec/heroku/scalr_spec.rb