demarches-normaliennes/config/initializers/active_storage.rb
Pierre de La Morinerie 75a1046315 active_storage: refactor concerns
Follow-up of #5953.

Refactor the concerns with two goals:

- Getting closer from the way ActiveStorage adds its own hooks.
  Usually ActiveStorage does this using an `Attachment#after_create`
  hook, which then delegates to the blob to enqueue the job.
- Enqueuing each job only once. By hooking on `Attachment#after_create`,
  we guarantee each job will be added only once.

We then let the jobs themselves check if they are relevant or not, and
retry or discard themselves if necessary.

We also need to update the tests a bit, because Rails'
`perform_enqueued_jobs(&block)` test helper doesn't honor the `retry_on`
clause of jobs. Instead it forwards the exception to the caller – which
makes the test fail.

Instead we use the inline version of `perform_enqueued_jobs()`, without
a block, which properly ignores errors catched by retry_on.
2021-03-16 11:49:14 +01:00

68 lines
1.8 KiB
Ruby

Rails.application.config.active_storage.service_urls_expire_in = 1.hour
Rails.application.config.active_storage.analyzers.delete ActiveStorage::Analyzer::ImageAnalyzer
Rails.application.config.active_storage.analyzers.delete ActiveStorage::Analyzer::VideoAnalyzer
ActiveSupport.on_load(:active_storage_blob) do
include BlobTitreIdentiteWatermarkConcern
include BlobVirusScannerConcern
include BlobSignedIdConcern
end
ActiveSupport.on_load(:active_storage_attachment) do
include AttachmentTitreIdentiteWatermarkConcern
include AttachmentVirusScannerConcern
end
# When an OpenStack service is initialized it makes a request to fetch
# `publicURL` to use for all operations. We intercept the method that reads
# this url and replace the host with DS_Proxy host. This way all the operation
# are performed through DS_Proxy.
#
# https://github.com/fog/fog-openstack/blob/37621bb1d5ca78d037b3c56bd307f93bba022ae1/lib/fog/openstack/auth/catalog/v2.rb#L16
require 'fog/openstack/auth/catalog/v2'
module Fog::OpenStack::Auth::Catalog
class V2
def endpoint_url(endpoint, interface)
url = endpoint["#{interface}URL"]
if interface == 'public'
publicize(url)
else
url
end
end
private
def publicize(url)
search = %r{^https://[^/]+/}
replace = "#{ENV['DS_PROXY_URL']}/"
url.gsub(search, replace)
end
end
end
require 'fog/openstack/auth/catalog/v3'
module Fog::OpenStack::Auth::Catalog
class V3
def endpoint_url(endpoint, interface)
url = endpoint["url"]
if interface == 'public'
publicize(url)
else
url
end
end
private
def publicize(url)
search = %r{^https://[^/]+/}
replace = "#{ENV['DS_PROXY_URL']}/"
url.gsub(search, replace)
end
end
end