Uploaded image for project: 'OpenMRS Core'
  1. OpenMRS Core
  2. TRUNK-6144

Calling REST API during initialization break Reference Application

    XMLWordPrintable

Details

    Description

      Calling a REST API endpoint like

      /ws/rest/v1/session 

      during initialization of the Reference Application causes the Reference Application to fail to start properly.

      Expected Behavior

      Calling any REST API endpoints – especially something as simple as checking for a REST session – while the Reference Application is initializing should not prevent the Reference Application from initializing properly. The REST API may not respond, could return an error code, or could even return an empty response before initialization is complete, but it should not interfere with the Reference Application's ability to start.

      Observed Behavior

      Making calls to

      /ws/rest/v1/session 

      during startup (e.g., calling this endpoint every ~10-30 seconds) as the database setup is nearly complete but before the home page is initially displayed prevents the Reference Application from starting up correctly, yielding a 404 page and disabling the Reference Application.

      Recreating the error

      Using this docker-compose.yml file:

      version: "3.8"
      
      services:
        db:
          image: mysql:5.6
          environment:
            - MYSQL_ROOT_PASSWORD=openmrs
            - MYSQL_USER=openmrs
            - MYSQL_PASSWORD=openmrs
            - MYSQL_DATABASE=openmrs
          command: "mysqld --character-set-server=utf8 --collation-server=utf8_general_ci"
      
        openmrs:
          image: openmrs/openmrs-reference-application-distro:2.10.0
          depends_on:
            - db
          environment:
            - DB_HOST=db
            - DB_USERNAME=openmrs
            - DB_PASSWORD=openmrs
            - DB_DATABASE=openmrs
            - DB_CREATE_TABLES=true
            - DB_AUTO_UPDATE=true
            - MODULE_WEB_ADMIN=false
            - DEBUG=false
          ports:
            - 8080:8080
      

      and these two bash scripts:

      delayed_rest_call.sh
      #!/bin/bash
      
      echo -n "Waiting for initialization to complete"
      while :
      do
        READY=$(curl -s -H "Accept: application/json" \
          localhost:8080/openmrs/initialsetup?page=progress.vm.ajaxRequest | \
          jq '.initializationComplete'
        )
        if [ "$READY" = "true" ]; then
          echo "done"
          break
        else
          echo -n "."
          sleep 1
        fi
      done
      
      echo "Invoking REST API..."
      curl -q -i -H "Accept: application/json" \
        localhost:8080/openmrs/ws/rest/v1/session 
      early_rest_call.sh
      #!/bin/bash
      
      echo -n "Waiting for initialization to complete ('+' when invoking REST API)"
      while :
      do
        JSON=$(curl -s -H "Accept: application/json" \
          localhost:8080/openmrs/initialsetup?page=progress.vm.ajaxRequest
        )
        READY=$(
          echo -n "$JSON" | \
          jq '.initializationComplete'
        )
        if [ "$READY" = "true" ]; then
          echo " done"
          break
        elif [ "$READY" = "false" ]; then
          PROGRESS=$(
            echo -n "$JSON" | \
            jq '.completedPercentage'
          )
          if [ "$PROGRESS" -ge 100 ]; then
            echo -n "+"
            curl -q -H "Accept: application/json" \
              localhost:8080/openmrs/ws/rest/v1/session > /dev/null 2>&1
            sleep 10
          else
            echo -n "."
            sleep 1
          fi
        else
          echo -n "."
          sleep 1
        fi
      done
      
      echo "Invoking REST API..."
      curl -q -i -H "Accept: application/json" \
        localhost:8080/openmrs/ws/rest/v1/session 

      First start the Reference Application using

      docker-compose up -d 

      Optionally, monitor progress of OpenMRS log output:

      docker-compose logs -f openmrs 

      While the Reference Application is starting, run the first script:

      ./delayed_rest_call.sh 

      This script waits for the Reference Application to finish starting up and then curls the /ws/rest/v1/session rest endpoint, which succeeds.

      Now, destroy the Reference Application stack and restart it:

      docker-compose down -v
      docker-compose up -d 

      This time, while the Reference Application is initializing, run the second script:

      ./early_rest_call.sh 

      This script waits until the database has been initialized and then begins calling the REST API at /ws/rest/v1/session every 10 seconds. When initialization is complete, the Reference Application fails to start and returns a 404 error page.

      The same result can be achieved without the scripts simply by starting up OpenMRS and monitoring the initial setup page using a browser.

      • If you wait for the initializing to complete and the login page to be shown and then open http://localhost:8080/ws/rest/v1/session in another tab, it will work as expected.
      • If, once the database initialization is 99-100% complete but the login page has not yet been shown, you use another browser tab and repeatedly (every 10 seconds or so) try to open http://localhost:8080/ws/rest/v1/session (you'll be redirect to the initial setup page each time), the Reference Application will fail to start up and fail with a 404 page.

      Looks like the exact same problem happened to grace at https://talk.openmrs.org/t/unblock-o3-local-deployment-via-docker-compose-yml/37678/5

      FWIW, i was able to reproduce this problem by running a fresh copy of openmrs with only the legacyui module installed. In the browser, i used this url http://localhost:8080/ws/rest/v1/session which took me to the normal setup wizard, which run successfully up to 100%, but then the application crashed.

      Gliffy Diagrams

        Attachments

          1. delayed_rest_call.sh
            0.4 kB
          2. docker-compose.yml
            0.6 kB
          3. early_rest_call.sh
            0.8 kB

          Issue Links

            Activity

              People

                dkayiwa Daniel Kayiwa
                burke Burke Mamlin
                Votes:
                1 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: