Skip to content

Commit 32e18c9

Browse files
author
Pavel Patrin
committed
Restructuring code
1 parent e0a1166 commit 32e18c9

File tree

7 files changed

+539
-306
lines changed

7 files changed

+539
-306
lines changed

common.sh

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,56 @@
44
## Does all common job
55
##
66
## Exits with codes:
7-
## 1 - configuration file errors
8-
## 2 - dependencies errors
7+
## 1 - dependencies errors
8+
## 2 - configuration file errors
99
##
1010

11+
# Dependencies check
12+
WC=$(type -P wc) || { echo "Error: wc not found" 1>&2; exit 2; }
13+
PS=$(type -P ps) || { echo "Error: ps not found" 1>&2; exit 2; }
14+
RM=$(type -P rm) || { echo "Error: rm not found" 1>&2; exit 2; }
15+
CAT=$(type -P cat) || { echo "Error: cat not found" 1>&2; exit 2; }
16+
SED=$(type -P sed) || { echo "Error: sed not found" 1>&2; exit 2; }
17+
DATE=$(type -P date) || { echo "Error: date not found" 1>&2; exit 2; }
18+
KILL=$(type -P kill) || { echo "Error: kill not found" 1>&2; exit 2; }
19+
GREP=$(type -P grep) || { echo "Error: grep not found" 1>&2; exit 2; }
20+
TOUCH=$(type -P touch) || { echo "Error: touch not found" 1>&2; exit 2; }
21+
MKDIR=$(type -P mkdir) || { echo "Error: mkdir not found" 1>&2; exit 2; }
22+
NOHUP=$(type -P nohup) || { echo "Error: nohup not found" 1>&2; exit 2; }
23+
SLEEP=$(type -P sleep) || { echo "Error: sleep not found" 1>&2; exit 2; }
24+
FLOCK=$(type -P flock) || { echo "Error: flock not found" 1>&2; exit 2; }
25+
FALSE=$(type -P false) || { echo "Error: false not found" 1>&2; exit 2; }
26+
BASE64=$(type -P base64) || { echo "Error: base64 not found" 1>&2; exit 2; }
27+
MKFIFO=$(type -P mkfifo) || { echo "Error: mkfifo not found" 1>&2; exit 2; }
28+
1129
# Config check (logging level)
12-
[ -n "$LOG_LEVEL" ] || ! [[ "$LOG_LEVEL" =~ ^[[:digit:]]+$ ]] || { echo "Error: config directive LOG_LEVEL not defined correctly"; exit 1;}
30+
[ -n "$LOG_LEVEL" ] || ! [[ "$LOG_LEVEL" =~ ^[[:digit:]]+$ ]] || { echo "Error: config directive LOG_LEVEL is not defined correctly" 1>&2; exit 1; }
1331

1432
# Config check (parallel tasks)
15-
[ -n "$PARALLEL_TASKS" ] || ! [[ "$PARALLEL_TASKS" =~ ^[[:digit:]]+$ ]] || { echo "Error: config directive PARALLEL_TASKS not defined correctly"; exit 1;}
33+
[ -n "$PARALLEL_TASKS" ] || ! [[ "$PARALLEL_TASKS" =~ ^[[:digit:]]+$ ]] || { echo "Error: config directive PARALLEL_TASKS is not defined correctly" 1>&2; exit 1; }
1634

