forked from joker1007/crono_trigger
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request joker1007#13 from joker1007/add-execution
Add CronoTrigger::Models::Execution model for tracking execution
- Loading branch information
Showing
18 changed files
with
463 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module CronoTrigger | ||
class ExecutionTracker | ||
def initialize(schedulable) | ||
@schedulable = schedulable | ||
end | ||
|
||
def track(&pr) | ||
if @schedulable.track_execution | ||
begin | ||
execution = @schedulable.crono_trigger_executions.create_with_timestamp! | ||
pr.call | ||
execution.complete! | ||
rescue => e | ||
execution.error!(e) | ||
raise | ||
end | ||
else | ||
pr.call | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module CronoTrigger | ||
module Models | ||
class Execution < ActiveRecord::Base | ||
self.table_name = "crono_trigger_executions" | ||
|
||
belongs_to :schedule, polymorphic: true, inverse_of: :crono_trigger_executions | ||
|
||
scope :recently, ->(schedule_type:) { where(schedule_type: schedule_type).order(executed_at: :desc) } | ||
|
||
enum status: { | ||
executing: "executing", | ||
completed: "completed", | ||
failed: "failed", | ||
} | ||
|
||
def self.create_with_timestamp! | ||
create!(executed_at: Time.current, status: :executing, worker_id: CronoTrigger.config.worker_id) | ||
end | ||
|
||
def complete! | ||
update!(status: :completed, completed_at: Time.current) | ||
end | ||
|
||
def error!(exception) | ||
update!(status: :failed, completed_at: Time.current, error_name: exception.class.to_s, error_reason: exception.message) | ||
end | ||
|
||
def retry! | ||
return false if schedule.locking? | ||
|
||
schedule.retry! | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe CronoTrigger::Models::Execution do | ||
let(:notification) do | ||
Notification.create!( | ||
name: "notification1", | ||
cron: "0,30 * * * *", | ||
started_at: Time.current, | ||
).tap(&:activate_schedule!) | ||
end | ||
|
||
describe ".create_with_timestamp", aggregate_failures: true do | ||
subject { notification.crono_trigger_executions.create_with_timestamp! } | ||
|
||
it "creates record with current time as executed_at" do | ||
time = Time.utc(2017, 6, 18, 1, 0) | ||
Timecop.freeze(time) do | ||
is_expected.to be_persisted | ||
expect(subject.executed_at).to eq(time) | ||
expect(subject.status).to eq("executing") | ||
expect(subject.worker_id).to eq(CronoTrigger.config.worker_id) | ||
end | ||
end | ||
end | ||
|
||
describe "#complete!" do | ||
let(:execution) { notification.crono_trigger_executions.create_with_timestamp! } | ||
|
||
it "update (status = completed, completed_at = now)" do | ||
time1 = Time.utc(2017, 6, 18, 1, 0) | ||
Timecop.freeze(time1) do | ||
execution | ||
end | ||
|
||
time2 = Time.utc(2017, 6, 18, 2, 0) | ||
Timecop.freeze(time2) do | ||
execution.complete! | ||
end | ||
|
||
expect(execution.completed_at).to eq(time2) | ||
expect(execution.status).to eq("completed") | ||
end | ||
end | ||
|
||
describe "#error!" do | ||
let(:execution) { notification.crono_trigger_executions.create_with_timestamp! } | ||
|
||
it "update (status = failed, error_name = ex.class_name, error_reason = ex.message)" do | ||
time1 = Time.utc(2017, 6, 18, 1, 0) | ||
Timecop.freeze(time1) do | ||
execution | ||
end | ||
|
||
time2 = Time.utc(2017, 6, 18, 2, 0) | ||
Timecop.freeze(time2) do | ||
execution.error!(RuntimeError.new("failed!!")) | ||
end | ||
|
||
expect(execution.completed_at).to eq(time2) | ||
expect(execution.status).to eq("failed") | ||
expect(execution.error_name).to eq("RuntimeError") | ||
expect(execution.error_reason).to eq("failed!!") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.