照片上传后服务器超时

Server timeouts after photo uploads

我正在使用 Postgres 爱好基础数据库进行 Heroku 的爱好计划。当我使用导入到 Amazon S3 的载波 gem 上传图像时,服务器超时。上传将发生,但需要一段时间才能处理。如果我上传一张照片或四张照片,也会出现同样的问题。

有人知道我可以做些什么来解决这个问题吗?创建照片时,会创建 8 个不同尺寸的版本,因为我在整个应用程序中使用了不同形状的照片。

我在下面包含了日志。

Jul 12 20:31:27 domain app/web.1: Processing by PhotosController#create as JS
Jul 12 20:31:27 domain app/web.1:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"M/h0eb77xtTjaeex2yi+rOPV+N2OA/nSd6X8HA69xyg=", "photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fb9c1a6fce8 @tempfile=#<Tempfile:/tmp/RackMultipart20150713-3-19cl8ow>, @original_filename="1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"1.jpg\"\r\nContent-Type: image/jpeg\r\n">}}
Jul 12 20:31:27 domain app/web.1: Processing by PhotosController#create as JS
Jul 12 20:31:27 domain app/web.1:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"M/h0eb77xtTjaeex2yi+rOPV+N2OA/nSd6X8HA69xyg=", "photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fb9c1a6fce8 @tempfile=#<Tempfile:/tmp/RackMultipart20150713-3-19cl8ow>, @original_filename="1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"1.jpg\"\r\nContent-Type: image/jpeg\r\n">}}
Jul 12 20:31:27 domain app/web.1: Started POST "/photos" for 73.54.214.248 at 2015-07-13 03:31:27 +0000
Jul 12 20:31:27 domain app/web.1: Started POST "/photos" for 73.54.214.248 at 2015-07-13 03:31:27 +0000
Jul 12 20:31:27 domain app/web.1: Started POST "/photos" for 73.54.214.248 at 2015-07-13 03:31:27 +0000
Jul 12 20:31:27 domain app/web.1: Processing by PhotosController#create as JS
Jul 12 20:31:27 domain app/web.1:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"M/h0eb77xtTjaeex2yi+rOPV+N2OA/nSd6X8HA69xyg=", "photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fb9c1cc5c90 @tempfile=#<Tempfile:/tmp/RackMultipart20150713-3-1rfxaqg>, @original_filename="2.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"2.jpg\"\r\nContent-Type: image/jpeg\r\n">}}
Jul 12 20:31:27 domain app/web.1:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"M/h0eb77xtTjaeex2yi+rOPV+N2OA/nSd6X8HA69xyg=", "photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fb9c1cc5c90 @tempfile=#<Tempfile:/tmp/RackMultipart20150713-3-1rfxaqg>, @original_filename="2.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"2.jpg\"\r\nContent-Type: image/jpeg\r\n">}}
Jul 12 20:31:27 domain app/web.1: Started POST "/photos" for 73.54.214.248 at 2015-07-13 03:31:27 +0000
Jul 12 20:31:27 domain app/web.1: Started POST "/photos" for 73.54.214.248 at 2015-07-13 03:31:27 +0000
Jul 12 20:31:27 domain app/web.1: Started POST "/photos" for 73.54.214.248 at 2015-07-13 03:31:27 +0000
Jul 12 20:31:27 domain app/web.1: Started POST "/photos" for 73.54.214.248 at 2015-07-13 03:31:27 +0000
Jul 12 20:31:27 domain app/web.1: Started POST "/photos" for 73.54.214.248 at 2015-07-13 03:31:27 +0000
Jul 12 20:31:27 domain app/web.1: Processing by PhotosController#create as JS
Jul 12 20:31:28 domain app/web.1: Attempting to optimize a jpeg without jpegoptim installed. Skipping...
Jul 12 20:31:29 domain app/web.1: Processing by PhotosController#create as JS
Jul 12 20:31:29 domain app/web.1: Attempting to optimize a jpeg without jpegoptim installed. Skipping...
Jul 12 20:31:29 domain app/web.1: Processing by PhotosController#create as JS
Jul 12 20:31:29 domain app/web.1:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"M/h0eb77xtTjaeex2yi+rOPV+N2OA/nSd6X8HA69xyg=", "photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fb9c1deaf80 @tempfile=#<Tempfile:/tmp/RackMultipart20150713-3-8t5ly3>, @original_filename="5.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"5.jpg\"\r\nContent-Type: image/jpeg\r\n">}}
Jul 12 20:31:29 domain app/web.1:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"M/h0eb77xtTjaeex2yi+rOPV+N2OA/nSd6X8HA69xyg=", "photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fb9c1deaf80 @tempfile=#<Tempfile:/tmp/RackMultipart20150713-3-8t5ly3>, @original_filename="5.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"5.jpg\"\r\nContent-Type: image/jpeg\r\n">}}
Jul 12 20:31:29 domain app/web.1: Processing by PhotosController#create as JS
Jul 12 20:31:29 domain app/web.1: Processing by PhotosController#create as JS
Jul 12 20:31:30 domain app/web.1:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"M/h0eb77xtTjaeex2yi+rOPV+N2OA/nSd6X8HA69xyg=", "photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fb9c1e7c750 @tempfile=#<Tempfile:/tmp/RackMultipart20150713-3-12sz8qi>, @original_filename="3.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"3.jpg\"\r\nContent-Type: image/jpeg\r\n">}}
Jul 12 20:31:30 domain app/web.1:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"M/h0eb77xtTjaeex2yi+rOPV+N2OA/nSd6X8HA69xyg=", "photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fb9c1e7c750 @tempfile=#<Tempfile:/tmp/RackMultipart20150713-3-12sz8qi>, @original_filename="3.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"3.jpg\"\r\nContent-Type: image/jpeg\r\n">}}
Jul 12 20:31:31 domain app/web.1: Processing by PhotosController#create as JS
Jul 12 20:31:31 domain app/web.1:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"M/h0eb77xtTjaeex2yi+rOPV+N2OA/nSd6X8HA69xyg=", "photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fb9c1d0c820 @tempfile=#<Tempfile:/tmp/RackMultipart20150713-3-28n50x>, @original_filename="4.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"4.jpg\"\r\nContent-Type: image/jpeg\r\n">}}
Jul 12 20:31:31 domain app/web.1:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"M/h0eb77xtTjaeex2yi+rOPV+N2OA/nSd6X8HA69xyg=", "photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fb9c1d0c820 @tempfile=#<Tempfile:/tmp/RackMultipart20150713-3-28n50x>, @original_filename="4.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"4.jpg\"\r\nContent-Type: image/jpeg\r\n">}}
Jul 12 20:31:31 domain app/web.1: Processing by PhotosController#create as JS
Jul 12 20:31:31 domain app/web.1: Attempting to optimize a jpeg without jpegoptim installed. Skipping...
Jul 12 20:31:34 domain app/web.1: Attempting to optimize a jpeg without jpegoptim installed. Skipping...
Jul 12 20:31:39 domain app/web.1: Attempting to optimize a jpeg without jpegoptim installed. Skipping...
Jul 12 20:31:57 domain heroku/router: at=error code=H12 desc="Request timeout" method=POST path="/photos" host=domain.com request_id=461e639a-52d9-423d-9159-425b6422dc63 fwd="73.54.214.248" dyno=web.1 connect=0ms service=30163ms status=503 bytes=0
Jul 12 20:31:57 domain heroku/router: at=error code=H12 desc="Request timeout" method=POST path="/photos" host=domain.com request_id=93120ab9-e570-45b3-b663-a8635355c0b3 fwd="73.54.214.248" dyno=web.1 connect=1ms service=30220ms status=503 bytes=0
Jul 12 20:31:57 domain heroku/router: at=error code=H12 desc="Request timeout" method=POST path="/photos" host=domain.com request_id=a754387c-96eb-419b-a35b-8d67f3451c4e fwd="73.54.214.248" dyno=web.1 connect=1ms service=30324ms status=503 bytes=0
Jul 12 20:31:57 domain heroku/router: at=error code=H12 desc="Request timeout" method=POST path="/photos" host=domain.com request_id=e17842ef-1a80-43ef-8594-bd67d924c0fd fwd="73.54.214.248" dyno=web.1 connect=1ms service=30283ms status=503 bytes=0
Jul 12 20:31:57 domain heroku/router: at=error code=H12 desc="Request timeout" method=POST path="/photos" host=domain.com request_id=a0bb9be3-49a7-4d19-a098-fa51dd59655d fwd="73.54.214.248" dyno=web.1 connect=1ms service=30257ms status=503 bytes=0
Jul 12 20:32:00 domain app/web.1: Started POST "/photos" for 73.54.214.248 at 2015-07-13 03:31:59 +0000
Jul 12 20:32:00 domain app/web.1: Started POST "/photos" for 73.54.214.248 at 2015-07-13 03:31:59 +0000
Jul 12 20:32:10 domain heroku/router: at=info method=POST path="/photos" host=domain.com request_id=593a9d5c-0ca0-41fe-a831-85e2ef023591 fwd="73.54.214.248" dyno=web.1 connect=1ms service=12581ms status=500 bytes=835
Jul 12 20:32:10 domain app/web.1: ActiveRecord::ConnectionTimeoutError (could not obtain a database connection within 5.000 seconds (waited 5.078 seconds)):

是的,请参阅 heroku/router 日志,其中表明请求花费的时间超过 30 秒。 Heroku 要求您在 30 秒内 return - see this article 解释一切。

我建议直接上传到 S3(可能是 like this),然后为 downloading/processing/uploading 使用后台工作程序。这样您就不会不必要地占用网络连接,并且您将拥有一个更具扩展性的解决方案。