1735
# Config check (script parts directories)
18-
[ -n "$SCRIPT_DIR" -a -d "$SCRIPT_DIR" -a -w "$SCRIPT_DIR" -a -x "$SCRIPT_DIR" ] || { echo "Error: config directive SCRIPT_DIR not defined correctly"; exit 1;}
19-
[ -n "$LOCK_DIR" -a -d "$LOCK_DIR" -a -w "$LOCK_DIR" -a -x "$LOCK_DIR" ] || { echo "Error: config directive LOCK_DIR not defined correctly"; exit 1;}
20-
[ -n "$PIPE_DIR" -a -d "$PIPE_DIR" -a -w "$PIPE_DIR" -a -x "$PIPE_DIR" ] || { echo "Error: config directive PIPE_DIR not defined correctly"; exit 1;}
21-
[ -n "$TASK_DIR" -a -d "$TASK_DIR" -a -w "$TASK_DIR" -a -x "$TASK_DIR" ] || { echo "Error: config directive TASK_DIR not defined correctly"; exit 1;}
22-
[ -n "$LOG_DIR" -a -d "$LOG_DIR" -a -w "$LOG_DIR" -a -x "$LOG_DIR" ] || { echo "Error: config directive LOG_DIR not defined correctly"; exit 1;}
23-
[ -n "$PID_DIR" -a -d "$PID_DIR" -a -w "$PID_DIR" -a -x "$PID_DIR" ] || { echo "Error: config directive PID_DIR not defined correctly"; exit 1;}
36+
[ -n "$SCRIPT_DIR" -a -d "$SCRIPT_DIR" -a -w "$SCRIPT_DIR" -a -x "$SCRIPT_DIR" ] || { echo "Error: config directive SCRIPT_DIR is not defined correctly" 1>&2; exit 1; }
37+
[ -n "$PIPE_DIR" -a -d "$PIPE_DIR" -a -w "$PIPE_DIR" -a -x "$PIPE_DIR" ] || { echo "Error: config directive PIPE_DIR is not defined correctly" 1>&2; exit 1; }
38+
[ -n "$TASK_DIR" -a -d "$TASK_DIR" -a -w "$TASK_DIR" -a -x "$TASK_DIR" ] || { echo "Error: config directive TASK_DIR is not defined correctly" 1>&2; exit 1; }
39+
[ -n "$LOG_DIR" -a -d "$LOG_DIR" -a -w "$LOG_DIR" -a -x "$LOG_DIR" ] || { echo "Error: config directive LOG_DIR is not defined correctly" 1>&2; exit 1; }
40+
[ -n "$PID_DIR" -a -d "$PID_DIR" -a -w "$PID_DIR" -a -x "$PID_DIR" ] || { echo "Error: config directive PID_DIR is not defined correctly" 1>&2; exit 1; }
2441

2542
# Config check (script part files)
26-
[ -n "$COMMON_SCRIPT_FILE" -o -e "$COMMON_SCRIPT_FILE" ] || { echo "Error: config directive COMMON_SCRIPT_FILE not defined correctly"; exit 1;}
27-
[ -n "$WORKER_SCRIPT_FILE" -o -e "$WORKER_SCRIPT_FILE" -o -x "$WORKER_SCRIPT_FILE" ] || { echo "Error: config directive WORKER_SCRIPT_FILE not defined correctly"; exit 1;}
28-
[ -n "$DASHBOARD_SCRIPT_FILE" -o -e "$DASHBOARD_SCRIPT_FILE" -o -x "$DASHBOARD_SCRIPT_FILE" ] || { echo "Error: config directive DASHBOARD_SCRIPT_FILE not defined correctly"; exit 1;}
29-
[ -n "$TASKSQUEUE_SCRIPT_FILE" -o -e "$TASKSQUEUE_SCRIPT_FILE" -o -x "$TASKSQUEUE_SCRIPT_FILE" ] || { echo "Error: config directive TASKSQUEUE_SCRIPT_FILE not defined correctly"; exit 1;}
43+
[ -n "$COMMON_SCRIPT_FILE" -o -e "$COMMON_SCRIPT_FILE" ] || { echo "Error: config directive COMMON_SCRIPT_FILE is not defined correctly" 1>&2; exit 1; }
44+
[ -n "$WORKER_SCRIPT_FILE" -o -e "$WORKER_SCRIPT_FILE" -o -x "$WORKER_SCRIPT_FILE" ] || { echo "Error: config directive WORKER_SCRIPT_FILE is not defined correctly" 1>&2; exit 1; }
45+
[ -n "$DASHBOARD_SCRIPT_FILE" -o -e "$DASHBOARD_SCRIPT_FILE" -o -x "$DASHBOARD_SCRIPT_FILE" ] || { echo "Error: config directive DASHBOARD_SCRIPT_FILE is not defined correctly" 1>&2; exit 1; }
46+
[ -n "$TASKSQUEUE_SCRIPT_FILE" -o -e "$TASKSQUEUE_SCRIPT_FILE" -o -x "$TASKSQUEUE_SCRIPT_FILE" ] || { echo "Error: config directive TASKSQUEUE_SCRIPT_FILE is not defined correctly" 1>&2; exit 1; }
3047

