Handle OS interrupt in resyncIndex

This commit is contained in:
Martin Boehm 2018-03-01 00:59:25 +01:00
parent f8fbbcfe29
commit b556d0354e

View File

@ -3,7 +3,9 @@ package main
import ( import (
"context" "context"
"encoding/hex" "encoding/hex"
"errors"
"flag" "flag"
"fmt"
"os" "os"
"os/signal" "os/signal"
"sync" "sync"
@ -74,6 +76,7 @@ var (
index *db.RocksDB index *db.RocksDB
callbacksOnNewBlockHash []func(hash string) callbacksOnNewBlockHash []func(hash string)
callbacksOnNewTxAddr []func(txid string, addr string) callbacksOnNewTxAddr []func(txid string, addr string)
chanOsSignal chan os.Signal
) )
func main() { func main() {
@ -84,6 +87,9 @@ func main() {
defer glog.Flush() defer glog.Flush()
chanOsSignal = make(chan os.Signal, 1)
signal.Notify(chanOsSignal, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
if *prof { if *prof {
defer profile.Start().Stop() defer profile.Start().Stop()
} }
@ -308,10 +314,7 @@ func mqHandler(m *bchain.MQMessage) {
} }
func waitForSignalAndShutdown(https *server.HTTPServer, socketio *server.SocketIoServer, mq *bchain.MQ, timeout time.Duration) { func waitForSignalAndShutdown(https *server.HTTPServer, socketio *server.SocketIoServer, mq *bchain.MQ, timeout time.Duration) {
stop := make(chan os.Signal, 1) sig := <-chanOsSignal
signal.Notify(stop, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
sig := <-stop
ctx, cancel := context.WithTimeout(context.Background(), timeout) ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel() defer cancel()
@ -513,16 +516,37 @@ func connectBlocksParallel(
wg.Add(1) wg.Add(1)
go work(i) go work(i)
} }
var hash string
for h := lower; h <= higher; h++ { var hash string
hash, err = chain.GetBlockHash(h) ConnectLoop:
if err != nil { for h := lower; h <= higher; {
break select {
} case <-chanOsSignal:
hch <- hashHeight{hash, h} // wait for the workers to finish block
if h > 0 && h%1000 == 0 { WaitAgain:
glog.Info("connecting block ", h, " ", hash) for {
for _, r := range running {
if r {
glog.Info("Waiting for workers to finish ", running)
time.Sleep(time.Millisecond * 500)
continue WaitAgain
}
}
err = errors.New(fmt.Sprint("connectBlocksParallel interrupted at height ", h))
break ConnectLoop
}
default:
hash, err = chain.GetBlockHash(h)
if err != nil {
glog.Error("GetBlockHash ", h, " error ", err)
time.Sleep(time.Millisecond * 500)
continue
}
hch <- hashHeight{hash, h}
if h > 0 && h%1000 == 0 {
glog.Info("connecting block ", h, " ", hash)
}
h++
} }
} }
close(hch) close(hch)