From 46fe2548d97ce57f931861a4dc512ba8405698b6 Mon Sep 17 00:00:00 2001 From: Dzmitry Plashchynski Date: Tue, 10 Mar 2015 23:46:07 +0200 Subject: [PATCH] Refactoring --- README.md | 4 ++-- lib/crono/job.rb | 30 ++++++++++++++++-------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c3ed3c6..92f024e 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Crono is a time-based background job scheduler daemon (just like Cron) for Ruby ## The Idea -Currently there is no such thing as Cron in Ruby for Rails. Well, there's [Whenever](https://github.com/javan/whenever) but it works on top of Unix Cron, so you have no total control of it from Ruby. Crono is pure Ruby. It doesn't use Unix Cron and other platform-dependent things. So you can use it on all platforms supported by Ruby. It persists job state to your database using Active Record. You have full control of jobs performing process. You have Ruby code, so you can understand and modify it to fit your needs. +Currently there is no such thing as Ruby Cron for Rails. Well, there's [Whenever](https://github.com/javan/whenever) but it works on top of Unix Cron, so you have no total control of it from Ruby. Crono is pure Ruby. It doesn't use Unix Cron and other platform-dependent things. So you can use it on all platforms supported by Ruby. It persists job state to your database using Active Record. You have full control of jobs performing process. You have Ruby code, so you can understand and modify it to fit your needs. ![Web UI](https://github.com/plashchynski/crono/raw/master/examples/crono_web_ui.png) @@ -26,7 +26,7 @@ Other versions are untested but might work fine. Add the following line to your application's Gemfile: ```ruby -gem 'crono' +gem 'crono', require: false ``` Run the `bundle` command to install it. diff --git a/lib/crono/job.rb b/lib/crono/job.rb index 0eb96e7..10a8ab4 100644 --- a/lib/crono/job.rb +++ b/lib/crono/job.rb @@ -31,20 +31,7 @@ module Crono log "Perform #{performer}" self.last_performed_at = Time.now - Thread.new do - begin - performer.new.perform - rescue Exception => e - log "Finished #{performer} in %.2f seconds with error: #{e.message}" % (Time.now - last_performed_at) - log e.backtrace.join("\n") - self.healthy = false - else - self.healthy = true - log "Finished #{performer} in %.2f seconds" % (Time.now - last_performed_at) - ensure - save - end - end + Thread.new { perform_job } end def save @@ -61,6 +48,21 @@ module Crono end private + def perform_job + begin + performer.new.perform + rescue Exception => e + log "Finished #{performer} in %.2f seconds with error: #{e.message}" % (Time.now - last_performed_at) + log e.backtrace.join("\n") + self.healthy = false + else + self.healthy = true + log "Finished #{performer} in %.2f seconds" % (Time.now - last_performed_at) + ensure + save + end + end + def log(message) @semaphore.synchronize do logger.info message