Skip to content
Snippets Groups Projects
Commit 84cef848 authored by Robert Martin-Legene's avatar Robert Martin-Legene
Browse files

Let geth create a clean synced copy at startup, before starting up for full service

parent ae252b1a
No related branches found
No related tags found
No related merge requests found
...@@ -10,39 +10,42 @@ bootnodekeyfile=${BFANETWORKDIR}/bootnode/key ...@@ -10,39 +10,42 @@ bootnodekeyfile=${BFANETWORKDIR}/bootnode/key
# Bail out if anything fails. # Bail out if anything fails.
trap "exit 1" ERR trap "exit 1" ERR
# Detect children dying # Detect children dying
trap "reaper" SIGINT SIGCHLD trap "reaper" SIGCHLD
unset LOGDIR LOGPIPE PIDIDX trap "killprocs" SIGTERM
declare -A PIDIDX trap "killallprocs" EXIT
trap "killallprocs" SIGTERM unset LOGPIPE PIDIDX ALLPIDIDX TMPDIR SHUTTING_DOWN
declare -A PIDIDX ALLPIDIDX
SHUTTING_DOWN=0
function reaper() function reaper()
{ {
local wecare=0 local reaped_a_child_status=0
# Find out which child died
for pid in ${!PIDIDX[*]} for pid in ${!PIDIDX[*]}
do do
kill -0 $pid 2>/dev/null && continue kill -0 $pid 2>/dev/null && continue
reaped_a_child_status=1
local rc=0 local rc=0
wait $pid || rc=$? || true wait $pid || rc=$? || true
echo "*** ${PIDIDX[$pid]} (pid $pid) has exited with value $rc" local name=${PIDIDX[$pid]}
echo "*** $name (pid $pid) has exited with value $rc"
if [ $name = 'geth' ]; then geth_rc_status=$rc; fi
unset PIDIDX[$pid] unset PIDIDX[$pid]
wecare=1
done done
test $wecare = 1 || return # Return if we didn't find out which child died.
# kill all - an extra kill doesn't hurt. I hope. if [ $reaped_a_child_status = 0 ]; then return; fi
# Kill all other registered processes
for pid in ${!PIDIDX[*]} for pid in ${!PIDIDX[*]}
do do
# don't kill log.sh, because it may still be logging for us kill -0 $pid 2>/dev/null || continue
if [ "${PIDIDX[$pid]}" != "log.sh" ] echo "*** Killing ${PIDIDX[$pid]} (pid $pid)."
then kill $pid || true
echo "*** Killing ${PIDIDX[$pid]} (pid $pid)."
kill $pid || true
fi
done done
max=30
} }
function killallprocs() function killprocs()
{ {
SHUTTING_DOWN=1
if [ ${#PIDIDX[*]} -gt 0 ] if [ ${#PIDIDX[*]} -gt 0 ]
then then
echo "*** Killing all remaining processes: ${PIDIDX[*]} (${!PIDIDX[*]})." echo "*** Killing all remaining processes: ${PIDIDX[*]} (${!PIDIDX[*]})."
...@@ -50,6 +53,30 @@ function killallprocs() ...@@ -50,6 +53,30 @@ function killallprocs()
fi fi
} }
function killallprocs()
{
# merge the 2 pid list, to make killing and echoing easier
for pid in ${!ALLPIDIDX[*]}
do
PIDIDX[$pid]=${ALLPIDIDX[$pid]}
unset ALLPIDIDX[$pid]
done
killprocs
if [ -n "$TMPDIR" -a -e "$TMPDIR" ]
then
rm -rf "$TMPDIR"
fi
}
function startgeth()
{
echo "***" Starting geth $*
# "NoPruning=true" means "--gcmode archive"
geth --config ${BFATOML} $* &
gethpid=$!
PIDIDX[${gethpid}]="geth"
}
# You can start as: # You can start as:
# BFAHOME=/home/bfa/bfa singlestart.sh # BFAHOME=/home/bfa/bfa singlestart.sh
# singlestart.sh /home/bfa/bfa # singlestart.sh /home/bfa/bfa
...@@ -75,8 +102,19 @@ else ...@@ -75,8 +102,19 @@ else
fi fi
source ${BFAHOME}/bin/libbfa.sh source ${BFAHOME}/bin/libbfa.sh
TMPDIR=$( mktemp -d )
mknod ${TMPDIR}/sleeppipe p
sleep 987654321 > ${TMPDIR}/sleeppipe &
ALLPIDIDX[$!]='sleep'
if [ "$VIRTUALIZATION" = "DOCKER" ] if [ "$VIRTUALIZATION" = "DOCKER" ]
then then
echo
echo
echo
echo
date
echo $0 startup
echo
echo "See log info with \"docker logs\"" echo "See log info with \"docker logs\""
else else
echo "Logging mostly everything to ${BFANODEDIR}/log" echo "Logging mostly everything to ${BFANODEDIR}/log"
...@@ -86,21 +124,40 @@ else ...@@ -86,21 +124,40 @@ else
# Docker has it's own logging facility, so we will not use our own # Docker has it's own logging facility, so we will not use our own
# logging functionality if we're in docker. # logging functionality if we're in docker.
echo "*** Setting up logging." echo "*** Setting up logging."
# Clean up logging LOGPIPE=${TMPDIR}/logpipe
LOGDIR=$( mktemp -d )
trap "rm -rf ${LOGDIR}" EXIT
LOGPIPE=${LOGDIR}/logpipe
mknod ${LOGPIPE} p mknod ${LOGPIPE} p
${BFAHOME}/bin/log.sh ${BFANODEDIR}/log < ${LOGPIPE} & ${BFAHOME}/bin/log.sh ${BFANODEDIR}/log < ${LOGPIPE} &
PIDIDX[$!]="log.sh" # Separate pididx for processes we don't want to send signals to
# until in the very end.
ALLPIDIDX[$!]="log.sh"
exec > ${LOGPIPE} 2>&1 exec > ${LOGPIPE} 2>&1
fi fi
echo "*** Starting geth." function sleep()
# "NoPruning=true" means "--gcmode archive" {
geth --config ${BFATOML} & read -t ${1:-1} < ${TMPDIR}/sleeppipe || true
PIDIDX[$!]="geth" }
# Start a sync
startgeth --exitwhensynced
echo "*** Starting monitor.js"
monitor.js &
PIDIDX[$!]="monitor.js"
# geth will exit when it has synced, then we kill it's monitor.
# Then wait here for their exit status to get reaped
while [ "${#PIDIDX[*]}" -gt 0 ]; do sleep 1; done
# if it went well, start a normal geth (to run "forever")
test $geth_rc_status = 0 || exit $geth_rc_status
test "$SHUTTING_DOWN" != 0 && exit 0
# regular geth
startgeth
# monitor
echo "*** Starting monitor.js"
monitor.js &
PIDIDX[$!]="monitor.js"
# bootnode # bootnode
if [ -r "$bootnodekeyfile" ] if [ -r "$bootnodekeyfile" ]
then then
...@@ -109,16 +166,4 @@ then ...@@ -109,16 +166,4 @@ then
PIDIDX[$!]="bootnode" PIDIDX[$!]="bootnode"
fi fi
echo "*** Starting monitor.js" while [ "${#PIDIDX[*]}" -gt 0 ]; do sleep 1; done
monitor.js &
PIDIDX[$!]="monitor.js"
max=-1
# Exit if only 1 process remains - we hope it is log.sh, but either way,
# it is time to end.
while [ "${#PIDIDX[*]}" -gt 1 -a $max -ne 0 ]
do
sleep 1
max=$(( $max - 1 ))
done
killallprocs
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment