【发布时间】:2014-11-28 21:23:05
【问题描述】:
我试图弄清楚如何在后台作业中使用回形针,尤其是在更新时。
我想要什么:两步更新第一次分配原始图片,第二次在后台作业中生成样式
更新用户参数时:
- 我在 S3 上有一个文件(使用 direct_upload 上传)
- 在回形针处理之前,我将此文件 (url) 附加到我的 document.attachment 模型中
- Paperclip 使用 url 并生成原始文件(将文件复制到回形针文件夹)
- 暂时不要处理样式
- 一旦原件没问题,我想重新处理其他样式(:medium, :thumb)
- 在后台作业 (Resque) 中隔离重新处理(或全局处理)以提高效率)
我尝试了什么
- 成功从S3 url分配文件,这里不再详述
- 所有样式都是从 S3 生成的
- 块回形针后处理
- 在 if @user.save 之后在控制器中添加重新处理后台作业调用,这会在 .save 上生成所有样式,然后使用 @user.panorama.attachment.reprocess 重新处理它们!...
我失败的地方
- 更新操作时样式重新处理隔离(后台作业)没有成功:
- 无限循环,因为回形针在文件更改后重新处理后重新启动更新
- 尝试了模型和控制器的方式,使用布尔“进程”,但仍然在循环中阻塞
after_update :reprocess_styles
before_post_process :stop_process
def stop_process
false #stop paperclip styles the post processing
end
def reprocess_styles
self.reprocess! :medium, :thumb #lauch the post process
self.save(validate: false)
end
它的更新操作让我陷入无法正确管理的循环。
我要么有一个无限循环再次重新处理样式,要么根本没有样式。
非常感谢您的帮助。
型号
class Panorama < Document
attr_reader :attachement_remote_url
before_create :attachment_remote_url
before_update :attachment_remote_url
# after_create :process_styles
after_update :process_styles
has_attached_file :attachment,
styles: { medium: "1000x1000#", thumb: "160x160#" },
convert_options: { medium: "-quality 75 -strip",
thumb: "-quality 75 -strip" },
default_url: ":parent_type/:class/:style/missing.png",
path: "/documents/:parent_type/:id_partition/:class/:style/:basename.:extension"
validates_attachment :attachment,
content_type: { content_type: ["image/gif", "image/png", "image/jpg", "image/jpeg"] },
size: { less_than: 10.megabyte }
before_post_process :stop_process
def stop_process
if !self.process && self.attachment_changed?
# self.process = true #loop...
false
end
end
def attachment_remote_url
self.attachment = URI.parse("path to my Amazon S3 file")
end
def process_styles
if !self.process
puts self.inspect
# self.process = false #loop...
self.attachment.reprocess! :medium, :thumb
self.save(validate: false)
end
end
def attachment_changed?
self.attachment_file_name_changed? or
self.attachment_content_type_changed? or
self.attachment_file_size_changed? or
self.attachment_updated_at_changed?
end
end
【问题讨论】:
-
做同样的事情并停留在同一点。你能解决这个问题吗?
标签: ruby-on-rails-4 paperclip background-process resque