Skip to content

Commit

Permalink
Adding command line option to specify state file location
Browse files Browse the repository at this point in the history
  • Loading branch information
hammady committed Nov 2, 2017
1 parent 2c8a8ba commit fa216ee
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ It can be used to monitor web services and worker services. The web services typ
This gem is inspired by [HireFire](https://manager.hirefire.io/) and was indeed motivated by the migration
from [Heroku](https://www.heroku.com/) to Docker Swarm mode.

## Installation (not published yet to rubygems)
## Installation

Add this line to your application's Gemfile:

Expand Down Expand Up @@ -36,6 +36,10 @@ Configuration is read from `scaltainer.yml` by default. If you want to read from
Note that after each run a new file is created (`yourconfig.yml.state`) which stores the state of the previous run.
This is because there are some configuration parameters (like sensitivity) need to
remember previous runs.
If you want to specify a different location for the state file, add the `--state-file` parameter.
Example:

scaltainer -f /path/to/configuration/file.yml --state-file /path/to/different/state/file.yml

Typically, the above command should be put inside a cronjob that is triggered every minute or so.

Expand All @@ -56,6 +60,7 @@ for each worker.
- `NEW_RELIC_LICENSE_KEY`: New Relic license key. Currently New Relic
is used to retrieve average response time metric for web services.
More monitoring services can be added in the future.

- `RESPONSE_TIME_WINDOW`: Time window in minutes to measure
average response time till the moment. For example 3 means
measure average response time in the past 3 minutes. Default value is 5.
Expand Down
4 changes: 2 additions & 2 deletions exe/scaltainer
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require 'scaltainer'

begin
configfile, logger = Scaltainer::Command.parse ARGV
Scaltainer::Runner.new configfile, logger
configfile, statefile, logger = Scaltainer::Command.parse ARGV
Scaltainer::Runner.new configfile, statefile, logger
rescue => e
$stderr.puts e.message
$stderr.puts e.backtrace
Expand Down
11 changes: 8 additions & 3 deletions lib/scaltainer/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@
module Scaltainer
class Command
def self.parse(args)
configfile = 'scaltainer.yml'
configfile, statefile = 'scaltainer.yml', nil
OptionParser.new do |opts|
opts.banner = "Usage: scaltainer [options]"
opts.on("-f", "--file FILE", "Specify configuration file (default: scaltainer.yml)") do |file|
opts.on("-f", "--conf-file FILE", "Specify configuration file (default: scaltainer.yml)") do |file|
configfile = file
end
opts.on("--state-file FILE", "Specify state file (default: <conf-file>.state)") do |file|
statefile = file
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!

statefile = "#{configfile}.state" unless statefile

raise ConfigurationError.new("File not found: #{configfile}") unless File.exists?(configfile)
logger = Logger.new(STDOUT)
logger.level = %w(debug info warn error fatal unknown).find_index((ENV['LOG_LEVEL'] || '').downcase) || 1

return configfile, logger
return configfile, statefile, logger
end
end
end
7 changes: 3 additions & 4 deletions lib/scaltainer/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Scaltainer
class Runner
def initialize(configfile, logger)
def initialize(configfile, statefile, logger)
@logger = logger
@default_service_config = {
"min" => 0,
Expand All @@ -11,11 +11,10 @@ def initialize(configfile, logger)
"upscale_sensitivity" => 1,
"downscale_sensitivity" => 1
}
@logger.debug "Scaltainer initialized with configuration file: #{configfile}"
@logger.debug "Scaltainer initialized with configuration file: #{configfile}, and state file: #{statefile}"
config = YAML.load_file configfile
Docker.logger = @logger
statefile = "#{configfile}.state"
state = get_state statefile || {}
state = get_state(statefile) || {}
endpoint = config["endpoint"]
service_prefix = config["stack_name"]
iterate_services config["web_services"], service_prefix, ServiceTypeWeb.new(endpoint), state
Expand Down

0 comments on commit fa216ee

Please sign in to comment.