Rails:open-uri,pdf到api post,timeout

时间:2014-05-29 17:37:04

标签: ruby-on-rails api pdf timeout open-uri

我试图通过其API将文档上传到DrChrono。当我转到指定的url并使用设备安全性(authenticate_user!)时,从Web浏览器,它下载68毫秒。但是,当我运行以下代码时,我收到超时错误。 PDF文件是一页,使用Prawn制作。

  open('http://' + host + '/recording/' + recording.id.to_s + '/analysis.pdf') do |file|
    params = {
      'document' => file.read,
      'doctor' => 'https://drchrono.com/api/doctors/' + doctor.id,
      'patient' => 'https://drchrono.com/api/patients/' + recording.patient.chrono_id.to_s,
      'description' => 'Report',
      'date' => Time.now.strftime("%Y/%m/%d").gsub('/', '-')
    }
    response = HTTParty.post('https://drchrono.com/api/documents', :headers => headers, :body => params)
    data = JSON.parse(response.body)
  end

我的日志显示在超时后

Started GET "/recording/131/analysis.pdf" for ip at 2014-05-29 10:18:43 -0700
Processing by Dashboard::RecordingsController#report as PDF
  Parameters: {"id"=>"131"}
Completed 401 Unauthorized in 8ms

我最初认为这是因为我的设计安全性没有通过正确的参数。因此,我将此特定控制器切换到使用令牌认证系统的API安全性。唯一真正的变化是现在打开后的网址包含一个身份验证令牌。

但是,我仍然在完全相同的位置得到超时错误,但现在它后跟

Started GET "/recording/132/analysis.pdf?token=relevant_token" for ip at 2014-05-29 10:27:52 -0700
Processing by Dashboard::RecordingsController#report as PDF
  Parameters: {"token"=>relevant_token, "id"=>"132"}
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE (authentication_token = 'SQVyUmxWsrsynxHN7PBJ') ORDER BY "users"."id" ASC LIMIT 1
  Recording Load (1.0ms)  SELECT "recordings".* FROM "recordings" WHERE "recordings"."user_id" IN (4)
  Note Load (0.4ms)  SELECT "notes".* FROM "notes" WHERE "notes"."user_id" IN (4)
  AccessiblePatient Load (0.4ms)  SELECT "accessible_patients".* FROM "accessible_patients" WHERE "accessible_patients"."user_id" IN (4)
  Patient Load (0.7ms)  SELECT "patients".* FROM "patients" WHERE "patients"."id" IN (93, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 114, 113)
  Recording Load (0.5ms)  SELECT "recordings".* FROM "recordings" WHERE "recordings"."id" = $1 LIMIT 1  [["id", "132"]]
  Patient Load (0.3ms)  SELECT "patients".* FROM "patients" WHERE "patients"."id" IN (97)
  Note Load (1.7ms)  SELECT "notes".* FROM "notes" WHERE "notes"."patient_id" = $1  [["patient_id", 97]]
  Recording Load (4.3ms)  SELECT "recordings".* FROM "recordings" WHERE "recordings"."patient_id" = $1  [["patient_id", 97]]
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 4]]
  Rendered text template (0.0ms)
Sent data  (6.4ms)
Completed 200 OK in 563ms (Views: 6.2ms | ActiveRecord: 10.4ms)

这让我相信初始超时与缺乏授权无关,而是因为即使访问未被拒绝,我仍然会超时。

我研究了我的设置,而这(Open an IO stream from a local file or url)表明我正走在正确的道路上。

由于pdf文件实际上是一个页面,通常只需要68秒,我不明白为什么我会遇到任何超时问题。有什么想法吗?

编辑:

此人似乎遇到了类似的问题(Rails open-uri breaking on path),但建议的解决方案我已经做过了(瘦而不是默认服务器)。

编辑:

让我们更具体一点。这很有效。

  content = open('http://www.google.com').read

这些没有。

  content = open('http://localhost:3000').read
  content = open('http://' + host).read

我有一个为我的Faye运行的puma服务器和一个通过rails运行的瘦服务器。

0 个答案:

没有答案