From f43ae4b1b1fa5b01c446f7863e5f1db373aa2875 Mon Sep 17 00:00:00 2001 From: Dzmitry Plashchynski Date: Sat, 7 Mar 2015 18:05:47 +0200 Subject: [PATCH] Save performing errors to DB --- lib/crono/cli.rb | 1 - lib/crono/job.rb | 14 +++++++++++--- spec/job_spec.rb | 22 ++++++++++++++++------ 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/lib/crono/cli.rb b/lib/crono/cli.rb index f1f90cc..362cfd7 100644 --- a/lib/crono/cli.rb +++ b/lib/crono/cli.rb @@ -75,7 +75,6 @@ module Crono end def start_working_loop - Thread.abort_on_exception = true while job = Crono.scheduler.next do sleep(job.next - Time.now) job.perform diff --git a/lib/crono/job.rb b/lib/crono/job.rb index 08c491a..f0a4755 100644 --- a/lib/crono/job.rb +++ b/lib/crono/job.rb @@ -34,10 +34,18 @@ module Crono def perform log "Perform #{performer}" self.last_performed_at = Time.now - save + Thread.new do - performer.new.perform - log "Finished #{performer} in %.2f seconds" % (Time.now - last_performed_at) + 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") + else + log "Finished #{performer} in %.2f seconds" % (Time.now - last_performed_at) + ensure + save + end end end diff --git a/spec/job_spec.rb b/spec/job_spec.rb index 6719488..28e1cd7 100644 --- a/spec/job_spec.rb +++ b/spec/job_spec.rb @@ -4,9 +4,16 @@ class TestJob def perform;end end +class TestFailingJob + def perform + raise "Some error" + end +end + describe Crono::Job do let(:period) { Crono::Period.new(2.day) } let(:job) { Crono::Job.new(TestJob, period) } + let(:failing_job) { Crono::Job.new(TestFailingJob, period) } it "should contain performer and period" do expect(job.performer).to be TestJob @@ -15,14 +22,17 @@ describe Crono::Job do describe "#perform" do it "should run performer in separate thread" do + expect(job).to receive(:save) thread = job.perform.join expect(thread).to be_stop + job.send(:model).destroy end - it "should call Job#save during perform" do - expect(job).to receive(:save) - job.perform.join - job.send(:model).destroy + it "should save performin errors to log" do + thread = failing_job.perform.join + expect(thread).to be_stop + saved_log = Crono::CronoJob.find_by(job_id: failing_job.job_id).log + expect(saved_log).to include "Some error" end end @@ -43,7 +53,7 @@ describe Crono::Job do job.last_performed_at = Time.now job.save @crono_job = Crono::CronoJob.find_by(job_id: job.job_id) - expect(@crono_job.last_performed_at.utc).to be_eql job.last_performed_at.utc + expect(@crono_job.last_performed_at.utc.to_s).to be_eql job.last_performed_at.utc.to_s end it "should save and truncate job log" do @@ -64,7 +74,7 @@ describe Crono::Job do it "should load last_performed_at from DB" do @job = Crono::Job.new(TestJob, period) @job.load - expect(@job.last_performed_at.utc).to be_eql @saved_last_performed_at.utc + expect(@job.last_performed_at.utc.to_s).to be_eql @saved_last_performed_at.utc.to_s end end