Skip to content
This repository was archived by the owner on Jul 30, 2019. It is now read-only.

Commit bba4307

Browse files
committed
Ensure proper exit from error
It appears that Thor does not allow exiting with an error code that is not 0, unless that is raised by Thor itself. There is some [description here][1]. I wish to be able to capture errors and put them into Logger, so I have added a `begin`/`rescue` block capturing all errors. I've added a very small test to capture that the errors are handled, but I think there is some further testing that I'd like to be able to add to ensure the service behaves as we expect it to. I'd like to add tests to capture the exceptions raised by AWS errors which is possible to stub, but I need to figure out how best to write this. For now I'm just adding the simple block and test. It is suggested that you can replace the `exit_on_failure?` method, but this did not capture the exception like I wished it to[2], and I had trouble implementing this against the correct test. [1]: rails/thor#244 [2]: rails/thor#244 (comment)
1 parent 88d4cf4 commit bba4307

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

lib/drone_autoscale.rb

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ class DroneAutoScale < Thor
1313
def agent
1414
Logger.new(STDOUT).info "Agent: started"
1515
loop do
16-
Agent.new(
17-
host: options[:host],
18-
aws_region: options[:aws_region],
19-
group_name_query: options[:group_name_query]
20-
).run
16+
begin
17+
Agent.new(
18+
host: options[:host],
19+
aws_region: options[:aws_region],
20+
group_name_query: options[:group_name_query]
21+
).run
22+
rescue => e
23+
Logger.new(STDERR).error e.to_s
24+
abort
25+
end
2126
sleep(options[:polling_time].to_i)
2227
end
2328
end
@@ -31,12 +36,17 @@ def agent
3136
def server
3237
Logger.new(STDOUT).info "Server: started"
3338
loop do
34-
Server.new(
35-
host: options[:host],
36-
aws_region: options[:aws_region],
37-
namespace: options[:namespace],
38-
drone_api_token: options[:drone_api_token]
39-
).run
39+
begin
40+
Server.new(
41+
host: options[:host],
42+
aws_region: options[:aws_region],
43+
namespace: options[:namespace],
44+
drone_api_token: options[:drone_api_token]
45+
).run
46+
rescue => e
47+
Logger.new(STDERR).error e.to_s
48+
abort
49+
end
4050
sleep(options[:polling_time].to_i)
4151
end
4252
end

lib/drone_autoscale/server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def initialize(
1111
namespace: 'Drone',
1212
drone_api_token: nil
1313
)
14-
abort("Error: must provide Drone API token") if drone_api_token.nil?
14+
raise StandardError.new("Must provide Drone API token") if drone_api_token.nil?
1515
@client = Aws::CloudWatch::Client.new(region: aws_region)
1616
@host = host
1717
@namespace = namespace

spec/drone_autoscale_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper'
2+
3+
require 'drone_autoscale'
4+
5+
RSpec.describe DroneAutoScale do
6+
describe '#agent' do
7+
it 'should exit when endpoints are unavailable' do
8+
expect { described_class.start(['agent']) }.to raise_exception(SystemExit)
9+
end
10+
end
11+
12+
describe '#server' do
13+
it 'should exit when endpoints are unavailable' do
14+
expect { described_class.start(['server']) }.to raise_exception(SystemExit)
15+
end
16+
end
17+
end

0 commit comments

Comments
 (0)