Skip to content

Commit

Permalink
Merge pull request adamwiggins#9 from cleverua/configuration
Browse files Browse the repository at this point in the history
Add configuration options
  • Loading branch information
tomykaira committed Feb 25, 2012
2 parents f86366b + e8a1ae1 commit 8cffcad
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------------------

Expand Down
20 changes: 16 additions & 4 deletions lib/clockwork.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'logger'

module Clockwork

@@events = []
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down
16 changes: 16 additions & 0 deletions test/clockwork_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8cffcad

Please sign in to comment.