当我创建一个新设备时,新版本会添加到我的图像上传器中。然后我运行一个作业来为每个 MyModel 记录重新创建文件。Sidekiq 启动作业,但它不创建文件。我使用 ActiveJob 来声明工作。
这段代码:
MakingNewVersionImagesJob.perform_later("some_recently_added_device_name")
Sidekiq 重启后在控制台中工作正常。我认为 Sidekiq 没有看到 MyModel 的变化。我对吗?这里有什么解决方案吗?
注意:当我直接在控制器中而不是在后台执行时,它工作正常
class BaseImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
# Config/versions ...
def self.add_version(name, width, height)
version name do
process resize_to_fit: [width, height]
end
end
end
class MyModelImageUploader < BaseImageUploader
# blabla
end
class MyModel < ActiveRecord::Base
#...
mount_uploader :image, MyModelImageUploader
end
class Device < ActiveRecord::Base
after_create :add_uploaders_versions
def add_uploaders_versions
version = self.name.downcase.to_sym
MyModelImageUploader.add_version(version, self.width, self.height)
end
end
class MakingNewVersionImagesJob < ActiveJob::Base
queue_as :regular
def perform(version)
MyModel.find_each do |record|
record.image.recreate_versions!(version.to_sym)
record.save!
end
end
end
class DevicesController < ApplicationController
#...
def create
@device = Device.new(device_params)
if @device.save
MakingNewVersionImagesJob.perform_later(@device.name.downcase)
#...
end
end
end