Automating the starting of the SQL and API servers - TheEvergreenStateCollege/upper-division-cs-23-24 GitHub Wiki
If you run docker ps
and don't see a running database, follow these steps:
docker run -e POSTGRES_PASSWORD=<changeMe> --name=pg --restart always -d -p 5432:5432 postgres:14
This command ensures a persistent database by restarting the Docker container always, assuming you
named your container pg
in the run command above.
sudo docker exec -u postgres -it pg psql
This connects connects an interactive terminal to give you a good check that it is running.
Below, we will call <REPO_DIR>
the directory where you cloned the monorepo.
If you followed the homework instructions, it will be ~/src/upper-division-cs
We will modify the following script in (api-server-start.sh
):
Substitute for <REPLACE_WITH_YOUR_API_SERVER_DIR>
with the location of your API express server, wherever you put it when we did the Google Search Hits exercise.
It will look similar to /home/ubuntu/src/upper-division-cs/web-24wi/assignments/<YOUR_GITHUB_USERNAME>/api-server
.
If you want to remind yourself where you put these files in the monorepo, you can run
find ~/ -name "search-hit*"
This will look for files that match the pattern search-hit*
, which means any filenames
that begin with search-hit
followed by any other characters or empty. In the list of results,
look for your Github username.
#!/bin/bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
# Port for the web server
PORT=5000
# Check if the port is in use
if ! lsof -i :$PORT > /dev/null
then
echo "Port $PORT is not in use. Starting the application..."
cd <REPLACE_WITH_API_SERVER_DIR>
git checkout main
git pull
/usr/local/bin/npm run dev
else
echo "Port $PORT is already in use. Nothing to do. If this is a zombie process, run>>> . ~/port-checker.sh"
fi
sudo ln -s /home/ubuntu/.nvm/versions/node/v20.11.0/bin/npm /usr/local/bin/npm
Run crontab -e
and insert these two lines at the bottom:
* * * * * ~/src/upper-division-cs/web-24wi/scripts/api-server-start.sh
@reboot ~/src/upper-division-cs/web-24wi/scripts/api-server-start.sh
The first one runs every minute to check if the server is running and starts it if not. The second one runs a script right after a server reboot
Run sudo crontab -e
and add the following line at the bottom:
@reboot ~/src/upper-division-cs/web-24wi/scripts/api-server-start.sh
sudo apt install mailutils
Run through the setup, pick a local installation to simplify configuration, and then run:
mail
...to view your messages.
Make sure your registered domain name (<YOUR_SUBDOMAIN>.arcology.builders) works and check that ports 5000 and 5432 are in LISTEN.
These steps provide a comprehensive guide to automatically restarting Docker PostgreSQL and an API server, along with additional details for monitoring and troubleshooting. Cron jobs are a critical piece of automation, they handle all sorts of tasks on the local level. I hope this helped get past any blockage.
Finally for a live example: here is mine up and running: http://pswish-corp.arcology.builders:5000/ and http://pswish-corp.arcology.builders:5000/api/product