使用Paperclip保存照片时,Rails app“无法分配内存”
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Paperclip保存照片时,Rails app“无法分配内存”相关的知识,希望对你有一定的参考价值。
我能够在我的本地计算机上上传开发中的图像就好了,有时上传会在服务器上运行时失败。
我的设置是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"
Content-Type: image/jpeg
">, "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'
这是指@photo = Photo.new(photo_params)
这里的第32行:
# 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
答案
您是否检查过服务器上当前的内存使用情况?当只剩下少量内存时,通常会发生此问题。正如你所说,这不会一直发生,所以我认为缺乏记忆是主要原因。
如果无法立即向服务器添加更多内存,尝试在服务器上添加交换分区也会有所帮助。
另一答案
要创建子进程,可用内存必须大于父进程占用的内存。宝石'posix-spawn'解决了这个问题。
根据文章 - https://blog.sundaycoding.com/blog/2014/02/05/fighting-paperclip-errno-enomem-error/
以上是关于使用Paperclip保存照片时,Rails app“无法分配内存”的主要内容,如果未能解决你的问题,请参考以下文章
Rails 4 Paperclip with Devise,文件保存错误
Ruby on rails将base64保存为xlsx(或pdf或word)并使用paperclip保存
Rails 6,Paperclip,S3,s3_direct_upload @attachment.save 在 s3_direct_upload 完成后不保存