rails / solid_queue

Database-backed Active Job backend
MIT License
1.76k stars 101 forks source link

No connection pool for 'SolidQueue::Record' found #331

Closed gczh closed 1 week ago

gczh commented 1 week ago

I'm trying to setup solid_queue for rails 7.2.1 and

I'm getting the error in terminal when i run bundle exec rails solid_queue:start

ActiveRecord::ConnectionNotEstablished: No connection pool for 'SolidQueue::Record' found. (ActiveRecord::ConnectionNotEstablished)

In my database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  primary:
    <<: *default
    database: test_app_development
  queue:
    <<: *default
    database: test_app_development_queue
    migrations_paths: db/queue_migrate

Running rails s works and it's clearly connecting to the primary database.

In my application.rb, I have the following:

# Database
config.active_record.writing_role = :primary
config.active_record.reading_role = :primary

Note: I have that setup as I have other databases configured in my database.yml and wanted to be explicit that all the app data goes to primary.

After commenting it out, I can verify that the queue is able to connect to the database now. However, I'm wondering if this is the expected behaviour as we should be able to specify those configuration in application.rb and it shouldn't affect how solid_queue connects to the database?

rosa commented 1 week ago

Hey @gczh, did you run bin/rails db:prepare after adding the queue database to database.yml?

rosa commented 1 week ago

And another question: do you have config.solid_queue.connects_to set somewhere?

gczh commented 1 week ago

Hey @gczh, did you run bin/rails db:prepare after adding the queue database to database.yml?

Yep I did!

And another question: do you have config.solid_queue.connects_to set somewhere?

I have that setup in both development.rb and production.rb. Is it not necessary to specify that if my database.yml has a multiple database config?

rosa commented 1 week ago

Is it not necessary to specify that if my database.yml has a multiple database config?

Yes, it is, that's why I asked. I saw another person running into trouble because they hadn't set that up šŸ˜…

In any case, apologies, I was replying from my phone yesterday night and didn't properly read and investigate your report šŸ˜³ In particular this part:

In my application.rb, I have the following:

# Database
config.active_record.writing_role = :primary
config.active_record.reading_role = :primary

Note: I have that setup as I have other databases configured in my database.yml and wanted to be explicit that all the app data goes to primary.

After commenting it out, I can verify that the queue is able to connect to the database now. However, I'm wondering if this is the expected behaviour as we should be able to specify those configuration in application.rb and it shouldn't affect how solid_queue connects to the database?

I think the writing_role and reading_role don't work like that. I think these parameters refer to the names of the roles and aren't directly related to one of your databases in particular. According to the docs:

Screenshot 2024-09-08 at 11 14 21

So this means you're setting both these roles to be named primary, and for Solid Queue, I assume you configured it to be using the default names, writing and reading. So it makes sense that when you commented that out, it worked. I think you should leave these two to be the writing and reading defaults, and for your app to use the main database, ensure ApplicationRecord connects to your configured primary DB.

class ApplicationRecord < ActiveRecord::Base
  connects_to database: { writing: :primary, reading: :replica }

Although I don't think this is necessary, Rails will do the right thing without you specifying that.