David
David

Reputation: 39

Temp files disappears when CarrierWave processing

I created an ActiveJob to process my carrier waves uploads. However, when I upload more than one image, I get the following error for the second file:

Errno::ENOENT (No such file or directory @ rb_sysopen - C:/Users/tdavi/AppData/Local/Temp/RackMultipart20180830-392-z2s2i.jpg)

Here's the code in my controller:

if @post.save
  files = params[:post_attachments].map { |p|
    {image: p['photo'][:image].tempfile.path, description: p['photo'][:decription]}
  }
  ProcessPhotosJob.perform_later(@post.id, files.to_json)
  format.html { render :waiting }
end

And my ActiveJob

require 'json'

class ProcessPhotosJob < ApplicationJob
  queue_as :default

  def perform(post_id, photos_json)
    post = Post.friendly.find(post_id)
    photos = JSON.parse photos_json

    photos.each do |p|
        src_file = File.new(p['image'])
        post.post_attachments.create!(:photo => src_file, :description => p[:description])
    end

    post.processed = true
    post.save
  end
end

When I upload only one file to upload, it works okay.

Upvotes: 0

Views: 554

Answers (1)

MrShemek
MrShemek

Reputation: 2483

You should not pass Tempfile to the queued jobs.

First of all - TempFiles can be deleted automatically by Ruby (docs, explanation)

If you would like to upload file(s) and process them later (in a background), then I would suggest you check this question.

Upvotes: 2

Related Questions