solid_queue: Enqueue jobs inside a connected_to block
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.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 3
- Comments: 41 (22 by maintainers)
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!
@thibaudgg good point! I think in that case, you could rely on
ActiveRecord::Base.connected_to_many, with both yourApplicationRecordandActiveStorage::Record. Something likeThe more I’ve been looking into the connection handling code, the more I think the guides are wrong/outdated and that
ActiveRecord::Base.connected_toshould never be used directly 🤔I’m going to propose a PR to update the guides shortly.
@rosa so happy you were able to reproduce the error 🙌🏻
I can confirm that setting
config.active_storage.touch_attachment_records = falsemakes the problem disappear in my app when testing in the Rails console. Awesome detective job! 🔍 👍🏻That said, running my specs still raise
SQLite3::BusyException: database is lockedwhen the spec is attaching a PDF. Not sure what would be different here, the:testqueue adapter maybe?My test config is:
Please let me know if I can help debugging it further.
Aha! I think I found the reason why this doesn’t happen when using
ActiveRecord::Base.connected_to! It’s because duringtouch_attachments, we reach over the other connection because ofActiveStorage.touch_attachment_records, which istrueby default. This means we’d try to fetchblob.attachments.record, which isDogin this case, but we’ll do that from theActiveStorage::Recordconnection if that makes sense 🤔 It’s a bit hand-wavy because I haven’t ironed out the details about that yet, but settingmakes the problem disappear.
Ok, I found the reason why this happens with a PDF and not with a PNG. The reason is that, when attaching the blob, this method gets called. The
analyzer_classfor the PNG file isActiveStorage::Analyzer::ImageAnalyzer::Vipswhich doesn’t change the default foranalyze_later?, so that’strue, and aActiveStorage::AnalyzeJobgets enqueued. However, when using a PDF, theanalyzer_classby default isActiveStorage::Analyzer::NullAnalyzer, which just returns empty metadata, it doesn’t perform any analysis. Because of this, it setsanalyze_later?tofalse, which means we run thisupdate, and this callback. In thetouch_attachmentscallback is where I get the error. For the PNG, that callback happens together with theupdatein theAnalyzeJob, so not there 😅I don’t know yet why the callback works just fine when using
ActiveRecord::Base.connected_to, but I’ll continue investigating. I think this should be also reproducible without Active Storage, just with a similar callback, ensuring it’s triggered. I’ll look into that as well, since the simpler the reproduction, the simpler it should be to narrow down the problem.Aha! I can indeed reproduce with a PDF file:
And then:
But in this case, looks like the purge job ran fine, though it couldn’t find a blob with
id=1, which is super weird because there was a blob withid=1, the one I created in the previous test 🤔 This suggests that perhaps the async job is not connecting to the right shard 🤔 Although TBH I’m not sure why a purge job is enqueued in this case, as the dog record is new, so there was no previous attachment or anything 😕The
SQLite3::BusyException: database is locked (ActiveRecord::StatementTimeout)error itself seems to have happened in a callback… I need to investigate a bit more here! But this does look like a bug in Rails. I’d expect this to work exactly as ifActiveRecord::Base.connected_towere used, and across file types as well 🤔@rosa thanks again taking the time and your detailed answer.
Yes, I have set a similar initializer in my local branch to test
connected_to_many. 👍🏻Reading with
connected_to_manyseems fine, but writing to both record types inside the sameconnected_to_manyblock is throwing theActiveRecord::StatementTimeout: SQLite3::BusyException: database is lockederror for me.Giving your code example, you should be able to reproduce it with something like that:
Is this snippet failing for you too?
PS: As you are most likely well aware 😅, running this from the Rails console in the development environment makes use of the
:asyncqueue adapter and not:solid_queue. Not sure it that’s relevant here, butActiveStorage::PurgeJobwill be run using theTenant.switchcontext in my codebase.Ahhhh! Thanks for the extra explanation, that makes sense 🙏 No problem at all with going off-topic, this was super helpful for me as I try to figure out the best way to fix this and also the best way to add sharding support to Solid Queue itself, so yeah, super super helpful and relevant! ❤️ Thank you so much.
Oh, awesome! I’m sure this is going to be super helpful! I’ll look into all those next week, too 🙏 Thank you so much!
Thanks @rosa!
I just gave a tried to this patch on production, and it seems to work like a charm.
My app is open-source, so you can give it a look if that’s helpful to you. Here some relevant parts:
Sorry for the delay on this one, I haven’t managed to look into it yet, but it’s definitely on my radar. I’ll try next week 🤞