diff --git a/lib/crono/job.rb b/lib/crono/job.rb index f0a4755..f2c8184 100644 --- a/lib/crono/job.rb +++ b/lib/crono/job.rb @@ -10,6 +10,7 @@ module Crono attr_accessor :last_performed_at attr_accessor :job_log attr_accessor :job_logger + attr_accessor :healthy def initialize(performer, period) self.performer, self.period = performer, period @@ -41,7 +42,9 @@ module Crono 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 @@ -54,7 +57,7 @@ module Crono log = model.reload.log || "" log << job_log.string job_log.truncate(job_log.rewind) - model.update(last_performed_at: last_performed_at, log: log) + model.update(last_performed_at: last_performed_at, log: log, healthy: healthy) end end diff --git a/lib/generators/crono/install/templates/migrations/create_crono_jobs.rb b/lib/generators/crono/install/templates/migrations/create_crono_jobs.rb index 476d295..b7d9543 100644 --- a/lib/generators/crono/install/templates/migrations/create_crono_jobs.rb +++ b/lib/generators/crono/install/templates/migrations/create_crono_jobs.rb @@ -4,6 +4,7 @@ class CreateCronoJobs < ActiveRecord::Migration t.string :job_id, null: false t.text :log t.datetime :last_performed_at + t.boolean :healthy t.timestamps null: false end add_index :crono_jobs, [:job_id], unique: true diff --git a/spec/job_spec.rb b/spec/job_spec.rb index 28e1cd7..2953d79 100644 --- a/spec/job_spec.rb +++ b/spec/job_spec.rb @@ -21,11 +21,12 @@ describe Crono::Job do end describe "#perform" do + after { job.send(:model).destroy } + 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 save performin errors to log" do @@ -34,6 +35,16 @@ describe Crono::Job do saved_log = Crono::CronoJob.find_by(job_id: failing_job.job_id).log expect(saved_log).to include "Some error" end + + it "should set Job#healthy to true if perform ok" do + thread = job.perform.join + expect(job.healthy).to be true + end + + it "should set Job#healthy to false if perform with error" do + thread = failing_job.perform.join + expect(failing_job.healthy).to be false + end end describe "#description" do @@ -51,9 +62,11 @@ describe Crono::Job do it "should update saved job" do job.last_performed_at = Time.now + job.healthy = true job.save @crono_job = Crono::CronoJob.find_by(job_id: job.job_id) expect(@crono_job.last_performed_at.utc.to_s).to be_eql job.last_performed_at.utc.to_s + expect(@crono_job.healthy).to be true end it "should save and truncate job log" do diff --git a/spec/period_spec.rb b/spec/period_spec.rb index 699ab45..ed4c73e 100644 --- a/spec/period_spec.rb +++ b/spec/period_spec.rb @@ -49,7 +49,7 @@ describe Crono::Period do time = 10.minutes.from_now at = {hour: time.hour, min: time.min} @period = Crono::Period.new(2.day, at: at) - expect(@period.next).to be_eql(Time.now.change(at)) + expect(@period.next.utc.to_s).to be_eql(Time.now.change(at).utc.to_s) end end end