-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.sh
executable file
·80 lines (62 loc) · 2.1 KB
/
functions.sh
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
#!/bin/bash
exit_function() {
MESSAGE=$1
CODE=$2
STATUS=$([ "$2" == 0 ] && echo "OK" || echo "Error")
echo "${STATUS}: ${MESSAGE}"
exit "$CODE"
}
export table_doesnt_exist="exit_function 'table does not exist' 1"
export table_exists="exit_function 'table already exists' 1"
export db_doesnt_exist="exit_function 'DB does not exist' 1"
export db_exists="exit_function 'DB already exists' 1"
export column_doesnt_exist="exit_function 'column does not exist' 1"
export schema_mismatch="exit_function 'number of columns in tuple does not match schema' 1"
export param_problem="exit_function 'parameter problem' 1"
export no_param="exit_function 'no parameter' 1"
export db_created="exit_function 'database created' 0"
export table_created="exit_function 'table created' 0"
export tuple_inserted="exit_function 'tuple inserted' 0"
export bad_request="exit_function 'bad request' 1"
export timeout="exit_function 'timeout' 1"
export server_pipe_exists="exit_function 'server pipe exists' 1"
export server_pipe_doesnt_exist="exit_function 'server pipe doesnt exist' 1"
# Separate a string using ',', then count the fields.
count_fields() {
IFS=','
read -ra line_array <<<"$1"
echo "${#line_array[@]}"
}
attempt_work() {
needed_file="$1"
pre_lock="$2"
post_lock="$3"
timeout=true
lock_name="${needed_file}.lock"
wait_time=$(bc -l <<< '1.0 / 2^7')
while [ "$(printf "%.0f" "$wait_time")" -lt $(( 2 ** 3 )) ]; do
# noclobbber causes pipe to return failure if file exists.
if ( set -o noclobber; echo "locked" > "$lock_name") 2> /dev/null; then
# If something bad happens remove the lock and exit with last seen status code
trap 'rm -f "$lock_name"; exit $?' INT TERM EXIT
# Ok actually do the work
eval "$pre_lock"
# ls company
# sleep 1
# Done with the lock
rm -f "$lock_name"
# ls company
# Finish and quit
eval "$post_lock"
timeout=false
break
else
sleep "$wait_time"
# Exponential back off
wait_time=$(bc -l <<< "${wait_time} * 2")
fi
done
if "$timeout"; then
eval "${timeout}"
fi
}