Skip to content

Commit

Permalink
fix some leaks and add an error handling block
Browse files Browse the repository at this point in the history
  • Loading branch information
nickelser committed May 7, 2015
1 parent c81edbc commit a13f086
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.1.3

- Fix several memory leaks.
- Add a proper error handler block.

## 0.1.2

- Fix bug with setting `at` like "**:35".
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ r = Redis.new
Zhong.schedule(redis: r) do |s|
s.category "stuff" do
s.every(5.seconds, "foo") { puts "foo" }
s.every(1.minute, "biz", at: ["**:26", "**:27"]) { puts "biz" }
s.every(1.week, "baz", at: ["mon 22:45", "wed 23:13"]) { puts "baz" }
s.every(10.seconds, "boom") { raise "fail" }
end

s.category "clutter" do
Expand All @@ -39,8 +41,11 @@ Zhong.schedule(redis: r) do |s|
puts "dong"
true
end
end

s.error_handler do |e, job|
puts "damn, #{job} messed up: #{e}"
end
end
```

## TODO
Expand Down
2 changes: 0 additions & 2 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ set -euo pipefail
IFS=$'\n\t'

bundle install

# Do any other automated setup that you need to do here
15 changes: 13 additions & 2 deletions lib/zhong/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def run?(time = Time.now)
run_every?(time) && run_at?(time) && run_if?(time)
end

def run(time = Time.now)
def run(time = Time.now, error_handler = nil)
return unless run?(time)

if running?
Expand All @@ -58,7 +58,18 @@ def run(time = Time.now)

@logger.info "running: #{self}"

@thread = Thread.new { @block.call } if @block
if @block
@thread = Thread.new do
begin
@block.call
rescue => boom
@logger.error "#{self} failed: #{boom}"
error_handler.call(boom, self) if error_handler
end

nil # do not retain thread return value
end
end

ran!(time)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/zhong/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def error_handler(&block)
end

def on(event, &block)
fail "Unsupported callback #{event}" unless [:before_tick, :after_tick, :before_run, :after_run].include?(event.to_sym)
fail "unknown callback #{event}" unless [:before_tick, :after_tick, :before_run, :after_run].include?(event.to_sym)
(@callbacks[event.to_sym] ||= []) << block
end

Expand All @@ -56,7 +56,7 @@ def start

jobs.each do |_, job|
if fire_callbacks(:before_run, job, now)
job.run(now)
job.run(now, error_handler)
fire_callbacks(:after_run, job, now)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/zhong/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Zhong
VERSION = "0.1.2"
VERSION = "0.1.3"
end

0 comments on commit a13f086

Please sign in to comment.