Skip to content

Commit

Permalink
Allow to specify parent class for active record (#238)
Browse files Browse the repository at this point in the history
* Allow to specify parent class for active record

Refs #236

* Add config description
  • Loading branch information
morgoth committed Apr 19, 2021
1 parent 8c0e2c0 commit 9f87ffb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
9 changes: 9 additions & 0 deletions lib/good_job.rb
Expand Up @@ -17,6 +17,15 @@
#
# +GoodJob+ is the top-level namespace and exposes configuration attributes.
module GoodJob
# @!attribute [rw] active_record_parent_class
# @!scope class
# The ActiveRecord parent class inherited by +GoodJob::Job+ (default: +ActiveRecord::Base+).
# Use this when using multiple databases or other custom ActiveRecord configuration.
# @return [ActiveRecord::Base]
# @example Change the base class:
# GoodJob.active_record_parent_class = "CustomApplicationRecord"
mattr_accessor :active_record_parent_class, default: "ActiveRecord::Base"

# @!attribute [rw] logger
# @!scope class
# The logger used by GoodJob (default: +Rails.logger+).
Expand Down
2 changes: 1 addition & 1 deletion lib/good_job/job.rb
Expand Up @@ -2,7 +2,7 @@ module GoodJob
#
# Represents a request to perform an +ActiveJob+ job.
#
class Job < ActiveRecord::Base
class Job < Object.const_get(GoodJob.active_record_parent_class)
include Lockable

# Raised if something attempts to execute a previously completed Job again.
Expand Down
4 changes: 2 additions & 2 deletions lib/good_job/lockable.rb
Expand Up @@ -143,7 +143,7 @@ def with_advisory_lock
def supports_cte_materialization_specifiers?
return @_supports_cte_materialization_specifiers if defined?(@_supports_cte_materialization_specifiers)

@_supports_cte_materialization_specifiers = ActiveRecord::Base.connection.postgresql_version >= 120000
@_supports_cte_materialization_specifiers = connection.postgresql_version >= 120000
end
end

Expand All @@ -158,7 +158,7 @@ def advisory_lock
WHERE pg_try_advisory_lock(('x'||substr(md5($1 || $2::text), 1, 16))::bit(64)::bigint)
SQL
binds = [[nil, self.class.table_name], [nil, send(self.class.primary_key)]]
ActiveRecord::Base.connection.exec_query(pg_or_jdbc_query(query), 'GoodJob::Lockable Advisory Lock', binds).any?
self.class.connection.exec_query(pg_or_jdbc_query(query), 'GoodJob::Lockable Advisory Lock', binds).any?
end

# Releases an advisory lock on this record if it is locked by this database
Expand Down
6 changes: 3 additions & 3 deletions lib/good_job/notifier.rb
Expand Up @@ -36,7 +36,7 @@ class Notifier
# Send a message via Postgres NOTIFY
# @param message [#to_json]
def self.notify(message)
connection = ActiveRecord::Base.connection
connection = Job.connection
connection.exec_query <<~SQL.squish
NOTIFY #{CHANNEL}, #{connection.quote(message.to_json)}
SQL
Expand Down Expand Up @@ -159,8 +159,8 @@ def listen
end

def with_listen_connection
ar_conn = ActiveRecord::Base.connection_pool.checkout.tap do |conn|
ActiveRecord::Base.connection_pool.remove(conn)
ar_conn = Job.connection_pool.checkout.tap do |conn|
Job.connection_pool.remove(conn)
end
pg_conn = ar_conn.raw_connection
raise AdapterCannotListenError unless pg_conn.respond_to? :wait_for_notify
Expand Down

0 comments on commit 9f87ffb

Please sign in to comment.