From 29727a0fc13a7c540f047008b77820d9aeebcff2 Mon Sep 17 00:00:00 2001 From: Hossam Hammady Date: Sun, 26 Nov 2017 10:33:44 +0300 Subject: [PATCH] Add -w option for repeated runs --- README.md | 8 +++++++- exe/scaltainer | 4 ++-- lib/scaltainer/command.rb | 7 +++++-- lib/scaltainer/runner.rb | 20 +++++++++++++++----- lib/scaltainer/version.rb | 2 +- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 8fc1f61..1565e21 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/exe/scaltainer b/exe/scaltainer index ca03b5f..fcb6e9a 100755 --- a/exe/scaltainer +++ b/exe/scaltainer @@ -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 diff --git a/lib/scaltainer/command.rb b/lib/scaltainer/command.rb index 0852197..5ee7d3c 100644 --- a/lib/scaltainer/command.rb +++ b/lib/scaltainer/command.rb @@ -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| @@ -13,6 +13,9 @@ def self.parse(args) opts.on("--state-file FILE", "Specify state file (default: .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" @@ -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 diff --git a/lib/scaltainer/runner.rb b/lib/scaltainer/runner.rb index 4799394..ee32806 100644 --- a/lib/scaltainer/runner.rb +++ b/lib/scaltainer/runner.rb @@ -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, @@ -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 diff --git a/lib/scaltainer/version.rb b/lib/scaltainer/version.rb index b96dd0d..1ead7e1 100644 --- a/lib/scaltainer/version.rb +++ b/lib/scaltainer/version.rb @@ -1,3 +1,3 @@ module Scaltainer - VERSION = "0.1.3" + VERSION = "0.1.4" end