mirror of
https://github.com/plashchynski/crono.git
synced 2026-04-22 08:48:38 +02:00
Add Job#healthy
This commit is contained in:
@@ -10,6 +10,7 @@ module Crono
|
|||||||
attr_accessor :last_performed_at
|
attr_accessor :last_performed_at
|
||||||
attr_accessor :job_log
|
attr_accessor :job_log
|
||||||
attr_accessor :job_logger
|
attr_accessor :job_logger
|
||||||
|
attr_accessor :healthy
|
||||||
|
|
||||||
def initialize(performer, period)
|
def initialize(performer, period)
|
||||||
self.performer, self.period = performer, period
|
self.performer, self.period = performer, period
|
||||||
@@ -41,7 +42,9 @@ module Crono
|
|||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
log "Finished #{performer} in %.2f seconds with error: #{e.message}" % (Time.now - last_performed_at)
|
log "Finished #{performer} in %.2f seconds with error: #{e.message}" % (Time.now - last_performed_at)
|
||||||
log e.backtrace.join("\n")
|
log e.backtrace.join("\n")
|
||||||
|
self.healthy = false
|
||||||
else
|
else
|
||||||
|
self.healthy = true
|
||||||
log "Finished #{performer} in %.2f seconds" % (Time.now - last_performed_at)
|
log "Finished #{performer} in %.2f seconds" % (Time.now - last_performed_at)
|
||||||
ensure
|
ensure
|
||||||
save
|
save
|
||||||
@@ -54,7 +57,7 @@ module Crono
|
|||||||
log = model.reload.log || ""
|
log = model.reload.log || ""
|
||||||
log << job_log.string
|
log << job_log.string
|
||||||
job_log.truncate(job_log.rewind)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ class CreateCronoJobs < ActiveRecord::Migration
|
|||||||
t.string :job_id, null: false
|
t.string :job_id, null: false
|
||||||
t.text :log
|
t.text :log
|
||||||
t.datetime :last_performed_at
|
t.datetime :last_performed_at
|
||||||
|
t.boolean :healthy
|
||||||
t.timestamps null: false
|
t.timestamps null: false
|
||||||
end
|
end
|
||||||
add_index :crono_jobs, [:job_id], unique: true
|
add_index :crono_jobs, [:job_id], unique: true
|
||||||
|
|||||||
@@ -21,11 +21,12 @@ describe Crono::Job do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "#perform" do
|
describe "#perform" do
|
||||||
|
after { job.send(:model).destroy }
|
||||||
|
|
||||||
it "should run performer in separate thread" do
|
it "should run performer in separate thread" do
|
||||||
expect(job).to receive(:save)
|
expect(job).to receive(:save)
|
||||||
thread = job.perform.join
|
thread = job.perform.join
|
||||||
expect(thread).to be_stop
|
expect(thread).to be_stop
|
||||||
job.send(:model).destroy
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should save performin errors to log" do
|
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
|
saved_log = Crono::CronoJob.find_by(job_id: failing_job.job_id).log
|
||||||
expect(saved_log).to include "Some error"
|
expect(saved_log).to include "Some error"
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe "#description" do
|
describe "#description" do
|
||||||
@@ -51,9 +62,11 @@ describe Crono::Job do
|
|||||||
|
|
||||||
it "should update saved job" do
|
it "should update saved job" do
|
||||||
job.last_performed_at = Time.now
|
job.last_performed_at = Time.now
|
||||||
|
job.healthy = true
|
||||||
job.save
|
job.save
|
||||||
@crono_job = Crono::CronoJob.find_by(job_id: job.job_id)
|
@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.last_performed_at.utc.to_s).to be_eql job.last_performed_at.utc.to_s
|
||||||
|
expect(@crono_job.healthy).to be true
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should save and truncate job log" do
|
it "should save and truncate job log" do
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ describe Crono::Period do
|
|||||||
time = 10.minutes.from_now
|
time = 10.minutes.from_now
|
||||||
at = {hour: time.hour, min: time.min}
|
at = {hour: time.hour, min: time.min}
|
||||||
@period = Crono::Period.new(2.day, at: at)
|
@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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user