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

Enqueue jobs inside a connected_to block #369

Open
bubiche opened this issue Oct 2, 2024 · 2 comments
Open

Enqueue jobs inside a connected_to block #369

bubiche opened this issue Oct 2, 2024 · 2 comments

Comments

@bubiche
Copy link

bubiche commented Oct 2, 2024

My rails application connects to multiple databases with the configs below:

database.yml
production:
  primary_shard:
    ...
  secondary_shard:
    ...
  queue:
    ...
config/initializers/database.rb
# This is needed due to us using another gem whose model writes to both primary_shard and secondary_shard and its model inherits from ActiveRecord::Base
ActiveRecord::Base.instance_eval do
    connects_to shards: {
      primary_shard: {
        writing: :primary_shard,
      },
      secondary_shard: {
        writing: :secondary_shard,
      },
    }
end

SolidQueue has been working well, the only time where we cannot enqueue is if we have a block of code like below:

ActiveRecord::Base.connected_to(role: :writing, shard: :secondary_shard) do
  # ...
  SomeJob.perform_later(some_argument)
  # ...
end

I think it is because SolidQueue is trying to insert the job into secondary_shard instead of the queue database. Is there any way to circumvent this besides rewriting the code to call perform_later outside of the connected_to block? SomeJob.perform_later might be called inside a function with the connected_to block so it's quite a big refactor for us.

@rosa
Copy link
Member

rosa commented Oct 5, 2024

Ohhh, totally! Let me look into this one to see how to fix, I think it's something Solid Queue should handle. Thanks for the report!

@djpate
Copy link

djpate commented Oct 18, 2024

Same issue here. is there a workaround? my entire controller is in a connected_to block?
I tried wrapping the perform_later in another connection switch but no luck.

ActiveRecord::Base.connected_to(role: :writing, shard: :queue) do
  Workers::Foobar.perform_later(account_id: account_id, id: id)
end

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

No branches or pull requests

3 participants