2

I'm trying to upload a document to DrChrono via its API. From the web browser when I go to the url specified and am using devise for security (authenticate_user!) it downloads in 68ms. However when I run the below code I get a timeout error. The PDF file is one page and made using 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

My log shows right after the timeout this

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

I initially thought this was because my devise security wasn't passing the correct parameters. So, I switched over this particular controller to my API security which uses a token authentication system. The only real change is now the url after open included an auth token.

However, I still get the timeout error in the exact same place but now its followed by

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)

This leads me to believe the initial timeout was not connected to a lack of authorization but something else since even when access is not denied I'm still timing out.

I researched my set-up and this (Open an IO stream from a local file or url) suggests I'm on the right path.

Since the pdf file is literally one page and takes a mere 68s usually, I don't see why I'd be having any issues with a timeout. Any ideas?

Edit:

This person seems to have had a similar issue (Rails open-uri breaking on path) but the suggested solution I'm already doing (Thin instead of default server).

Edit:

Let's get more specific. This works.

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

These do not.

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

I have a puma server running for my Faye and a Thin server running via rails s.

Community
  • 1
  • 1
Morgan
  • 1,438
  • 3
  • 17
  • 32

0 Answers0