Skip to content

Enqueue jobs inside a connected_to blockΒ #369

Closed
rails/rails
#53865
@bubiche

Description

@bubiche

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions