rails / solid_queue

Database-backed Active Job backend
MIT License
1.87k stars 121 forks source link

Enqueue jobs inside a connected_to block #369

Open bubiche opened 2 weeks ago

bubiche commented 2 weeks ago

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

database.yml ``` production: primary_shard: ... secondary_shard: ... queue: ... ```
config/initializers/database.rb ```ruby # 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 commented 2 weeks ago

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 commented 3 days ago

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