Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to specify parent class for active record #238

Merged
merged 2 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#
# +GoodJob+ is the top-level namespace and exposes configuration attributes.
module GoodJob
# TODO: explain me
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# TODO: explain me
# @!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
Original file line number Diff line number Diff line change
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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
Original file line number Diff line number Diff line change
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 = Job.connection.postgresql_version >= 120000
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The GoodJob::Lockable class is intended to be a generic ActiveRecord mixin, so this hopefully can be:

Suggested change
@_supports_cte_materialization_specifiers = Job.connection.postgresql_version >= 120000
@_supports_cte_materialization_specifiers = self.class.connection.postgresql_version >= 120000

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, this doesn't work somehow

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops. This is within class_methods so it just needs to be: 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?
Job.connection.exec_query(pg_or_jdbc_query(query), 'GoodJob::Lockable Advisory Lock', binds).any?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here:

Suggested change
Job.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
Original file line number Diff line number Diff line change
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