mirror of
https://github.com/plashchynski/crono.git
synced 2026-04-25 10:18:49 +02:00
Add Job#healthy
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user