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 ----------------------- diff --git a/lib/clockwork.rb b/lib/clockwork.rb index 889f19a..0436987 100644 --- a/lib/clockwork.rb +++ b/lib/clockwork.rb @@ -1,3 +1,5 @@ +require 'logger' + module Clockwork @@events = [] @@ -77,7 +79,7 @@ def run(t) end def log_error(e) - STDERR.puts exception_message(e) + Clockwork.config[:logger].error(e) end def exception_message(e) @@ -92,6 +94,16 @@ def exception_message(e) end end + @@configuration = { :sleep_timeout => 1, :logger => Logger.new(STDOUT) } + + def configure + yield(config) + end + + def config + @@configuration + end + extend self def handler(&block) @@ -121,12 +133,12 @@ def run log "Starting clock for #{@@events.size} events: [ " + @@events.map { |e| e.to_s }.join(' ') + " ]" loop do tick - sleep 1 + sleep(config[:sleep_timeout]) end end def log(msg) - puts msg + config[:logger].info(msg) end def tick(t=Time.now) @@ -135,7 +147,7 @@ def tick(t=Time.now) end to_run.each do |event| - log "Triggering #{event}" + log "Triggering '#{event}'" event.run(t) end diff --git a/test/clockwork_test.rb b/test/clockwork_test.rb index ddbe98f..baa3348 100644 --- a/test/clockwork_test.rb +++ b/test/clockwork_test.rb @@ -169,4 +169,20 @@ 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] = 200 + config[:logger] = "A Logger" + end + + assert_equal 200, Clockwork.config[:sleep_timeout] + assert_equal "A Logger", Clockwork.config[:logger] + end + + test "configuration should have reasonable defaults" do + assert_equal 1, Clockwork.config[:sleep_timeout] + assert Clockwork.config[:logger].is_a?(Logger) + end + end