Wednesday, 15 April 2015

upload - Rails 4 paperclip Background job -



upload - Rails 4 paperclip Background job -

current logic

first, think have read articles on paperclip , i'm still stucked despite infos learned... can taht help really precious.

secondly, not utilize delayed_paperclip, nor s3_direct_upload (but jquery directupload).

i have users profile 4 differents pictures : logo, avatar, worka, workb

each format (logo, avatar, worka, workb) has 3 styles (:small, :thumb, :medium) user can update pictures => so update action concerned the 4 files fields in form other classic fields (name, email, ...) upload managed jquery directupload + paperclip

when user click file field add together image:

jquery directupload uploads file temp directory on s3 jquery callbacks url(key) the url assigned :temp hidden field generated in javascript

when form submit button pressed:

i assign paperclip url of file uploaded direct upload, help of @user.logo.temp contains url(key) paperclip generates styles <input type="hidden" name="user[logo_attributes][temp]" value="https://bucketname.s3.amazonaws.com/temp/e4b46d01-5d69-483b.jpg">

my problem: styles generation brings me heroku idle , timeout #15 #12

.

attempts: tried isolate upload process set in background job

.

i can't figure out how block paperclip styles generation @ first upload , generate them after in background job

before_post_process block post process, in background job

i didn't utilize .reprocess! since paperclip 4, update triggered and... infinite loop..., utilize .assign , .save instead

the file correctly assigned s3 hosted file, processed paperclip i'm not sure file file field, if uploaded or not (no trace of in console, since form submited, file too, if unused.

.

need: styles blocked processing in background job

my logo model

class logo < document s3_temp_url_format = %r{\/\/bucketname\.s3\.amazonaws\.com\/(?<path>temp\/.+\/(?<filename>.+))\z}.freeze has_attached_file :attachment, styles: { medium: "300x300#" }, convert_options: { medium: "-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: 1.megabyte } validates :temp, # presence: true, format: { with: s3_temp_url_format } before_save :set_attachment after_save :set_remote_url before_post_process :stop_process def stop_process false end def styles_process self.attachment.assign(attachment) self.save end def set_attachment # puts "begin -- set attachment" tries ||= 5 s3_temp_url_data = s3_temp_url_format.match(self.temp) s3 = aws::s3.new s3_temp_head = s3.buckets[env['s3_bucket']].objects[s3_temp_url_data[:path]].head self.attachment_file_name = s3_temp_url_data[:filename] self.attachment_file_size = s3_temp_head.content_length self.attachment_content_type = s3_temp_head.content_type self.attachment_updated_at = s3_temp_head.last_modified rescue aws::s3::errors::nosuchkey => e tries -= 1 if tries > 0 sleep(3) retry else false end end def set_remote_url s3_temp_url_data = s3_temp_url_format.match(self.temp) s3 = aws::s3.new self.attachment = uri.parse(self.temp) self.save s3.buckets[env['s3_bucket']].objects.with_prefix(s3_temp_url_data[:path]).delete_all end end

my controller

def update account_update_params = devise_parameter_sanitizer.sanitize(:account_update) @user = user.find(current_user.id) if @user.update_attributes(account_update_params) # here styles processing # resque background job go @user.logo.styles_process set_flash_message :notice, :updated redirect_to after_update_path_for(@user) else render :edit end end

my form

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), method: :put, html: { class: "form-horizontal directupload", role: "form" }) |f| %> <%= f.fields_for :logo |l| %> <%= l.file_field(:attachment, accept: 'image/gif,image/png,image/jpg,image/jpeg') %> <% end %> <input type="hidden" name="user[logo_attributes][temp]" value="https://bucketname.s3.amazonaws.com/temp/e4b46d01-5d69-483b.jpg"> <% end %>

ruby-on-rails-4 upload paperclip

No comments:

Post a Comment