-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.sh
More file actions
81 lines (65 loc) · 2.29 KB
/
runner.sh
File metadata and controls
81 lines (65 loc) · 2.29 KB
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
#!/usr/bin/env bash
readonly TQ_DIR="$1"
source $HOME/opt/taskqueue/common.sh
while true; do
if [ ! -f "$JOBS_FILE" ]; then
echo -e "${YELLOW}[R:$$] Tasks not found.${NC}"
break
fi
# Acquire file lock
if ! acquire_lock "$LOCK_FILE"; then
return 1
fi
# Find the line number of the first un-executed job
LINE_NUM=$(grep -n -m 1 "^\[ \]" "$JOBS_FILE" | cut -d: -f1)
# Check if there are any un-executed jobs left
if [ -z "$LINE_NUM" ]; then
release_lock
break
fi
# Extract the command and append this runner's PID
JOB_LINE=$(sed -n "${LINE_NUM}p" "$JOBS_FILE")
JOB_COMMAND=$(echo "$JOB_LINE" | sed -e 's#^\[ \] ##')
SAFE_JOB_COMMAND=$(safe_quote "$JOB_COMMAND")
START_DATE=$(date +%m/%d\ %H:%M)
EXECUTING_JOB_LINE="[-] $SAFE_JOB_COMMAND [$START_DATE] [R:$$]"
# Replace the line with the executing status
sed -i "${LINE_NUM}s#.*#$EXECUTING_JOB_LINE#" "$JOBS_FILE"
release_lock
# Display runner command
echo -e "${CYAN}[R:$$] $JOB_COMMAND${NC}"
# Record start time
START=$(date +%s.%N)
# Execute the command
bash -c "$JOB_COMMAND"
STATUS=$?
# Calculate elapsed time
END=$(date +%s.%N)
ELAPSED=$(echo "$END - $START" | bc)
HOURS=$(echo "$ELAPSED/3600" | bc)
MINUTES=$(echo "($ELAPSED%3600)/60" | bc)
SECONDS=$(echo "$ELAPSED%60" | bc | awk '{printf "%.0f", $1}')
if [ $HOURS -gt 0 ]; then
ELAPSED="${HOURS}h${MINUTES}m${SECONDS}s"
elif [ $MINUTES -gt 0 ]; then
ELAPSED="${MINUTES}m${SECONDS}s"
else
ELAPSED="${SECONDS}s"
fi
# Acquire file lock
if ! acquire_lock "$LOCK_FILE"; then
return 1
fi
# Update the job status based on execution result
if [ $STATUS -eq 0 ]; then
# Update the job status to completed
sed -i "/R:$$/s#.*#[x] $SAFE_JOB_COMMAND [$START_DATE] [$ELAPSED]#" "$JOBS_FILE"
echo -e "${GREEN}[R:$$] Job finished successfully. ${YELLOW}[$ELAPSED]${NC}"
else
# Update the job status to failed
sed -i "/R:$$/s#.*#[!] $SAFE_JOB_COMMAND [$START_DATE] [$ELAPSED]#" "$JOBS_FILE"
echo -e "${RED}[R:$$] Job finished with code $STATUS. ${YELLOW}[$ELAPSED]${NC}"
fi
release_lock
done
echo -e "${CYAN}[R:$$] All jobs are completed.${NC}"