From 5f2cc880c14a11d59d4faae7dd870ef36a023f6c Mon Sep 17 00:00:00 2001 From: "Pavlo V. Lysov" Date: Sun, 12 Feb 2012 13:58:36 +0200 Subject: [PATCH 1/4] Allows to configure the Clockwork module, adds configuration defaults, updates unit tests --- lib/clockwork.rb | 14 +++++++++++--- test/clockwork_test.rb | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/clockwork.rb b/lib/clockwork.rb index 889f19a..2fd1f95 100644 --- a/lib/clockwork.rb +++ b/lib/clockwork.rb @@ -1,6 +1,13 @@ +require 'logger' + module Clockwork @@events = [] + @@configuration = { :sleep_timeout => 1, :logger => Logger.new(STDOUT) } + + def configure + yield(@@configuration) + end class At class FailedToParse < StandardError; end; @@ -78,6 +85,7 @@ def run(t) def log_error(e) STDERR.puts exception_message(e) + Clockwork.send(:class_variable_get, :@@configuration)[:logger].warn(e) end def exception_message(e) @@ -121,12 +129,12 @@ def run log "Starting clock for #{@@events.size} events: [ " + @@events.map { |e| e.to_s }.join(' ') + " ]" loop do tick - sleep 1 + sleep @@configuration[:sleep_timeout] end end def log(msg) - puts msg + @@configuration[:logger].info(msg) end def tick(t=Time.now) @@ -135,7 +143,7 @@ def tick(t=Time.now) end to_run.each do |event| - log "Triggering #{event}" + log "Triggering '#{event}' at #{Time.now}" event.run(t) end diff --git a/test/clockwork_test.rb b/test/clockwork_test.rb index ddbe98f..8c49b28 100644 --- a/test/clockwork_test.rb +++ b/test/clockwork_test.rb @@ -169,4 +169,21 @@ def assert_wont_run(t) Clockwork.tick(t = Time.now) assert_equal t, event.last end + + test "should be configurable" do + Clockwork.configure do |config| + config[:sleep_timeout] = 100 + config[:dummy] = "dummy" + end + + underlying_var = Clockwork.send(:class_variable_get, :@@configuration) + assert_equal 100, underlying_var[:sleep_timeout] + assert_equal "dummy", underlying_var[:dummy] + end + + test "configuration defaults" do + underlying_var = Clockwork.send(:class_variable_get, :@@configuration) + assert_equal 1, underlying_var[:sleep_timeout] + assert underlying_var[:logger].is_a?(Logger) + end end From 21b10fadae8dc27aa4d811b103ab26e63eee1a05 Mon Sep 17 00:00:00 2001 From: "Pavlo V. Lysov" Date: Sun, 12 Feb 2012 21:30:16 +0200 Subject: [PATCH 2/4] Gets rid of private variable use for the config, uses a getter method instead. Makes the configuration itself a separate class backed with a Hash. Updates tests. --- lib/clockwork.rb | 31 +++++++++++++++++++++++++++---- test/clockwork_test.rb | 31 +++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/lib/clockwork.rb b/lib/clockwork.rb index 2fd1f95..30e4358 100644 --- a/lib/clockwork.rb +++ b/lib/clockwork.rb @@ -3,7 +3,6 @@ module Clockwork @@events = [] - @@configuration = { :sleep_timeout => 1, :logger => Logger.new(STDOUT) } def configure yield(@@configuration) @@ -85,7 +84,7 @@ def run(t) def log_error(e) STDERR.puts exception_message(e) - Clockwork.send(:class_variable_get, :@@configuration)[:logger].warn(e) + Clockwork.config.logger.error(e) end def exception_message(e) @@ -100,6 +99,30 @@ def exception_message(e) end end + class Configuration + def initialize(defaults = {}) + @backend = defaults.clone + end + + def method_missing(method, *params, &block) + if method.to_s =~ /^(.+)=/ + #setter method called + @backend[Regexp.last_match[1].to_sym] = params.first + else + #getter method called + @backend[method.to_sym] + end + end + end + + @@configuration = Configuration.new( + { :sleep_timeout => 1, :logger => Logger.new(STDOUT) } + ) + + def config + @@configuration + end + extend self def handler(&block) @@ -129,12 +152,12 @@ def run log "Starting clock for #{@@events.size} events: [ " + @@events.map { |e| e.to_s }.join(' ') + " ]" loop do tick - sleep @@configuration[:sleep_timeout] + sleep(config.sleep_timeout) end end def log(msg) - @@configuration[:logger].info(msg) + config.logger.info(msg) end def tick(t=Time.now) diff --git a/test/clockwork_test.rb b/test/clockwork_test.rb index 8c49b28..1dda39f 100644 --- a/test/clockwork_test.rb +++ b/test/clockwork_test.rb @@ -170,20 +170,31 @@ def assert_wont_run(t) assert_equal t, event.last end + test "Configuration instance creation: defaults" do + cfg = Clockwork::Configuration.new({:sleep_timeout => 100, :logger => "A Logger"}) + assert_equal 100, cfg.sleep_timeout + assert_equal "A Logger", cfg.logger + end + + test "Configuration, overriding defaults" do + cfg = Clockwork::Configuration.new({:sleep_timeout => 100}) + cfg.sleep_timeout = 200 + assert_equal 200, cfg.sleep_timeout + end + test "should be configurable" do Clockwork.configure do |config| - config[:sleep_timeout] = 100 - config[:dummy] = "dummy" + config.sleep_timeout = 200 + config.logger = "A Logger" + config.whatever = "It is supposed to illustrate that we can add arbitrary configurations" end + - underlying_var = Clockwork.send(:class_variable_get, :@@configuration) - assert_equal 100, underlying_var[:sleep_timeout] - assert_equal "dummy", underlying_var[:dummy] - end + assert Clockwork.config.is_a?(Clockwork::Configuration) - test "configuration defaults" do - underlying_var = Clockwork.send(:class_variable_get, :@@configuration) - assert_equal 1, underlying_var[:sleep_timeout] - assert underlying_var[:logger].is_a?(Logger) + assert_equal 200, Clockwork.config.sleep_timeout + assert_equal "A Logger", Clockwork.config.logger + assert_match /^It is supposed to/, Clockwork.config.whatever end + end From 848d7716fe96d5b03681ead82efbb430e479607f Mon Sep 17 00:00:00 2001 From: "Pavlo V. Lysov" Date: Mon, 13 Feb 2012 11:34:44 +0200 Subject: [PATCH 3/4] Simplifies the way we configure the Clockwork, gets rid of "whatever"-like configuration parameters, gets rid of sending error messages to STDERR, updates unit tests --- lib/clockwork.rb | 35 ++++++++--------------------------- test/clockwork_test.rb | 28 ++++++++-------------------- 2 files changed, 16 insertions(+), 47 deletions(-) diff --git a/lib/clockwork.rb b/lib/clockwork.rb index 30e4358..0436987 100644 --- a/lib/clockwork.rb +++ b/lib/clockwork.rb @@ -4,10 +4,6 @@ module Clockwork @@events = [] - def configure - yield(@@configuration) - end - class At class FailedToParse < StandardError; end; NOT_SPECIFIED = nil @@ -83,8 +79,7 @@ def run(t) end def log_error(e) - STDERR.puts exception_message(e) - Clockwork.config.logger.error(e) + Clockwork.config[:logger].error(e) end def exception_message(e) @@ -99,25 +94,11 @@ def exception_message(e) end end - class Configuration - def initialize(defaults = {}) - @backend = defaults.clone - end - - def method_missing(method, *params, &block) - if method.to_s =~ /^(.+)=/ - #setter method called - @backend[Regexp.last_match[1].to_sym] = params.first - else - #getter method called - @backend[method.to_sym] - end - end - end + @@configuration = { :sleep_timeout => 1, :logger => Logger.new(STDOUT) } - @@configuration = Configuration.new( - { :sleep_timeout => 1, :logger => Logger.new(STDOUT) } - ) + def configure + yield(config) + end def config @@configuration @@ -152,12 +133,12 @@ def run log "Starting clock for #{@@events.size} events: [ " + @@events.map { |e| e.to_s }.join(' ') + " ]" loop do tick - sleep(config.sleep_timeout) + sleep(config[:sleep_timeout]) end end def log(msg) - config.logger.info(msg) + config[:logger].info(msg) end def tick(t=Time.now) @@ -166,7 +147,7 @@ def tick(t=Time.now) end to_run.each do |event| - log "Triggering '#{event}' at #{Time.now}" + log "Triggering '#{event}'" event.run(t) end diff --git a/test/clockwork_test.rb b/test/clockwork_test.rb index 1dda39f..baa3348 100644 --- a/test/clockwork_test.rb +++ b/test/clockwork_test.rb @@ -170,31 +170,19 @@ def assert_wont_run(t) assert_equal t, event.last end - test "Configuration instance creation: defaults" do - cfg = Clockwork::Configuration.new({:sleep_timeout => 100, :logger => "A Logger"}) - assert_equal 100, cfg.sleep_timeout - assert_equal "A Logger", cfg.logger - end - - test "Configuration, overriding defaults" do - cfg = Clockwork::Configuration.new({:sleep_timeout => 100}) - cfg.sleep_timeout = 200 - assert_equal 200, cfg.sleep_timeout - end - test "should be configurable" do Clockwork.configure do |config| - config.sleep_timeout = 200 - config.logger = "A Logger" - config.whatever = "It is supposed to illustrate that we can add arbitrary configurations" + config[:sleep_timeout] = 200 + config[:logger] = "A Logger" end - - assert Clockwork.config.is_a?(Clockwork::Configuration) + assert_equal 200, Clockwork.config[:sleep_timeout] + assert_equal "A Logger", Clockwork.config[:logger] + end - assert_equal 200, Clockwork.config.sleep_timeout - assert_equal "A Logger", Clockwork.config.logger - assert_match /^It is supposed to/, Clockwork.config.whatever + test "configuration should have reasonable defaults" do + assert_equal 1, Clockwork.config[:sleep_timeout] + assert Clockwork.config[:logger].is_a?(Logger) end end From e8a1ae16d35a474ab3c561f1d632a1911d724331 Mon Sep 17 00:00:00 2001 From: "Pavlo V. Lysov" Date: Fri, 24 Feb 2012 15:27:32 +0200 Subject: [PATCH 4/4] adds Configuration section to the README file --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 4524ca4..fb1337f 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,29 @@ You can set more than one timing: # send reminders at noon and evening +Configuration +----------------------- + +Clockwork exposes a couple of configuration options you may change: + +### :logger + +By default Clockwork logs to STDOUT. In case you prefer to make it to use our +own logger implementation you have to specify the `logger` configuration option. See example below. + +### :sleep_timeout + +Clockwork wakes up once a second (by default) and performs its duties. If that +is the rare case you need to tweak the number of seconds it sleeps then you have +the `sleep_timeout` configuration option to set like shown below. + +### Configuration example + + Clockwork.configure do |config| + config[:sleep_timeout] = 5 + config[:logger] = Logger.new(log_file_path) + end + Anatomy of a clock file -----------------------