3148
# Config check (script files)
32-
[ -n "$WORKERS_PID_FILE" ] || { echo "Error: config directive WORKERS_PID_FILE not defined correctly"; exit 1;}
33-
[ -n "$TASKSQUEUE_PID_FILE" ] || { echo "Error: config directive TASKSQUEUE_PID_FILE not defined correctly"; exit 1;}
34-
[ -n "$TASKS_DELAYED_FILE" ] || { echo "Error: config directive TASKS_DELAYED_FILE not defined correctly"; exit 1;}
35-
[ -n "$TASKS_DELAYED_FILE_LOCK" ] || { echo "Error: config directive TASKS_DELAYED_FILE_LOCK not defined correctly"; exit 1;}
36-
[ -n "$TASKS_TRANSPORT_PIPE" ] || { echo "Error: config directive TASKS_TRANSPORT_PIPE not defined correctly"; exit 1;}
37-
[ -n "$TASKS_TRANSPORT_PIPE_LOCK" ] || { echo "Error: config directive TASKS_TRANSPORT_PIPE_LOCK not defined correctly"; exit 1;}
49+
[ -n "$WORKERS_PID_FILE" ] || { echo "Error: config directive WORKERS_PID_FILE is not defined correctly" 1>&2; exit 1; }
50+
[ -n "$TASKSQUEUE_PID_FILE" ] || { echo "Error: config directive TASKSQUEUE_PID_FILE is not defined correctly" 1>&2; exit 1; }
51+
[ -n "$TASKS_FILE" ] || { echo "Error: config directive TASKS_FILE is not defined correctly" 1>&2; exit 1; }
52+
[ -n "$TASKS_PIPE" ] || { echo "Error: config directive TASKS_PIPE is not defined correctly" 1>&2; exit 1; }
3853

39-
# Dependencies check
40-
WC=$(type -P wc) || { echo "Error: wc not found"; exit 2;}
41-
PS=$(type -P ps) || { echo "Error: ps not found"; exit 2;}
42-
RM=$(type -P rm) || { echo "Error: rm not found"; exit 2;}
43-
CAT=$(type -P cat) || { echo "Error: cat not found"; exit 2;}
44-
SED=$(type -P sed) || { echo "Error: sed not found"; exit 2;}
45-
DATE=$(type -P date) || { echo "Error: date not found"; exit 2;}
46-
KILL=$(type -P kill) || { echo "Error: kill not found"; exit 2;}
47-
GREP=$(type -P grep) || { echo "Error: grep not found"; exit 2;}
48-
TOUCH=$(type -P touch) || { echo "Error: touch not found"; exit 2;}
49-
MKDIR=$(type -P mkdir) || { echo "Error: mkdir not found"; exit 2;}
50-
NOHUP=$(type -P nohup) || { echo "Error: nohup not found"; exit 2;}
51-
SLEEP=$(type -P sleep) || { echo "Error: sleep not found"; exit 2;}
52-
FLOCK=$(type -P flock) || { echo "Error: flock not found"; exit 2;}
53-
FALSE=$(type -P false) || { echo "Error: false not found"; exit 2;}
54-
BASE64=$(type -P base64) || { echo "Error: base64 not found"; exit 2;}
55-
MKFIFO=$(type -P mkfifo) || { echo "Error: mkfifo not found"; exit 2;}
54+
# Create parts if needed
55+
[ -p "$TASKS_PIPE" ] || "$MKFIFO" "$TASKS_PIPE" || { echo "Error: creating pipe $TASKS_PIPE failed ($?)" 1>&2; exit 1; }
56+
[ -e "$TASKS_FILE" ] || "$TOUCH" "$TASKS_FILE" || { echo "Error: touching $TASKS_FILE failed ($?)" 1>&2; exit 1; }
5657

5758
# Logging
5859

@@ -73,7 +74,7 @@ log_warn()
7374
{
7475
local LOG_SOURCE=$1
7576
local LOG_MESSAGE=$2
76-
(( $LOG_LEVEL & 2 )) && echo "[$($DATE '+%F %T.%N')][$LOG_SOURCE $$][WARN] $LOG_MESSAGE" >> "$LOG_DIR/$LOG_SOURCE.log"
77+
(( $LOG_LEVEL & 2 )) && echo "[$($DATE '+%F %T.%N')][$LOG_SOURCE $$][WARN] $LOG_MESSAGE" >> "$LOG_DIR/$LOG_SOURCE.log"
7778
}
7879

7980
##
@@ -83,7 +84,7 @@ log_info()
8384
{
8485
local LOG_SOURCE=$1
8586
local LOG_MESSAGE=$2
86-
(( $LOG_LEVEL & 4 )) && echo "[$($DATE '+%F %T.%N')][$LOG_SOURCE $$][INFO] $LOG_MESSAGE" >> "$LOG_DIR/$LOG_SOURCE.log"
87+
(( $LOG_LEVEL & 4 )) && echo "[$($DATE '+%F %T.%N')][$LOG_SOURCE $$][INFO] $LOG_MESSAGE" >> "$LOG_DIR/$LOG_SOURCE.log"
8788
}
8889

8990
##

0 commit comments

Comments
 (0)