Skip to content

Commit

Permalink
Add -w option for repeated runs
Browse files Browse the repository at this point in the history
  • Loading branch information
hammady committed Nov 26, 2017
1 parent 48f1d81 commit 29727a0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[![Build Status](https://travis-ci.org/hammady/scaltainer.svg?branch=master)](https://travis-ci.org/hammady/scaltainer)
[![Coverage Status](https://coveralls.io/repos/github/hammady/scaltainer/badge.svg?service=github&branch=master)](https://coveralls.io/github/hammady/scaltainer?branch=master)
[![Gem Version](https://badge.fury.io/rb/scaltainer.svg)](https://badge.fury.io/rb/scaltainer)

# Scaltainer

Expand Down Expand Up @@ -41,7 +42,12 @@ 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.
Typically one would want to repeatedly call scaltainer every minute or so. To do this
specify the wait time between repetitions using the `-w` parameter in seconds:

scaltainer -w 60

This will repeatedly call scaltainer every 60 seconds, sleeping in between.

## Configuration

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, statefile, logger = Scaltainer::Command.parse ARGV
Scaltainer::Runner.new configfile, statefile, logger
configfile, statefile, logger, wait = Scaltainer::Command.parse ARGV
Scaltainer::Runner.new configfile, statefile, logger, wait
rescue => e
$stderr.puts e.message
$stderr.puts e.backtrace
Expand Down
7 changes: 5 additions & 2 deletions lib/scaltainer/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module Scaltainer
class Command
def self.parse(args)
configfile, statefile = 'scaltainer.yml', nil
configfile, statefile, wait = 'scaltainer.yml', nil, 0
OptionParser.new do |opts|
opts.banner = "Usage: scaltainer [options]"
opts.on("-f", "--conf-file FILE", "Specify configuration file (default: scaltainer.yml)") do |file|
Expand All @@ -13,6 +13,9 @@ def self.parse(args)
opts.on("--state-file FILE", "Specify state file (default: <conf-file>.state)") do |file|
statefile = file
end
opts.on("-w", "--wait SECONDS", "Specify wait time between repeated calls, 0 for no repetition (default: 0)") do |w|
wait = w.to_i
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
puts "\nEnvironment variables: \n"
Expand All @@ -35,7 +38,7 @@ def self.parse(args)
logger = Logger.new(STDOUT)
logger.level = %w(debug info warn error fatal unknown).find_index((ENV['LOG_LEVEL'] || '').downcase) || 1

return configfile, statefile, logger
return configfile, statefile, logger, wait
end

private
Expand Down
20 changes: 15 additions & 5 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, statefile, logger)
def initialize(configfile, statefile, logger, wait)
@logger = logger
@default_service_config = {
"min" => 0,
Expand All @@ -16,14 +16,24 @@ def initialize(configfile, statefile, logger)
Docker.logger = @logger
state = get_state(statefile) || {}
endpoint = config["endpoint"]
service_prefix = config["stack_name"]
iterate_services config["web_services"], service_prefix, ServiceTypeWeb.new(endpoint), state
iterate_services config["worker_services"], service_prefix, ServiceTypeWorker.new(endpoint), state
save_state statefile, state
service_type_web = ServiceTypeWeb.new(endpoint)
service_type_worker = ServiceTypeWorker.new(endpoint)
loop do
run config, state, service_type_web, service_type_worker
save_state statefile, state
sleep wait
break if wait == 0
end
end

private

def run(config, state, service_type_web, service_type_worker)
service_prefix = config["stack_name"]
iterate_services config["web_services"], service_prefix, service_type_web, state
iterate_services config["worker_services"], service_prefix, service_type_worker, state
end

def get_state(statefile)
YAML.load_file statefile if File.exists? statefile
end
Expand Down
2 changes: 1 addition & 1 deletion lib/scaltainer/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Scaltainer
VERSION = "0.1.3"
VERSION = "0.1.4"
end

0 comments on commit 29727a0

Please sign in to comment.