diff --git a/lib/good_job.rb b/lib/good_job.rb index e4ad69e01..a7304e346 100644 --- a/lib/good_job.rb +++ b/lib/good_job.rb @@ -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+). diff --git a/lib/good_job/job.rb b/lib/good_job/job.rb index 52cd33ae3..636c16862 100644 --- a/lib/good_job/job.rb +++ b/lib/good_job/job.rb @@ -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. diff --git a/lib/good_job/lockable.rb b/lib/good_job/lockable.rb index 6cb2a2a13..a5471cd94 100644 --- a/lib/good_job/lockable.rb +++ b/lib/good_job/lockable.rb @@ -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 @@ -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 diff --git a/lib/good_job/notifier.rb b/lib/good_job/notifier.rb index 708e57306..ccfec08d1 100644 --- a/lib/good_job/notifier.rb +++ b/lib/good_job/notifier.rb @@ -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 @@ -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