Skip to content

Commit

Permalink
Rename perform_expected_at to simpler scheduled_at
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon committed Apr 20, 2023
1 parent c35586b commit 526f9b5
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 33 deletions.
19 changes: 4 additions & 15 deletions app/models/good_job/discrete_execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@ def number

# Time between when this job was expected to run and when it started running
def queue_latency
now = Time.zone.now
actual_start = performed_at || finished_at || now

actual_start - perform_expected_at unless perform_expected_at >= now
created_at - scheduled_at
end

# Time between when this job started and finished
def runtime_latency
(finished_at || Time.zone.now) - performed_at if performed_at
(finished_at || Time.current) - performed_at if performed_at
end

def last_status_at
finished_at || performed_at || perform_expected_at || created_at
finished_at || created_at
end

def status
Expand All @@ -41,16 +38,8 @@ def status
else
:succeeded
end
elsif (perform_expected_at || created_at) > DateTime.current
if serialized_params.fetch('executions', 0) > 1
:retried
else
:scheduled
end
elsif performed_at.present?
:running
else
:queued
:running
end
end

Expand Down
9 changes: 5 additions & 4 deletions app/models/good_job/execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ def self.enqueue(active_job, scheduled_at: nil, create_with_advisory_lock: false
if current_execution.discrete?
execution = current_execution
execution.assign_attributes(enqueue_args(active_job, { scheduled_at: scheduled_at }))
execution.scheduled_at ||= Time.current
execution.performed_at = nil
execution.finished_at = nil
else
execution = build_for_enqueue(active_job, { scheduled_at: scheduled_at })
Expand Down Expand Up @@ -376,8 +378,8 @@ def perform
if discrete?
transaction do
now = Time.current
discrete_execution = discrete_executions.create!(serialized_params: serialized_params, perform_expected_at: scheduled_at || created_at, created_at: now)
update!(performed_at: now, executions_count: (executions_count || 0) + 1)
discrete_execution = discrete_executions.create!(serialized_params: serialized_params, scheduled_at: (scheduled_at || created_at), created_at: now)
update!(performed_at: now, executions_count: ((executions_count || 0) + 1))
end
else
update!(performed_at: Time.current)
Expand Down Expand Up @@ -419,8 +421,7 @@ def perform
if discrete_execution
transaction do
discrete_execution.update!(finished_at: Time.current)
self.performed_at = nil # TODO: will this break something to unset this?
save!
update!(performed_at: nil, finished_at: nil, retried_good_job_id: nil)
end
else
save!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class CreateGoodJobs < ActiveRecord::Migration<%= migration_version %>
create_table :good_job_executions, id: :uuid do |t|
t.timestamps

t.uuid :active_job_id
t.uuid :active_job_id, null: false
t.jsonb :serialized_params
t.datetime :perform_expected_at
t.datetime :scheduled_at
t.datetime :finished_at
t.text :error
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class CreateGoodJobExecutions < ActiveRecord::Migration<%= migration_version %>
create_table :good_job_executions, id: :uuid do |t|
t.timestamps

t.uuid :active_job_id
t.uuid :active_job_id, null: false
t.jsonb :serialized_params
t.datetime :perform_expected_at
t.datetime :scheduled_at
t.datetime :finished_at
t.text :error

Expand Down
15 changes: 10 additions & 5 deletions spec/app/models/good_job/execution_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -666,15 +666,20 @@ def job_params
good_job.update!(is_discrete: true)
end

it 'creates a DiscreteExecution record' do
it 'updates the Execution record and creates a DiscreteExecution record' do
good_job.perform

expect(good_job.reload).to have_attributes(
executions_count: 1,
finished_at: within(1.second).of(Time.current)
)

dexecution = good_job.discrete_executions.first
expect(dexecution).to be_present
expect(dexecution).to have_attributes(
active_job_id: good_job.active_job_id,
created_at: within(0.001).of(good_job.performed_at),
perform_expected_at: within(0.001).of(good_job.created_at),
scheduled_at: within(0.001).of(good_job.created_at),
finished_at: within(1.second).of(Time.current),
error: nil,
serialized_params: good_job.serialized_params
Expand All @@ -696,10 +701,10 @@ def job_params
expect { good_job.perform }
.to not_change(described_class, :count)
.and change { good_job.reload.serialized_params["executions"] }.by(1)
.and not_change { good_job.reload.id }
.and not_change { good_job.reload.id }
.and not_change { described_class.count }

expect(good_job).to have_attributes(
expect(good_job.reload).to have_attributes(
error: "TestJob::ExpectedError: Raised expected error",
created_at: within(1.second).of(Time.current),
performed_at: nil,
Expand All @@ -712,7 +717,7 @@ def job_params
active_job_id: good_job.active_job_id,
error: "TestJob::ExpectedError: Raised expected error",
created_at: within(1.second).of(Time.current),
perform_expected_at: within(1.second).of(Time.current),
scheduled_at: within(1.second).of(Time.current),
finished_at: within(1.second).of(Time.current)
)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/app/models/good_job/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
).tap do |job|
job.discrete_executions.create!(
perform_expected_at: 1.minute.ago,
scheduled_at: 1.minute.ago,
created_at: 1.minute.ago,
finished_at: 1.minute.ago,
error: "TestJob::Error: TestJob::Error"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def change
create_table :good_job_executions, id: :uuid do |t|
t.timestamps

t.uuid :active_job_id
t.uuid :active_job_id, null: false
t.jsonb :serialized_params
t.datetime :perform_expected_at
t.datetime :scheduled_at
t.datetime :finished_at
t.text :error

Expand Down
4 changes: 2 additions & 2 deletions spec/test_app/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
create_table "good_job_executions", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.uuid "active_job_id"
t.uuid "active_job_id", null: false
t.jsonb "serialized_params"
t.datetime "perform_expected_at"
t.datetime "scheduled_at"
t.datetime "finished_at"
t.text "error"
t.index ["active_job_id", "created_at"], name: "index_good_job_executions_on_active_job_id_and_created_at"
Expand Down

0 comments on commit 526f9b5

Please sign in to comment.