Rails app"无法分配内存"使用Paperclip保存照片时

时间:2014-06-15 07:34:46

标签: ruby-on-rails imagemagick paperclip

我能够在我的本地计算机上上传开发中的图像就好了,有时上传会在服务器上运行时失败。

我的设置是DigitalOcean上的rails droplet,相当不变。也就是说,使用Nginx和Unicorn。

这是日志的最后一部分:

I, [2014-06-15T07:19:14.456577 #17704]  INFO -- : Started GET "/photos" for 99.28.124.182 at 2014-06-15 07:19:14 +0000
I, [2014-06-15T07:19:14.458123 #17704]  INFO -- : Processing by PhotosController#index as HTML
I, [2014-06-15T07:19:14.537837 #17704]  INFO -- :   Rendered photos/index.html.erb within layouts/application (76.7ms)
I, [2014-06-15T07:19:14.540100 #17704]  INFO -- : Completed 200 OK in 82ms (Views: 78.7ms | ActiveRecord: 1.0ms)
I, [2014-06-15T07:19:23.700892 #17645]  INFO -- : Started GET "/photos/new" for 99.28.124.182 at 2014-06-15 07:19:23 +0000
I, [2014-06-15T07:19:23.703720 #17645]  INFO -- : Processing by PhotosController#new as HTML
I, [2014-06-15T07:19:23.717503 #17645]  INFO -- :   Rendered photos/_form.html.erb (8.1ms)
I, [2014-06-15T07:19:23.717913 #17645]  INFO -- :   Rendered photos/new.html.erb within layouts/application (8.9ms)
I, [2014-06-15T07:19:23.720011 #17645]  INFO -- : Completed 200 OK in 16ms (Views: 11.7ms | ActiveRecord: 0.5ms)
I, [2014-06-15T07:27:53.659373 #17704]  INFO -- : Started POST "/photos" for 99.28.124.182 at 2014-06-15 07:27:53 +0000
I, [2014-06-15T07:27:53.661330 #17704]  INFO -- : Processing by PhotosController#create as HTML
I, [2014-06-15T07:27:53.661460 #17704]  INFO -- :   Parameters: {"utf8"=>"✓", "authenticity_token"=>"[removed]", "photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x0000000138eba8 @tempfile=#<Tempfile:/tmp/RackMultipart20140615-17704-kpatwj>, @original_filename="IMG_0078.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"IMG_0078.jpg\"\r\nContent-Type: image/jpeg\r\n">, "description"=>"", "show_on_slideshow"=>"0", "show_on_gallery"=>"0"}, "commit"=>"Create Photo"}
I, [2014-06-15T07:27:53.671946 #17704]  INFO -- : Command :: file -b --mime-type '/tmp/91ff30d4faf0a3803148a98cb7ae142420140615-17704-f93sr9'
I, [2014-06-15T07:27:53.673477 #17704]  INFO -- : Completed 500 Internal Server Error in 12ms
F, [2014-06-15T07:27:53.676652 #17704] FATAL -- :
Errno::ENOMEM (Cannot allocate memory - file -b --mime-type '/tmp/91ff30d4faf0a3803148a98cb7ae142420140615-17704-f93sr9'):
app/models/photo.rb:14:in `check_file_size'
app/controllers/photos_controller.rb:32:in `create'

这是指第32行@photo = Photo.new(photo_params)行:

# POST /photos
# POST /photos.json
def create
  hide_from_unauthenticated
  @photo = Photo.new(photo_params)

  respond_to do |format|
    if @photo.save
      format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
      format.json { render :show, status: :created, location: @photo }
    else
      format.html { render :new }
      format.json { render json: @photo.errors, status: :unprocessable_entity }
    end
  end
end

2 个答案:

答案 0 :(得分:0)

您是否检查过服务器上当前的内存使用情况?当只剩下少量内存时,通常会发生此问题。正如你所说,这不会一直发生,所以我认为缺乏记忆是主要原因。

如果您无法立即向服务器添加更多内存,尝试在服务器上添加交换分区也会有所帮助。

答案 1 :(得分:0)

要创建子进程,可用内存必须大于父进程占用的内存。宝石“ posix-spawn”解决了这个问题。

根据文章-https://blog.sundaycoding.com/blog/2014/02/05/fighting-paperclip-errno-enomem-error/