Skip to content

Commit

Permalink
Implemented start/stop/restart npm commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Razvan Botea committed Sep 20, 2016
1 parent 0c1d7cb commit 9cad451
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 0 deletions.
27 changes: 27 additions & 0 deletions bin/restart.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

function command_exists () {
hash "$1" &> /dev/null
}

worker_id=""

while [ $# -gt 0 ]; do
case "$1" in
--id=*)
worker_id="${1#*=}"
;;
esac
shift
done

if command_exists "forever" ; then
if [ $worker_id ]; then
forever stop ${worker_id}
else
forever stopall
fi
else
echo "Unable to run script. Please install 'forever' npm package globally."
exit 1
fi
128 changes: 128 additions & 0 deletions bin/startup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/usr/bin/env bash

function command_exists() {
hash "$1" &> /dev/null
}

declare -A env_vars

index=0
type=""
TP_SCKT_PORT=""
TP_SSL=""
TP_KAFKA_HOST=""
TP_KFK_PORT=""
TP_KFK_CLIENT=""
TP_REDIS_HOST=""
TP_REDIS_PORT=""
TP_REDISCACHE=""
TP_REDISCACHE_PORT=""
TP_MAIN_DB=""
TP_ES_HOST=""
TP_ES_PORT=""
TP_AMQP_HOST=""
TP_AMQP_USER=""
TP_AMQP_PASSWORD=""

env_vars_string=""

worker_types=("aggregation" "write" "transport_manager" "android_transport" "ios_transport" "sockets_transport")

while [ $# -gt 0 ]; do
case "$1" in
--t=*)
type="${1#*=}"
;;
--i=*)
index="${1#*=}"
;;
--TP_SCKT_PORT=*)
TP_SCKT_PORT="${1#*=}"
env_vars_string+="TP_SCKT_PORT=$TP_SCKT_PORT "
;;
--TP_SCKT_SSL=*)
TP_SCKT_SSL="${1#*=}"
env_vars_string+="TP_SSL=$TP_SCKT_SSL "
;;
--TP_KAFKA_HOST=*)
TP_KAFKA_HOST="${1#*=}"
env_vars_string+="TP_KAFKA_HOST=$TP_KAFKA_HOST "
;;
--TP_KFK_PORT=*)
TP_KFK_PORT="${1#*=}"
env_vars_string+="TP_KFK_PORT=$TP_KFK_PORT "
;;
--TP_KFK_CLIENT=*)
TP_KFK_CLIENT="${1#*=}"
env_vars_string+="TP_KFK_CLIENT=$TP_KFK_CLIENT "
;;
--TP_REDIS_HOST=*)
TP_REDIS_HOST="${1#*=}"
env_vars_string+="TP_REDIS_HOST=$TP_REDIS_HOST "
;;
--TP_REDIS_PORT=*)
TP_REDIS_PORT="${1#*=}"
env_vars_string+="TP_REDIS_PORT=$TP_REDIS_PORT "
;;
--TP_REDISCACHE=*)
TP_REDISCACHE="${1#*=}"
env_vars_string+="TP_REDISCACHE=$TP_REDISCACHE "
;;
--TP_REDISCACHE_PORT=*)
TP_REDISCACHE_PORT="${1#*=}"
env_vars_string+="TP_REDISCACHE_PORT=$TP_REDISCACHE_PORT "
;;
--TP_MAIN_DB=*)
TP_MAIN_DB="${1#*=}"
env_vars_string+="TP_MAIN_DB=$TP_MAIN_DB "
;;
--TP_ES_HOST=*)
TP_ES_HOST="${1#*=}"
env_vars_string+="TP_ES_HOST=$TP_ES_HOST "
;;
--TP_ES_PORT=*)
TP_ES_PORT="${1#*=}"
env_vars_string+="TP_ES_PORT=$TP_ES_PORT "
;;
--TP_AMQP_HOST=*)
TP_AMQP_HOST="${1#*=}"
env_vars_string+="TP_AMQP_HOST=$TP_AMQP_HOST "
;;
--TP_AMQP_USER=*)
TP_AMQP_USER="${1#*=}"
env_vars_string+="TP_AMQP_USER=$TP_AMQP_USER "
;;
--TP_AMQP_PASSWORD=*)
TP_AMQP_PASSWORD="${1#*=}"
env_vars_string+="TP_AMQP_PASSWORD=$TP_AMQP_PASSWORD "
esac
shift
done

if command_exists "forever" ; then
if [ $TP_SCKT_PORT ]; then
sleep 0
else
if [ $TP_SCKT_SSL ]; then
TP_SCKT_PORT=443
else
TP_SCKT_PORT=3000
fi
fi

if [[ $TP_SCKT_SSL == "1" && $TP_SCKT_PORT -lt 1023 && "$(whoami)" != "root" ]]; then
echo "ERROR: You must run this command as root (or with sudo)"
exit 3
else
total_ram=$(cat /proc/meminfo | grep MemTotal | awk '{ print $2 }')

if command_exists "bc"; then
total_ram=$(printf "%d" $(bc <<< "scale=2; ${total_ram}/1024*(3/4)") 2>/dev/null)
fi

eval "${env_vars_string} forever start --append --uid \"${type}${index}\" --colors -o ./logs/aggregation.out -e ./logs/aggregation.err -c \"node --nouse-idle-notification --max-old-space-size=${total_ram}\" ./index.js -t ${type} -i ${index}"
fi
else
echo "Unable to run script. Please install 'forever' npm package globally."
exit 4
fi
27 changes: 27 additions & 0 deletions bin/stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

function command_exists () {
hash "$1" &> /dev/null
}

worker_id=""

while [ $# -gt 0 ]; do
case "$1" in
--id=*)
worker_id="${1#*=}"
;;
esac
shift
done

if command_exists "forever" ; then
if [ $worker_id ]; then
forever stop ${worker_id}
else
forever stopall
fi
else
echo "Unable to run script. Please install 'forever' npm package globally."
exit 1
fi
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"description": "Telepat Workers are responsible for processing requests coming from the API and sending notifications to clients",
"main": "index.js",
"scripts": {
"start": "./bin/startup.sh",
"stop": "./bin/stop.sh",
"restart": "./bin/restart.sh",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand Down

0 comments on commit 9cad451

Please sign in to comment.