diff --git a/run.sh b/run.sh index ced1bcd..0d1542f 100755 --- a/run.sh +++ b/run.sh @@ -21,6 +21,11 @@ function usage { exit $1 } +function log { + message="$1" + echo -e "`date` - $message" +} + ## Parameters ## while getopts ':p:h' opt; do @@ -43,11 +48,11 @@ cd $DIR sudo="" if [[ $LOGNAME != "root" ]]; then - echo "`date` - Using sudo since user is '$LOGNAME'." + log "Using sudo since user is '$LOGNAME'." sudo="sudo" fi -echo "`date` - Check if any system dependencies need installed." +log "Check if any system dependencies need installed." if [[ ! `which php` ]]; then echo "- Installing PHP" $sudo apt-get install -y php-cli @@ -62,7 +67,7 @@ if [[ ! `which npm` ]]; then fi # Directories and allowed page types are executable, others are not. -echo "`date` - Fix any strange file permissions." +log "Fix any strange file permissions." find ./pages/ | while read file; do if [[ $file == *".php" || $file == *".sh" || -d $file ]]; then mode=755 @@ -72,30 +77,43 @@ find ./pages/ | while read file; do chmod -c $mode $file done -echo "`date` - Check if any node modules need updated/installed." +log "Check if any node modules need updated/installed." npm install # Reset generated index files. -echo "`date` - Removing old index files." +log "Removing old index files." find files/photos/ -name "*".html -print -delete { + count=1 http_code=0 + port="${ports%% *}" + if [[ -z $port ]]; then + port="8080" + fi + photos_uri=":$port/photos/" while [[ $http_code != "200" ]]; do - sleep 1 - echo "`date` - Checking if /photos/ is ready yet." - http_code="`curl --silent --fail --w '\n%{http_code}' localhost:8080/photos | tail -n 1`" - echo "`date` - Check for /photos/ responded with '$http_code'." + log "Sleeping for '$count' while waiting for $photos_uri to come up." + sleep $count + log "Checking if $photos_uri is available." + http_code="`curl --silent --fail --w '\n%{http_code}' localhost$photos_uri | tail -n 1`" + log "Check for $photos_uri responded with '$http_code'." + if (( $count >= 10 )); then + log "Giving up on loading $photos_uri after '$count' attempts." + break + else + count=$(( count + 1 )) + fi done - echo "`date` - Finished loading /photos/." + log "Finished checking for /photos/." } & ## Main ## -echo "`date` - Start website API." +log "Start website API." node ./main.js $ports status=$? ## Finish ## -echo "`date` - Exiting with status '$status'." +log "Exiting with status '$status'." exit $status