Rails: How to disable ActiveStorage Analyzers and Previewers

白昼怎懂夜的黑 提交于 2020-12-05 11:50:45

问题


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.


回答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




回答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.



来源:https://stackoverflow.com/questions/56445881/rails-how-to-disable-activestorage-analyzers-and-previewers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!