Reputation: 2134
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.
Upvotes: 7
Views: 8896
Reputation: 5241
You can remove the Analyzers globally, which will only end up running the NullAnalyzer
:
ActiveStorage.analyzers = []
For something more selective, you can wrap them in a subclass that overrides their .accept?(blob)
.
For instance we use a custom ActiveStorage service class to encrypt some uploads, but it doesn't support the streaming download used by ActiveStorage::AnalyzeJob
. We disabled the analyze only for attachments using that service this way:
ActiveStorage.analyzers.map! do |klass|
Class.new(klass) do
def self.accept?(blob)
return false if blob.service.is_a?(EncryptedS3Service)
super
end
end
end
Upvotes: 0
Reputation: 21
I prefer a clear way by removing the auto call.
Rails.application.config.to_prepare do
ActiveStorage::Attachment.skip_callback(:commit, :after, :analyze_blob_later)
end
Upvotes: 2
Reputation: 1757
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
Upvotes: 2
Reputation: 500
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.
Upvotes: 5
Reputation: 2695
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
Upvotes: 2