-
Notifications
You must be signed in to change notification settings - Fork 2
/
queuedb.sh
175 lines (153 loc) · 4.35 KB
/
queuedb.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/bash
##
## This script must be sourced with:
## $1 - Path to db file
## $2 - Path to database file lock
## $3 - Database file lock timeout
##
# Arguments
QUEUEDB_DB_FILE=$1
QUEUEDB_DB_LOCK=$2
QUEUEDB_DB_LOCK_TIMEOUT=$3
# Check arguments
[ -n "$QUEUEDB_DB_FILE" -a -r "$QUEUEDB_DB_FILE" -a -w "$QUEUEDB_DB_FILE" ] || return 2
[ -n "$QUEUEDB_DB_LOCK_TIMEOUT" ] || return 2
##
## Pops data from DB file and sets up RESULT variable with row data
##
## Return:
## 0 - When all ok
## 1 - When lock failed or timed out
## 2 - When read failed [f.e. db is empty]
## 3 - When write failed
##
queuedb_pop()
{
unset -v RESULT
log_debug "queuedb" "Popping row from [$QUEUEDB_DB_FILE] ..."
{
if ! flock -x -w "$QUEUEDB_DB_LOCK_TIMEOUT" 200
then
log_debug "queuedb" "Popping row from [$QUEUEDB_DB_FILE] failed (Lock with timeout [$QUEUEDB_DB_LOCK_TIMEOUT] failed with code [$?])"
return 1
fi
local ROW_DATA
if ! read -a ROW_DATA 0<"$QUEUEDB_DB_FILE"
then
log_debug "queuedb" "Popping row from [$QUEUEDB_DB_FILE] failed (Read failed with code [$?])"
return 2
fi
if ! sed -i "1d" "$QUEUEDB_DB_FILE"
then
log_debug "queuedb" "Popping row from [$QUEUEDB_DB_FILE] failed (Sed failed with code [$?])"
return 3
fi
log_debug "queuedb" "Popping row from [$QUEUEDB_DB_FILE] ok"
RESULT=("${ROW_DATA[@]}")
return 0
} 200<"$QUEUEDB_DB_LOCK"
}
##
## Pushes data to DB file and sets up RESULT variable with row is
##
## Return:
## 0 - When all ok
## 1 - When lock failed or timed out
## 2 - When write failed
##
queuedb_push()
{
unset -v RESULT
local ROW_ID=$(date '+%s%N')
local ROW_DATA=("$@")
log_debug "queuedb" "Pushing row [$ROW_ID] to [$QUEUEDB_DB_FILE] ..."
{
if ! flock -x -w "$QUEUEDB_DB_LOCK_TIMEOUT" 200
then
log_debug "queuedb" "Pushing row [$ROW_ID] to [$QUEUEDB_DB_FILE] failed (Lock with timeout [$QUEUEDB_DB_LOCK_TIMEOUT] failed with code [$?])"
return 1
fi
if ! echo $(printf "%q " "$ROW_ID" "${ROW_DATA[@]}") 1>>"$QUEUEDB_DB_FILE"
then
log_debug "queuedb" "Pushing row [$ROW_ID] to [$QUEUEDB_DB_FILE] failed (Write failed with code [$?])"
return 2
fi
log_debug "queuedb" "Pushing row [$ROW_ID] to [$QUEUEDB_DB_FILE] ok"
RESULT=$ROW_ID
return 0
} 200<"$QUEUEDB_DB_LOCK"
}
##
## Selects data in DB file and sets up RESULT variable with found row data
##
## Return:
## 0 - When all ok
## 1 - When lock failed or timed out
## 2 - When grep failed (f.e. not found)
## 3 - When read failed
##
queuedb_find()
{
unset -v RESULT
local ROW_ID=$1
log_debug "queuedb" "Selecting row [$ROW_ID] from [$QUEUEDB_DB_FILE] ..."
{
if ! flock -x -w "$QUEUEDB_DB_LOCK_TIMEOUT" 200
then
log_debug "queuedb" "Selecting row [$ROW_ID] from [$QUEUEDB_DB_FILE] failed (Lock with timeout [$QUEUEDB_DB_LOCK_TIMEOUT] failed with code [$?])"
return 1
fi
local ROW_LINE=$(grep "^$ROW_ID\s" "$QUEUEDB_DB_FILE")
if ! [ -n "$ROW_LINE" ]
then
log_debug "queuedb" "Selecting row [$ROW_ID] from [$QUEUEDB_DB_FILE] failed (Grep failed with code [$?])"
return 2
fi
local ROW_DATA
if ! read -a ROW_DATA 0<<<$ROW_LINE
then
log_debug "queuedb" "Selecting row [$ROW_ID] from [$QUEUEDB_DB_FILE] failed (Read failed with code [$?])"
return 3
fi
log_debug "queuedb" "Selecting row [$ROW_ID] from [$QUEUEDB_DB_FILE] ok"
RESULT=("${ROW_DATA[@]}")
return 0
} 200<"$QUEUEDB_DB_LOCK"
}
##
## Removes data from DB file with specified row id
##
## Params:
## $1 - Row id
##
## Return:
## 0 - When all ok
## 1 - When lock failed or timed out
## 2 - When row not found
## 3 - When write failed
##
queuedb_remove()
{
unset -v RESULT
local ROW_ID=$1
log_debug "queuedb" "Removing row [$ROW_ID] from [$QUEUEDB_DB_FILE] ..."
{
if ! flock -x -w "$QUEUEDB_DB_LOCK_TIMEOUT" 200
then
log_debug "queuedb" "Removing row [$ROW_ID] from [$QUEUEDB_DB_FILE] failed (Lock with timeout [$QUEUEDB_DB_LOCK_TIMEOUT] failed with code [$?])"
return 1
fi
if ! grep -q "^$TASK_ID\s" "$QUEUEDB_DB_FILE"
then
log_debug "queuedb" "Removing row [$ROW_ID] from [$QUEUEDB_DB_FILE] failed (Grep failed with code [$?])"
return 2
fi
if ! sed -i "/^$TASK_ID\s/d" "$QUEUEDB_DB_FILE"
then
log_debug "queuedb" "Removing row [$ROW_ID] from [$QUEUEDB_DB_FILE] failed (Sed failed with code [$?])"
return 3
fi
log_debug "queuedb" "Removing row [$ROW_ID] from [$QUEUEDB_DB_FILE] ok"
return 0
} 200<"$QUEUEDB_DB_LOCK"
}