[英]Rails: How to process images with a Rake task called from within an initializer
I need to get a specific variant of an image when I start the server, which is why I attempt this with a Rake task that I call from within an initializer:我需要在启动服务器时获取图像的特定变体,这就是为什么我尝试使用从初始化程序中调用的 Rake 任务来执行此操作的原因:
lib/test.rake:库/test.rake:
task test: :environment do
@user = User.find_by_email("thing@domain.com")
if @user.avatar.attached?
# FOR DEBUGGING:
puts "content_type detected is: #{@user.avatar.blob.content_type}"
puts "ActiveStorage.variable_content_types are: #{ActiveStorage.variable_content_types}"
# GOAL: This should return the image variant
puts @user.avatar.variant(:thumbnail).inspect
end
end
When I execute this Rake task from the command line, it works perfectly:当我从命令行执行这个 Rake 任务时,它工作得很好:
$ rake test
But when it's executed from within the initializer below, it fails with an ActiveStorage::InvariableError
:但是当它从下面的初始化程序中执行时,它会失败并出现
ActiveStorage::InvariableError
:
/home/user/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activestorage-7.0.4/app/models/active_storage/blob/representable.rb:38:in `variant': ActiveStorage::InvariableError (ActiveStorage::InvariableError)
from /home/user/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activestorage-7.0.4/app/models/active_storage/attachment.rb:66:in `variant'
from /home/user/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-7.0.4/lib/active_support/core_ext/module/delegation.rb:304:in `public_send'
from /home/user/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-7.0.4/lib/active_support/core_ext/module/delegation.rb:304:in `method_missing'
initializers/my_initializer.rb:初始值设定项/my_initializer.rb:
Rails.configuration.after_initialize do
# Execute only when in DEV, and not when called from within a Rake task
if Rails.env.development? && !( defined?(Rake) &&
defined?(Rake.application) &&
Rake.application.top_level_tasks.length > 0 )
# Load the Rake tasks
Rails.application.load_tasks
# Test the execution of the Rake task:
Rake::Task["test"].reenable
Rake::Task["test"].invoke
end
end
I have traced the problem back to ActiveStorage.variable_content_types
being an empty array, here, instead of the full list of accepted content_types
:我已经将问题追溯到
ActiveStorage.variable_content_types
是一个空数组,在这里,而不是接受的content_types
的完整列表:
["image/png",
"image/gif",
"image/jpg",
"image/jpeg",
"image/pjpeg",
"image/tiff",
"image/bmp",
"image/vnd.adobe.photoshop",
"image/vnd.microsoft.icon",
"image/webp",
"image/avif",
"image/heic",
"image/heif"]
I suppose that somehow ActiveStorage
is not quite ready, when the Rake task is called from within the initializer.我想当从初始化程序中调用 Rake 任务时,
ActiveStorage
还没有完全准备好。 But I thought that using the .after_initialize
hook inside the initializer would solve the issue.但我认为在初始化程序中使用
.after_initialize
挂钩可以解决问题。 Apparently not.显然不是。
What is the correct way to get ActiveStorage
fully ready, in this scenario, so that the image variant can be processed/returned?在这种情况下,让
ActiveStorage
完全准备好以便可以处理/返回图像变体的正确方法是什么?
Not sure if can help, but I also run a rake task when my app starts.不确定是否有帮助,但我还在我的应用程序启动时运行了一个 rake 任务。 And I have put the code in
config/application.rb
我把代码放在
config/application.rb
module myApp
class Application < Rails::Application
...
if defined?(Rails::Server)
config.after_initialize do
Rails.application.load_tasks
Rake::Task["my_task"].invoke
end
end
end
end
Though I don't really care at what point it is invoked in the boot sequence.虽然我真的不在乎它在引导序列中的什么时候被调用。 It may not suit your requirements.
它可能不适合您的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.