'Rails: How to disable ActiveStorage Analyzers and Previewers
I have a Rails 5.2.3 app using ActiveStorage. By default, ActiveStorage would run some background jobs to extract metadata from attached files, and/or create thumbnail images for previews.
I don't want that. I don't need any metadata, nor do I need any thumbnails. So how can I disable these background jobs?
According to the official Rails guide, I've set
config.active_storage.analyzers = []
config.active_storage.previewers = []
in /config/application.rb.
However, looks like it doesn't help. When running rails test, I still see
[ActiveJob] [ActiveStorage::AnalyzeJob] Performing ActiveStorage::AnalyzeJob (Job ID: 741592f5-c5e4-48d7-8cf9-158790fb8a00) from Inline(default) with arguments: #<GlobalID:0x00005642f9050748 @uri=#<URI::GID >>
[ActiveJob] [ActiveStorage::AnalyzeJob] (22.0ms) SAVEPOINT active_record_1
[ActiveJob] [ActiveStorage::AnalyzeJob] ActiveStorage::Blob Update (22.7ms) UPDATE `active_storage_blobs` SET `metadata` = '{\"identified\":true,\"analyzed\":true}' WHERE `active_storage_blobs`.`id` = 3056
[ActiveJob] [ActiveStorage::AnalyzeJob] (21.9ms) RELEASE SAVEPOINT active_record_1
I've also tried via an initializer file:
# /config/initializers/active_storage_disable_analyze.rb
Rails.application.config.active_storage.analyzers.delete ActiveStorage::Analyzer::ImageAnalyzer
Rails.application.config.active_storage.analyzers.delete ActiveStorage::Analyzer::VideoAnalyzer
But this doesn't help neither.
Solution 1:[1]
There is no easy way to disable Analyzers completely. Rails falls back to NullAnalyzer when you delete the Image/Video analyzers, which doesn't gather any metadata.
You can see where it defaults here
Solution 2:[2]
I did not want or need any analysis at all, ever, and over-riding the module methods seemed to do the trick.
ActiveStorage::Blob::Analyzable.module_eval do
def analyze_later
end
def analyzed?
true
end
end
Quite early days so I cannot guarantee no side effects but so far so good, no longer seeing any ActiveStorage::AnalyzeJob entries in my sidekiq log and the blobs seem perfectly usable, download links work etc.
Solution 3:[3]
config/initializers/override_as_blob.rb
Rails.application.config.to_prepare do
ActiveStorage::Blob.class_eval do
def analyzed?
# Do whatever override
if checksum.blank?
true
else
super
end
end
end
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | aqwan |
| Solution 3 | raykin |
