How to disable ActiveStorage Analyzers and Previewers
Asked Answered
L

5

7

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.

Leopoldine answered 4/6, 2019 at 14:23 Comment(0)
P
2

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

Paramorph answered 5/6, 2019 at 5:49 Comment(0)
T
5

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.

Torrell answered 12/6, 2019 at 14:52 Comment(7)
What do you name this file and where do you save it?Flavine
app/decorators/active_storage/blob/analyzable_decorator.rb You may also need to add require 'active_storage/blob/analyzable'Torrell
@aqwan, I don't understand the /decorators/ file structure. Are you requiring 'decorators' somewhere?Shushubert
I know it's probably terrible, I but added this code to the end of the model file that has_one_attached. I tried adding it to an initializer, application.rb, my environment files. All other locations broke method calls elsewhere in ActiveStorage::BlobShushubert
@Shushubert - yes, seems like in 5.2 I have had to add following to application.rb # Load decorators as late as possible so models being extended already autoloaded %w[ decorators ].each do |dir| config.to_prepare do Dir.glob(File.join(Rails.root, 'app', dir, '**/*_decorator.rb')).each { |c| require_dependency(c) } end endTorrell
@Torrell Thank you very much for this. It helped me to locate a problem in our test suite.Kentiggerma
This is not a bad solution, but overriding analyze_later is unnecessary since returning true from analyzed? will always effectively skip the job. The ability to manually trigger it could come in handy. Also putting it in the model file as @Shushubert mentions doesn't limit its effect to that model. It might be an error-free place, but will still override these methods globally.Nisse
P
2

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

Paramorph answered 5/6, 2019 at 5:49 Comment(0)
C
2

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
Clo answered 31/3, 2022 at 12:18 Comment(0)
K
2

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
Keef answered 21/12, 2022 at 16:59 Comment(0)
K
0

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
Kingsley answered 28/2 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.