Skip to content

Commit

Permalink
Extract shared methods/scopes into a BaseExecution to share between E…
Browse files Browse the repository at this point in the history
…xecution and Job models; remove deprecated ActiveJobJob model (#894)
  • Loading branch information
bensheldon authored Apr 2, 2023
1 parent 6318e14 commit 685d80b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 69 deletions.
2 changes: 1 addition & 1 deletion app/filters/good_job/base_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def queues

def job_classes
filtered_query(params.slice(:queue_name)).unscope(:select)
.group("serialized_params->>'job_class'").count
.group(GoodJob::BaseExecution.params_job_class).count
.sort_by { |name, _count| name.to_s }
.to_h
end
Expand Down
11 changes: 0 additions & 11 deletions app/models/good_job/active_job_job.rb

This file was deleted.

44 changes: 44 additions & 0 deletions app/models/good_job/base_execution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

module GoodJob
# ActiveRecord model to share behavior between {Job} and {Execution} models
# which both read out of the same table.
class BaseExecution < BaseRecord
self.table_name = 'good_jobs'

# With a given class name
# @!method job_class
# @!scope class
# @param string [String] Execution class name
# @return [ActiveRecord::Relation]
scope :job_class, ->(name) { where(params_job_class.eq(name)) }

class << self
def json_string(json, attr)
Arel::Nodes::Grouping.new(Arel::Nodes::InfixOperation.new('->>', json, Arel::Nodes.build_quoted(attr)))
end

def params_job_class
json_string(arel_table['serialized_params'], 'job_class')
end

def params_execution_count
Arel::Nodes::InfixOperation.new(
'::',
json_string(arel_table['serialized_params'], 'executions'),
Arel.sql('integer')
)
end

def coalesce_scheduled_at_created_at
arel_table.coalesce(arel_table['scheduled_at'], arel_table['created_at'])
end
end

# The ActiveJob job class, as a string
# @return [String]
def job_class
serialized_params['job_class']
end
end
end
14 changes: 3 additions & 11 deletions app/models/good_job/execution.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
module GoodJob
# ActiveRecord model that represents an +ActiveJob+ job.
class Execution < BaseRecord
class Execution < BaseExecution
include Lockable
include Filterable
include Reportable
Expand Down Expand Up @@ -82,14 +82,6 @@ def self.queue_parser(string)
# @return [ActiveRecord::Relation]
scope :active_job_id, ->(active_job_id) { where(active_job_id: active_job_id) }

# Get executions with given class name
# @!method job_class
# @!scope class
# @param string [String]
# Execution class name
# @return [ActiveRecord::Relation]
scope :job_class, ->(job_class) { where("serialized_params->>'job_class' = ?", job_class) }

# Get executions that have not yet finished (succeeded or discarded).
# @!method unfinished
# @!scope class
Expand Down Expand Up @@ -119,7 +111,7 @@ def self.queue_parser(string)
# @!method creation_ordered
# @!scope class
# @return [ActiveRecord:Relation]
scope :creation_ordered, -> { order('created_at ASC') }
scope :creation_ordered, -> { order(created_at: :asc) }

# Order executions for de-queueing
# @!method dequeueing_ordered
Expand Down Expand Up @@ -155,7 +147,7 @@ def self.queue_parser(string)
# @!method schedule_ordered
# @!scope class
# @return [ActiveRecord::Relation]
scope :schedule_ordered, -> { order(Arel.sql('COALESCE(scheduled_at, created_at) ASC')) }
scope :schedule_ordered, -> { order(coalesce_scheduled_at_created_at.asc) }

# Get Jobs were completed before the given timestamp. If no timestamp is
# provided, get all jobs that have been completed. By default, GoodJob
Expand Down
35 changes: 1 addition & 34 deletions app/models/good_job/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module GoodJob
# The +good_jobs+ table is a table of individual {GoodJob::Execution}s that share the same +active_job_id+.
# A single row from the +good_jobs+ table of executions is fetched to represent a Job.
#
class Job < BaseRecord
class Job < BaseExecution
include Filterable
include Lockable
include Reportable
Expand All @@ -23,26 +23,6 @@ class << self
def table_name=(_value)
raise NotImplementedError, 'Assign GoodJob::Execution.table_name directly'
end

def json_string(json, attr)
Arel::Nodes::Grouping.new(Arel::Nodes::InfixOperation.new('->>', json, Arel::Nodes.build_quoted(attr)))
end

def params_job_class
json_string(arel_table['serialized_params'], 'job_class')
end

def params_execution_count
Arel::Nodes::InfixOperation.new(
'::',
json_string(arel_table['serialized_params'], 'executions'),
Arel.sql('integer')
)
end

def coalesce_scheduled_at_created_at
arel_table.coalesce(arel_table['scheduled_at'], arel_table['created_at'])
end
end

self.primary_key = 'active_job_id'
Expand All @@ -54,13 +34,6 @@ def coalesce_scheduled_at_created_at
# Only the most-recent unretried execution represents a "Job"
default_scope { where(retried_good_job_id: nil) }

# Get Jobs with given class name
# @!method job_class
# @!scope class
# @param string [String] Execution class name
# @return [ActiveRecord::Relation]
scope :job_class, ->(name) { where(params_job_class.eq(name)) }

# Get Jobs finished before the given timestamp.
# @!method finished_before(timestamp)
# @!scope class
Expand Down Expand Up @@ -89,12 +62,6 @@ def id
active_job_id
end

# The ActiveJob job class, as a string
# @return [String]
def job_class
serialized_params['job_class']
end

# Override #reload to add a custom scope to ensure the reloaded record is the head execution
# @return [Job]
def reload(options = nil)
Expand Down
12 changes: 0 additions & 12 deletions spec/app/models/good_job/active_job_job_spec.rb

This file was deleted.

0 comments on commit 685d80b

Please sign in to comment.