-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiple.subr
177 lines (149 loc) · 4.85 KB
/
multiple.subr
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
176
177
#
if [ ! "$_CBSD_MULTIPLE_SUBR" ]; then
_CBSD_MULTIPLE_SUBR=1
###
## A handler for multiple generic tasks over environments
##
## typical use:
## in main program:
## a) prepare and run the necessary tasks through tasks
## b) take a TASKID list in $multiple_task_id_all global vars
## c) execute mutliple_processing_spawn -n "consumer name" -o "taskowner"
## where taskowner - uniq world for this task
## e.g:
## --
# #${ECHO} "${N1_COLOR}Hint: Press ${N2_COLOR}'Ctrl+t'${N1_COLOR} to see last logfile line for active task${N0_COLOR}" 1>&2
# task_id=
# task_id_cur=
# task_owner="pkg_install"
#
#for jname in ${jail_list}; do
# . ${jrcconf}
# [ ${myjid} -eq 0 ] && continue
# task_id_cur=$( task mode=new logfile=${tmpdir}/${task_owner}.${jname}.log.$$ client_id=${jname} autoflush=0 owner=${task_owner} /usr/bin/env NOCOLOR=1 /usr/local/bin/cbsd pkg jname=${jname} ${*} 2>/dev/null )
# task_id="${task_id} ${task_id_cur}"
#done
#
#multiple_task_id_all=$( echo ${task_id} | ${TR_CMD} " " "," )
#multiple_processing_spawn -o multi_pkg_install -n "pkg install"
## --
###
# _multiple_consumer_name as global variable for name in status string
multiple_subr_info()
{
local IFS=" "
[ -z "${_multiple_consumer_name}" ] && _multiple_consumer_name="multi"
if [ ${_pending_num} -ne 0 ]; then
_pending_state="${_pending_num}:[${_pending_jail}]"
else
_pending_state="${_pending_num}"
fi
if [ ${_active_num} -ne 0 ]; then
_active_state="${_active_num}:[${_active_jail}]"
else
_active_state="${_active_num}"
fi
printf "${CLRLINE}"
printf "${CURSORRST}"
printf "${N2_COLOR} * [${N1_COLOR}${_multiple_consumer_name}: ${N2_COLOR}pending: ${H2_COLOR}${_pending_state}${N1_COLOR}, ${H4_COLOR}active: ${H3_COLOR}${_active_state}${N2_COLOR}]${N0_COLOR}" 1>&2
}
multiple_tail_stale_log()
{
local i
local IFS="|"
cbsdsqlro ${sqlfile} SELECT logfile,jname FROM taskd WHERE owner=\"${_multiple_task_owner}\" AND id IN \( ${multiple_task_id_all} \) AND status=\"1\" | while read logfile jname; do
[ ! -r ${logfile} ] && continue
IFS=" "
${ECHO} "${N1_COLOR}Last 200 line of logfile for :${N2_COLOR}${jname}${N0_COLOR}"
echo "---cut---"
${TAIL_CMD} -n200 ${logfile}
echo
echo "---end---"
IFS="|"
done
}
# -n "name of the tools" - show <name> in Info string, e.g: -n jexec, -n "pkg install" ...
# -o uniq_name_of_the_task (one world)
# -s silence - no info/any output (error only)
multiple_processing_spawn()
{
local _ret _silence=0
while getopts "n:o:s" opt; do
case "${opt}" in
n) _multiple_consumer_name="${OPTARG}" ;;
o) _multiple_task_owner="${OPTARG}" ;;
s) _silence=1 ;;
esac
shift $(($OPTIND - 1))
done
[ -z "${_multiple_task_owner}" ] && err 1 "${N1_COLOR}multiple_processing_spawn: empty -o multiple_task_owner${N0_COLOR}"
[ -z "${multiple_task_id_all}" ] && err 1 "${N1_COLOR}multiple_processing_spawn: empty multiple_task_id_all global variable${N0_COLOR}"
sqlfile="cbsdtaskd"
_ret=1
trap 'multiple_tail_stale_log' SIGINFO
while [ ${_ret} -ne 0 ]; do
_pending_num=0
_active_num=0
_pending_jail=
_active_jail=
# and control task for each task execution
OIFS=${IFS}
IFS="|"
cbsdsqlro ${sqlfile} SELECT id,status,logfile,errcode,jname,st_time,end_time FROM taskd WHERE owner=\"${_multiple_task_owner}\" AND id IN \( ${multiple_task_id_all} \) | while read _id _status _logfile _errcode _jname _st_time _end_time; do
IFS=${OIFS}
if [ ${_status} -eq 0 ]; then
_pending_num=$(( _pending_num + 1 ))
_pending_jail="${_pending_jail} ${_jname}"
fi
if [ ${_status} -eq 1 ]; then
_active_num=$(( _active_num + 1 ))
_active_jail="${_active_jail} ${_jname}"
fi
if [ ${_status} -eq 2 ]; then
tasktime=$(( _end_time - _st_time ))
case ${_errcode} in
0)
STATE="${N2_COLOR}success in ${tasktime} sec:${N0_COLOR}"
;;
*)
STATE="${W1_COLOR}failed in ${tasktime} sec:${N0_COLOR}"
;;
esac
[ ${_silence} -ne 1 ] && echo
[ ${_silence} -ne 1 ] && ${ECHO} "${H3_COLOR}${_jname}${N2_COLOR}: ${STATE}${N0_COLOR}"
if [ -f ${_logfile} ]; then
[ ${_silence} -ne 1 ] && echo
[ ${_silence} -ne 1 ] && ${CAT_CMD} ${_logfile}
[ ${_silence} -ne 1 ] && echo
${RM_CMD} -f ${_logfile}
fi
cbsdsqlrw ${sqlfile} DELETE FROM taskd WHERE id=\"${_id}\"
else
${CAT_CMD} > ${tmpdir}/cbsd_multiple.lock.$$ <<EOF
_pending_num="${_pending_num}"
_active_num="${_active_num}"
_pending_jail="${_pending_jail}"
_active_jail="${_active_jail}"
EOF
fi
IFS="|"
done
IFS=${OIFS}
if [ -r ${tmpdir}/cbsd_multiple.lock.$$ ]; then
. ${tmpdir}/cbsd_multiple.lock.$$
[ ${_silence} -ne 1 ] && multiple_subr_info
# Waiting for other task
${RM_CMD} -f ${tmpdir}/cbsd_multiple.lock.$$
# push changes to cbsdd
[ ${_ret} -eq 1 ] && ${TOUCH_CMD} ${dbdir}/cbsdtaskd.sqlite
sleep 1
else
# all finished, exit
_ret=0
fi
done
[ ${_silence} -ne 1 ] && echo
return 0
}
###
fi