Skip to content

Commit

Permalink
Have GoodJob::Execution shadow GoodJob::DiscreteExecution
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon committed Jul 6, 2024
1 parent 5b07ea7 commit 3b88f4c
Show file tree
Hide file tree
Showing 20 changed files with 173 additions and 183 deletions.
4 changes: 2 additions & 2 deletions app/charts/good_job/scheduled_by_queue_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def data
start_time = end_time - 1.day
table_name = GoodJob::Job.table_name

count_query = Arel.sql(GoodJob::Execution.pg_or_jdbc_query(<<~SQL.squish))
count_query = Arel.sql(GoodJob::Job.pg_or_jdbc_query(<<~SQL.squish))
SELECT *
FROM generate_series(
date_trunc('hour', $1::timestamp),
Expand All @@ -35,7 +35,7 @@ def data
ActiveRecord::Relation::QueryAttribute.new('start_time', start_time, ActiveRecord::Type::DateTime.new),
ActiveRecord::Relation::QueryAttribute.new('end_time', end_time, ActiveRecord::Type::DateTime.new),
]
executions_data = GoodJob::Execution.connection.exec_query(GoodJob::Execution.pg_or_jdbc_query(count_query), "GoodJob Dashboard Chart", binds)
executions_data = GoodJob::Job.connection.exec_query(GoodJob::Job.pg_or_jdbc_query(count_query), "GoodJob Dashboard Chart", binds)

queue_names = executions_data.reject { |d| d['count'].nil? }.map { |d| d['queue_name'] || BaseFilter::EMPTY }.uniq
labels = []
Expand Down
10 changes: 3 additions & 7 deletions app/models/good_job/execution.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
# frozen_string_literal: true

module GoodJob
# Active Record model that represents an +ActiveJob+ job.
# Most behavior is currently in BaseExecution in anticipation of
# moving behavior into +GoodJob::Job+.
class Execution < BaseExecution
self.table_name = 'good_jobs'

belongs_to :job, class_name: 'GoodJob::Job', foreign_key: 'active_job_id', primary_key: 'active_job_id', optional: true, inverse_of: :executions
# Created at the time a Job begins executing.
# Behavior from +DiscreteExecution+ will be merged into this class.
class Execution < DiscreteExecution
end
end
4 changes: 2 additions & 2 deletions app/models/good_job/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class Job < BaseExecution

belongs_to :batch, class_name: 'GoodJob::BatchRecord', inverse_of: :jobs, optional: true
belongs_to :locked_by_process, class_name: "GoodJob::Process", foreign_key: :locked_by_id, inverse_of: :locked_jobs, optional: true
has_many :executions, -> { order(created_at: :asc) }, class_name: 'GoodJob::DiscreteExecution', foreign_key: 'active_job_id', primary_key: :active_job_id, inverse_of: :job, dependent: :delete_all
has_many :executions, -> { order(created_at: :asc) }, class_name: 'GoodJob::Execution', foreign_key: 'active_job_id', primary_key: :active_job_id, inverse_of: :job, dependent: :delete_all
# TODO: rename callers of discrete_execution to executions, but after v4 has some time to bake for cleaner diffs/patches
has_many :discrete_executions, -> { order(created_at: :asc) }, class_name: 'GoodJob::DiscreteExecution', foreign_key: 'active_job_id', primary_key: :active_job_id, inverse_of: :job, dependent: :delete_all
has_many :discrete_executions, -> { order(created_at: :asc) }, class_name: 'GoodJob::Execution', foreign_key: 'active_job_id', primary_key: :active_job_id, inverse_of: :job, dependent: :delete_all

before_create -> { self.id = active_job_id }, if: -> { active_job_id.present? }

Expand Down
4 changes: 2 additions & 2 deletions demo/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
break if start_date > Time.current
end

GoodJob::Execution.insert_all(jobs_data)
puts "Inserted #{jobs_data.size} job records for a total of #{GoodJob::Execution.count} job records."
GoodJob::Job.insert_all(jobs_data)
puts "Inserted #{jobs_data.size} job records for a total of #{GoodJob::Job.count} job records."

puts ActiveJob::Base.queue_adapter

Expand Down
8 changes: 1 addition & 7 deletions lib/good_job/current_thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ module CurrentThread
# @return [GoodJob::Job, nil]
thread_mattr_accessor :job

# # @!attribute [rw] executions
# # @!scope class
# # Execution
# # @return [GoodJob::Execution, nil]
# thread_mattr_accessor :execution

# @!attribute [rw] execution_interrupted
# @!scope class
# Execution Interrupted
Expand Down Expand Up @@ -96,7 +90,7 @@ def self.to_h
end
end

# @return [String] UUID of the currently executing GoodJob::Execution
# @return [String] UUID of the currently executing GoodJob::Job
def self.active_job_id
job&.active_job_id
end
Expand Down
2 changes: 2 additions & 0 deletions sorbet/rbi/todo.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ module ::TestJob::Error; end
module ::TestJob::ExpectedError; end
module ::TestJob::RunError; end
module ::TestJob::SuccessCallbackJob; end
module ::TestRecord; end
module ::WrapperJob; end
module GoodJob::Job::ERROR_EVENT_INTERRUPTED; end
module GoodJob::Job::ERROR_EVENT_RETRIED; end
module Rails::Server; end

10 changes: 5 additions & 5 deletions spec/app/filters/good_job/jobs_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
end
Timecop.return

running_job = ExampleJob.perform_later('success')
running_execution = GoodJob::Execution.find(running_job.provider_job_id)
running_execution.update!(
running_active_job = ExampleJob.perform_later('success')
running_job = GoodJob::Job.find(running_active_job.provider_job_id)
running_job.update!(
finished_at: nil
)
running_execution.advisory_lock
running_job.advisory_lock
end

after do
GoodJob::Execution.advisory_unlock_session
GoodJob::Job.advisory_unlock_session
end

describe '#job_classes' do
Expand Down
8 changes: 4 additions & 4 deletions spec/app/jobs/example_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
describe "SUCCESS_TYPE" do
it 'completes successfully' do
active_job = described_class.perform_later(described_class::SUCCESS_TYPE)
execution = GoodJob::Execution.find(active_job.provider_job_id)
expect(execution.error).to be_nil
job = GoodJob::Job.find(active_job.provider_job_id)
expect(job.error).to be_nil
end
end

Expand Down Expand Up @@ -72,8 +72,8 @@

active_job = described_class.perform_later(described_class::SLOW_TYPE)

execution = GoodJob::Execution.find(active_job.provider_job_id)
expect(execution.error).to be_nil
job = GoodJob::Job.find(active_job.provider_job_id)
expect(job.error).to be_nil
end
end
end
Expand Down
Loading

0 comments on commit 3b88f4c

Please sign in to comment.