diff --git a/.gitignore b/.gitignore index 9e67b269c21d..1d41f4e216f6 100644 --- a/.gitignore +++ b/.gitignore @@ -69,3 +69,5 @@ dump.rdb ui-debug.log firebase-debug.log debug.log +# development logs and status entries +.dev diff --git a/Makefile b/Makefile index 547e00952127..90488f30793e 100644 --- a/Makefile +++ b/Makefile @@ -46,15 +46,14 @@ run-devserver: ## Runs the dev-server docker compose stop angular-build docker compose up dev-server -d --no-deps $(MAKE) update.requirements - $(MAKE) run-offline + $(MAKE) start-devserver run-offline: ## Runs the dev-server in offline mode + ## Users can pass this check by simply running `make start-devserver` + $(MAKE) check.dev-container-healthy $(MAKE) start-devserver - @echo 'Please visit http://localhost:8181 to access the development server.' - @echo 'Check dev-server logs using "make logs.dev-server"' - @echo 'Stop the development server using "make stop"' -start-devserver: ## Starts the development server for the tests +start-devserver: ## Starts the development server docker compose up dev-server -d @printf 'Please wait while the development server starts...\n\n' @while [[ $$(curl -s -o /tmp/status_code.txt -w '%{http_code}' http://localhost:8181) != "200" ]] || [[ $$(curl -s -o /tmp/status_code.txt -w '%{http_code}' http://localhost:8181/community-library) != "200" ]]; do \ @@ -65,7 +64,9 @@ start-devserver: ## Starts the development server for the tests sleep 1; \ done @printf '\n\n' - @echo 'Development server started at port 8181.' + @echo 'Please visit http://localhost:8181 to access the development server.' + @echo 'Check dev-server logs using "make logs.dev-server"' + @echo 'Stop the development server using "make stop"' init: build run-devserver ## Initializes the build and runs dev-server. @@ -95,6 +96,20 @@ update.package: ## Installs the npm requirements for the project @echo 'yarn-path "../oppia_tools/yarn-1.22.15/bin/yarn"' > .yarnrc @echo 'cache-folder "../yarn_cache"' >> .yarnrc +check.dev-container-healthy: + @run_devserver_prompt="Please, run \`make run-devserver\` (requires internet) once before running \`make run-offline\`"; \ + if [ -f ".dev/containers-health.json" ]; then \ + if jq -e ".devserver != true" ".dev/containers-health.json" > /dev/null; then \ + echo "Container is unhealthy"; \ + echo $$run_devserver_prompt; \ + exit 1; \ + fi \ + else \ + echo "Can't check container health!"; \ + echo $$run_devserver_prompt; \ + exit 1; \ + fi + logs.%: ## Shows the logs of the given docker service. Example: make logs.datastore docker compose logs -f $* diff --git a/docker-compose.yml b/docker-compose.yml index f2797b0868f2..74bf312cfb53 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -74,7 +74,7 @@ services: - firebase - redis healthcheck: - test: curl -f http://localhost:8181/ || exit 1 + test: ["CMD", "bash", "docker/dev-server.healthcheck.sh"] timeout: 10s interval: 30s retries: 5 diff --git a/docker/Dockerfile.backend b/docker/Dockerfile.backend index 19d0cc598df1..39483cb8ac09 100644 --- a/docker/Dockerfile.backend +++ b/docker/Dockerfile.backend @@ -14,7 +14,8 @@ RUN apt-get update -y && apt-get upgrade -y \ python2 \ python3-matplotlib \ unzip \ - wget + wget \ + jq RUN curl -sL https://deb.nodesource.com/setup_16.x | bash - RUN apt-get install -y nodejs diff --git a/docker/dev-server.healthcheck.sh b/docker/dev-server.healthcheck.sh new file mode 100644 index 000000000000..8adc9391dfb5 --- /dev/null +++ b/docker/dev-server.healthcheck.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# Copyright 2023 The Oppia Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS-IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FILE=".dev/containers-health.json" + +# Create parent directories if needed +DIR=$(dirname "$FILE") +if [ ! -d "$DIR" ]; then + mkdir -p "$DIR" +fi + +# Check and create file +echo '{ "devserver": false }' > "$FILE" + +# Check if devserver health is stored and reset it to false +key_to_check="devserver" +if jq -e "has(\"$key_to_check\")" "$FILE" > /dev/null; then + jq '.["devserver"] = false' "$FILE" > /tmp/status.json && mv /tmp/status.json "$FILE" +else + jq '. + { "devserver": false }' "$FILE" > /tmp/status.json && mv /tmp/status.json "$FILE" +fi + +# Check container health +curl -f http://localhost:8181/ +STATUS=$? + +if [ $STATUS -eq 0 ]; then + # Healthy + jq '.["devserver"] = true' "$FILE" > /tmp/status.json && mv /tmp/status.json "$FILE" + + exit 0 + +else + # Unhealthy + jq '.["devserver"] = false' "$FILE" > /tmp/status.json && mv /tmp/status.json "$FILE" + + exit 1 +fi