Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

job runs now log their started/finished_at times, and that time is retrievable with the API #161

Merged
merged 2 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions spec/mosquito/api/job_run_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
require "../../spec_helper"

describe Mosquito::Api::JobRun do
# the job run timestamps are stored as a unix epoch with millis, so nanosecond precision is lost.
def at_beginning_of_millisecond(time)
time - (time.nanosecond.nanoseconds) + (time.millisecond.milliseconds)
end

getter job : QueuedTestJob { QueuedTestJob.new }
getter job_run : Mosquito::JobRun { job.build_job_run }
getter api : Mosquito::Api::JobRun { Mosquito::Api::JobRun.new job_run.id }
Expand Down Expand Up @@ -33,13 +38,34 @@ describe Mosquito::Api::JobRun do
job_run.store
end

# the enqueue time is stored as a unix epoch with millis, so nanosecond precision is lost.
expected_time = now - (now.nanosecond.nanoseconds) + (now.millisecond.milliseconds)
expected_time = at_beginning_of_millisecond now
assert_equal expected_time, api.enqueue_time
end

it "can retrieve the retry count" do
job_run.store
assert_equal 0, api.retry_count
end

it "can retrieve the started at timestamp" do
now = at_beginning_of_millisecond Time.utc
job_run = create_job_run
Timecop.freeze now do
job_run.run
end

api = Mosquito::Api::JobRun.new(job_run.id)
assert_equal now, api.started_at
end

it "can retrieve the finished_at timestamp" do
now = at_beginning_of_millisecond Time.utc
job_run = create_job_run
Timecop.freeze now do
job_run.run
end

api = Mosquito::Api::JobRun.new(job_run.id)
assert_equal now, api.finished_at
end
end
2 changes: 1 addition & 1 deletion spec/mosquito/runners/overseer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe "Mosquito::Runners::Overseer" do
assert_in_epsilon(
overseer.idle_wait.total_seconds,
tick_time.total_seconds,
epsilon: 0.05
epsilon: 0.06
)
end
end
Expand Down
14 changes: 13 additions & 1 deletion src/mosquito/api/job_run.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Mosquito::Api

def runtime_parameters : Hash(String, String)
config.reject do |key, _|
["id", "type", "enqueue_time", "retry_count"].includes? key
["id", "type", "enqueue_time", "retry_count", "started_at", "finished_at"].includes? key
end
end

Expand All @@ -33,6 +33,18 @@ module Mosquito::Api
Time.unix_ms config["enqueue_time"].to_i64
end

def started_at : Time?
if time = config["started_at"]
Time.unix_ms time.to_i64
end
end

def finished_at : Time?
if time = config["finished_at"]
Time.unix_ms time.to_i64
end
end

# The number of times this job has been retried.
def retry_count : Int
config["retry_count"].to_i
Expand Down
15 changes: 14 additions & 1 deletion src/mosquito/job_run.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module Mosquito
getter id : String
getter retry_count = 0
getter job : Mosquito::Job?
@started_at : Time?
@finished_at : Time?

def job! : Mosquito::Job
job || raise RuntimeError.new("No job yet retrieved for job_run.")
Expand Down Expand Up @@ -57,6 +59,14 @@ module Mosquito
fields["type"] = type
fields["retry_count"] = retry_count.to_s

if started_at_ = @started_at
fields["started_at"] = started_at_.to_unix_ms.to_s
end

if finished_at_ = @finished_at
fields["finished_at"] = finished_at_.to_unix_ms.to_s
end

Mosquito.backend.store config_key, fields
end

Expand Down Expand Up @@ -86,12 +96,15 @@ module Mosquito
# Builds and runs the job with this job_run config.
def run
instance = build_job

@started_at = Time.utc
instance.run
@finished_at = Time.utc

if executed? && failed?
@retry_count += 1
store
end
store
end

# Fails this job run and make sure it's persisted as such.
Expand Down