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

Use custom ActiveRecord connection parameters in GoodJob external process #1591

Open
rdallasgray opened this issue Jan 30, 2025 · 8 comments

Comments

@rdallasgray
Copy link

Hi there, migrating from Sidekiq, where we would use the configure_server block to set a longer statement_timeout for all ActiveRecord queries inside the Sidekiq server process (where, in the 'real time' Rails server, we would use a timeout commensurate with the timeout for requests). Is there a way to do something equivalent in GoodJob? It seems like configure_active_record would only apply to models inheriting from GoodJob::BaseRecord, not to all connections.

@bensheldon
Copy link
Owner

If these are dynamic values, I think a simple around_perform in your application's ApplicationJob would be a good place.

I think that setting statement timeout though is done via database.yml, right? Any setup that happens during Rails initialization you'd need to set up directly in Rails (via an ENV variable probably).

Another way to do it would be by having multiple named database configurations. Here's how I do it in one of my apps:

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 20
  connect_timeout: 5
  checkout_timeout: 5

development:
  <<: *default
  database: dayoftheshirt_development

test:
  <<: *default
  database: dayoftheshirt_test

staging:
  primary:
    <<: *default
    database: dayoftheshirt_staging
    pool: 20
  unpooled:
    <<: *default
    url: <%= ENV["DATABASE_UNPOOLED_URL"] || ENV["DATABASE_URL"] %>
    pool: 20

production:
  primary:
    <<: *default
    url:  <%= ENV["DATABASE_URL"] %>
    pool: 20
  unpooled:
    <<: *default
    url:  <%= ENV["DATABASE_UNPOOLED_URL"] || ENV["DATABASE_URL"] %>
    pool: 20

...though you still have to then say "connects_to" in your application if it's in GoodJob.

@bensheldon
Copy link
Owner

bensheldon commented Jan 30, 2025

Also, I realize now there isn't a documented way to check for this, but if you needed to check if you're in the good_job command, you can do:

GoodJob::CLI.within_exe? # => true or nil/false

Which gets set here: https://github.com/bensheldon/good_job/blob/fdf9a01fc8c5f93a5394a2a59cc59ba1ab82af03/exe/good_job#L6C1-L6C24

@rdallasgray
Copy link
Author

Thanks @bensheldon, within_exe? is what I needed! Is that likely to stay stable?

@bensheldon
Copy link
Owner

Good question. I should make a PR to delegate that from GoodJob e.g. GoodJob.within_exe? (unless you have a better name for it) to make a stronger guarantee.

@rdallasgray
Copy link
Author

Naming works fine for me.

@bensheldon
Copy link
Owner

Naming is hard 😆 I just opened up this PR for GoodJob.cli?: #1592

I'll get this released in a bit today. Glad you opened this issue!

@rdallasgray
Copy link
Author

Fantastic thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Inbox
Development

No branches or pull requests

3 participants
@bensheldon @rdallasgray and others