0

I have an app that calls the Google Drive API to create and copy documents in a Google Drive folder, the create method generates a folder for each Test and creates copies of a Google Doc (question doc) and puts them in the associated folder to the Test (answers).

In localhost it works and all API responses arrive successfully, but in EC2 the response of the last request to the API doesn't arrive.

In the app's log the request doesn't appear but if the success response of the previous Google Doc generated("200"," HTTP :: Message ... from Google Drive API").

# log/production.log

D, [2019-09-07T01:47:11.891256 #12230] DEBUG -- : [3457498a-19dd-4a0a-a326-4dbf47a358c5] Sending HTTP post https://www.googleapis.com/drive/v3/files/1exr3HKLe6IgOZU8I22VZEV4WH24UrKnGOPvmGi8-g2k/copy?fields=id%2CwebViewLink%2Cname
D, [2019-09-07T01:47:14.442112 #12230] DEBUG -- : [3457498a-19dd-4a0a-a326-4dbf47a358c5] 200
D, [2019-09-07T01:47:14.442366 #12230] DEBUG -- : [3457498a-19dd-4a0a-a326-4dbf47a358c5] #<HTTP::Message:0x000055f8e60e6d40 @http_header=#<HTTP::Message::Headers:0x000055f8e60e6d18 @http_version="1.1", @body_size=0, @chunked=false, @request_method="POST", @request_uri=#<Addressable::URI:0x2afc73073d44 URI:https://www.googleapis.com/drive/v3/files/1exr3HKLe6IgOZU8I22VZEV4WH24UrKnGOPvmGi8-g2k/copy?fields=id%2CwebViewLink%2Cname>, @request_query=nil, @request_absolute_uri=nil, @status_code=200, @reason_phrase="OK", @body_type=nil, @body_charset=nil, @body_date=nil, @body_encoding=#<Encoding:UTF-8>, @is_request=false, @header_item=[["Cache-Control", "no-cache, no-store, max-age=0, must-revalidate"], ["Pragma", "no-cache"], ["Expires", "Mon, 01 Jan 1990 00:00:00 GMT"], ["Date", "Sat, 07 Sep 2019 01:47:14 GMT"], ["Vary", "Origin"], ["Vary", "X-Origin"], ["Content-Type", "application/json; charset=UTF-8"], ["Content-Encoding", "gzip"], ["X-Content-Type-Options", "nosniff"], ["X-Frame-Options", "SAMEORIGIN"], ["X-XSS-Protection", "1; mode=block"], ["Server", "GSE"], ["Alt-Svc", "quic=\":443\"; ma=2592000; v=\"46,43,39\""], ["Transfer-Encoding", "chunked"]], @dumped=false>, @peer_cert=#<OpenSSL::X509::Certificate: subject=#<OpenSSL::X509::Name CN=*.googleapis.com,O=Google LLC,L=Mountain View,ST=California,C=US>, issuer=#<OpenSSL::X509::Name CN=GTS CA 1O1,O=Google Trust Services,C=US>, serial=#<OpenSSL::BN:0x000055f8e6101028>, not_before=2019-08-23 10:30:37 UTC, not_after=2019-11-21 10:30:37 UTC>, @http_body=#<HTTP::Message::Body:0x000055f8e60e6ca0 @body="{\n \"id\": \"1qmoh-0cTJTNesh1qmxe97Bm0BpnXlrAng3ul2Ttryn8\",\n \"name\": \"Pregunta 2\",\n \"webViewLink\": \"https://docs.google.com/document/d/1qmoh-0cTJTNesh1qmxe97Bm0BpnXlrAng3ul2Ttryn8/edit?usp=drivesdk\"\n}\n", @size=0, @positions=nil, @chunk_size=nil>, @previous=nil>
D, [2019-09-07T01:47:14.443173 #12230] DEBUG -- : [3457498a-19dd-4a0a-a326-4dbf47a358c5] Success - #<Google::Apis::DriveV3::File:0x000055f8e6100768
 @id="1qmoh-0cTJTNesh1qmxe97Bm0BpnXlrAng3ul2Ttryn8",
 @name="Pregunta 2",
 @web_view_link=
  "https://docs.google.com/document/d/1qmoh-0cTJTNesh1qmxe97Bm0BpnXlrAng3ul2Ttryn8/edit?usp=drivesdk">

D, [2019-09-07T01:47:14.443797 #12230] DEBUG -- : [3457498a-19dd-4a0a-a326-4dbf47a358c5]   ^[[1m^[[35m (0.2ms)^[[0m  ^[[1m^[[35mBEGIN^[[0m
D, [2019-09-07T01:47:14.444924 #12230] DEBUG -- : [3457498a-19dd-4a0a-a326-4dbf47a358c5]   ^[[1m^[[36mAnswer Create (0.4ms)^[[0m  ^[[1m^[[32mINSERT INTO "answers" ("name", "id_google_doc", "gdrive_link", "test_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"^[[0m  [["name", "Pregunta 2"], ["id_google_doc", "1qmoh-0cTJTNesh1qmxe97Bm0BpnXlrAng3ul2Ttryn8"], ["gdrive_link", "https://docs.google.com/document/d/1qmoh-0cTJTNesh1qmxe97Bm0BpnXlrAng3ul2Ttryn8/edit?usp=drivesdk"], ["test_id", 59], ["created_at", "2019-09-07 01:47:14.444031"], ["updated_at", "2019-09-07 01:47:14.444031"]]
D, [2019-09-07T01:47:14.446000 #12230] DEBUG -- : [3457498a-19dd-4a0a-a326-4dbf47a358c5]   ^[[1m^[[35m (0.9ms)^[[0m  ^[[1m^[[35mCOMMIT^[[0m
D, [2019-09-07T01:47:14.446717 #12230] DEBUG -- : [3457498a-19dd-4a0a-a326-4dbf47a358c5] Sending HTTP post https://www.googleapis.com/drive/v3/files/1gtGCWyATlRkCu8yMLOifdPt_F6c2oRRwc2zLUG0HGUc/copy?fields=id%2CwebViewLink%2Cname
  # app/controllers/tests_controller.rb
  # POST /tests
  # POST /tests.json
  def create

    @test = Test.new(test_params)
    @students_course = @test.test_rubric.course.students
    @questions = @test.test_rubric.questions

    if @test.test_rubric.tests.first.present?
      init_drive_service
      @test.test_rubric.tests.each do |old_test|
        @gdrive_service.delete_doc(old_test.id_google)
        old_test.destroy
      end
    end


    respond_to do |format|
      if @students

        init_drive_service
        @root_folder = Folder.find(@test.test_rubric.tests_folder_id)
        @students_course.each do |student_test|
          @test1 = Test.new(test_params)
          @test_folder = new_folder(student_test.dni,@root_folder) #create folder in Google Drive
          @test1.id_google = @test_folder.id_gdrive
          @test1.gdrive_link = @test_folder.gdrive_link
          @test1.student = student_test
          @test1.shared = false
          @test1.save
          @questions.each do |q|
            @test_answer_gdrive = @gdrive_service.copy_doc(q.name, q.id_google_doc, @test_folder.id_gdrive) # ----- HERE CRASH ----
            @answer = Answer.new(name: @test_answer_gdrive.name,
                                 id_google_doc: @test_answer_gdrive.id,
                                 gdrive_link: @test_answer_gdrive.web_view_link)
            @answer.test = @test1
            @answer.save
          end
        end
        format.html { redirect_to evaluaciones_path(curso: @test.test_rubric.course), alert: 'success', notice: @test.test_rubric.name+' - Pruebas generadas exitosamente'}
        format.json { render :show, status: :created, location: @test }
      else
        format.html { render :new }
        format.json { render json: @test.errors, status: :unprocessable_entity }
      end
    end
  end

Could I be exceeding the Google Drive API calls limit or could I have an error in the @questions.each loop ?

Roman Alekseiev
  • 1,854
  • 16
  • 24
sebastian
  • 55
  • 5
  • Call limit a is likely possibility. This seems like a typical `n+1` problem. You have a loop within a loop. Do you have any idea of what `@students_course.count x @questions.count` equals? Looking at time stamps on your logs these are firing very quickly. See https://stackoverflow.com/questions/10311969/what-is-the-limit-on-google-drive-api-usage – lacostenycoder Sep 07 '19 at 11:20
  • can you post the code which defines `@gdrive_service` ? But I would recommend you move this heavy lifting out of the controller and into a worker because you have no idea how long this controller action could take and it could easily timeout out in production. – lacostenycoder Sep 07 '19 at 11:23
  • 1
    It might not be ratel limits as you should get at 403 error according to https://developers.google.com/drive/api/v3/handle-errors – lacostenycoder Sep 07 '19 at 11:29

0 Answers0