Pergunta

i have a rails 3 app, and today i decided add resque to my message_image uploader in my controller in create action

def create
@recipient = User.find(params[:message][:recipient_id])
@message = current_user.send_message(@recipient, params[:message][:body])
#@message.images << MessageImage.create!(params[:message][:images_attributes])
respond_with(@message) do |format|
  if @message.save!
    Resque.enqueue(AddImageToMessage, @message.id, params[:message][:images_attributes])
    format.js do
      unless request.referer.include? 'messages'
        render :text => "window.location = '#{messages_path}'", notice: 'message has been succ created.'
      end
    end
  else
    redirect_to :back
  end
end

end and i have class

class AddImageToMessage
 @queue = :messages_queue
 def self.perform(message_id, images_params)
   message = Message.find(message_id)
   message.images << MessageImage.create!(images_params)
   message.save
 end
end

But if i try to create new message and attach it image, a have an error in redis log server https://gist.github.com/Olefine/6270753 .In this logs i have a record invalid, but all my validations are pass if i use standart method in controller

@message.images << MessageImage.create!(params[:message][:images_attributes])
Foi útil?

Solução

I guess reason for that is how resque/redis work internally resque dump data to redis by serializing it

to json format

so it some what internally does this

 object.to_json  ## before pushing to redis
 JSON.parse object  ## after popping from redis

there lies your problem unlike yaml with json it not advisable to serialize complex object

But in your case your image_attributes contain a complex hash containing a file object and AFAIK no json library can serialize/deserialize a file ,which is even evident in your log as well

"tempfile"=>["�PNG\r\n", "\u001A\n", "\u0000\u0000\u0000\rIHDR\u0000\u0000\u0000�\u0000\u0000\u0000�\b\u0006\u0000\u0000\u0000�X��\u0000\u0000\u0000\u0006bKGD\u0000�\u0000�\u0000�����\u0000\u0000\u0000\atIME\a�\u0004\u0019\u000F\u0016!7\u0011\u001A-\u0000\u0000 \u0000IDATx���wt\u0014�����m��$��{�]D@\u0001�M�t\u0011\u0005EA�l(XP\u0004\u0015\u0010Q�H���S� �t�\"��P\u0013\b\u0010 !��-3�\u001FK����4\u0012���\u001C�w��\\6������\u001F��\u001Fv\u0011\n", "{\u0000�\u0018\u0001@\t \b�X\u001DK\u0000\u0012��\a?��\u001D�\u007F\u0014\u0004�\t�2��\u0016@#�\u0001P\u001D(\u000F�\\<�\u0000D\u0000 ..."

you can see that object serialization and deserialization is not what you except you it should be a file not a string displaying binary data output

I suggest you rather change your architecture and perhaps rethinking on how you intend to do this

Suggestion

Perhaps you can model your form multipart options and then use carrierwave_backgrounder or anything like it (if you dont intend to use carrierwave) and offload the uploading part in background through those library

Hope this help

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top