-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp-control
executable file
·137 lines (103 loc) · 2.75 KB
/
app-control
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
# Control the operation of Python applications.
# Usage: app-control APP_NAME ACTION
# Actions:
# cron: Cron job tasks for the application.
# kill: Kill the application (in case it has hung).
# restart: Gracefully restart the application (uWSGI reload).
# start: Start the application.
# status: Status of the application (values: `running`, `stopped`, `crashed`).
# stop: Stop the application.
APPS_DIR="$HOME/apps"
__DIR__="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. $__DIR__/_easycli
# HELPERS
function _date() {
date '+%Y-%m-%d %H:%M:%S'
}
function _error() {
echo -e "\e[31mERROR: $1\e[0m"
exit 1
}
function _log() {
[ $IS_CRON = false ] && echo "[app: $APP_NAME] $1"
}
function _isRunning() {
# [ -n "$PID" ] && (ps -u `whoami` -o pid= | grep "^`whoami`\s+${PID}$" &> /dev/null)
[ -n "$PID" ] && [ "`ps -p $PID -o user=`" = "`whoami`" ]
}
# COMMANDS
## Cron job tasks for the application.
function command_cron() {
IS_CRON=true
command_start
}
## Kill the application (in case it has hung).
function command_kill() {
kill -9 $PID
_log "Killed uWSGI at `_date`."
rm -f $PID_FILE
}
## List available applications.
function command_list() {
ls $APPS_DIR
}
## Gracefully restart the application (uWSGI reload).
function command_restart() {
if _isRunning; then
$BIN_DIR/uwsgi --reload $PID_FILE
else
_log "uWSGI is not running so start it instead."
command_start
fi
}
## Start the application.
function command_start() {
if _isRunning; then
_log "uWSGI already running."
else
mkdir -p `dirname $PID_FILE`
$BIN_DIR/uwsgi --ini $APP_DIR/uwsgi.ini
_log "Started uWSGI at `_date`."
fi
}
## Status of the application (values: `running`, `stopped`, `crashed`).
function command_status() {
if _isRunning; then
echo "running"
else
[ -z "$PID" ] && echo "stopped" || echo "crashed"
fi
}
## Stop the application.
function command_stop() {
if _isRunning; then
$BIN_DIR/uwsgi --stop $PID_FILE
sleep 3
_isRunning && command_kill || _log "Stopped uWSGI at `_date`."
else
[ -f "$PID_FILE" ] && _log "uWSGI not running." || _log "No PID file."
fi
rm -f $PID_FILE
}
# EXECUTION
# Validate script arguments.
if [ $# -lt 2 ]; then
[ "$1" == "list" ] && { command_list; exit; }
easycli_display_usage
exit 1
fi
# Initialization.
APP_NAME=$1
ACTION=$2
ACTION_FUNCTION="command_$ACTION"
APP_DIR="$APPS_DIR/$APP_NAME"
BIN_DIR="$APP_DIR/venv/bin"
PID_FILE="$APP_DIR/tmp/uwsgi.pid"
[ -f "$PID_FILE" ] && PID=`cat $PID_FILE` || PID=
IS_CRON=false
# Check if app exists.
[ -d "$APP_DIR" ] || { _error "App '$APP_NAME' does not exist."; }
# Execute action.
declare -F "$ACTION_FUNCTION" &> /dev/null
[ $? = 0 ] && $ACTION_FUNCTION || _error "Unknown action '$ACTION'."