cli: persist restored blocks on exit

Add grace context to execute defer's on exit.
This commit is contained in:
Evgenii Stratonikov 2020-03-16 12:00:22 +03:00
parent 8cb9e1d85d
commit c880435c92
2 changed files with 15 additions and 6 deletions

View file

@ -111,10 +111,6 @@ func (d *dump) add(index uint32, batch *storage.MemBatch) {
}
func (d *dump) tryPersist(prefix string, index uint32) error {
if index%1000 != 0 {
return nil
}
f, err := createFile(prefix, index)
if err != nil {
return err

View file

@ -277,9 +277,19 @@ func restoreDB(ctx *cli.Context) error {
}
}
gctx := newGraceContext()
var lastIndex uint32
dump := newDump()
defer func() {
_ = dump.tryPersist(dumpDir, lastIndex)
}()
for ; i < skip+count; i++ {
select {
case <-gctx.Done():
return cli.NewExitError("cancelled", 1)
default:
}
bytes, err := readBlock(reader)
block := &block.Block{}
newReader := io.NewBinReaderFromBuf(bytes)
@ -302,8 +312,11 @@ func restoreDB(ctx *cli.Context) error {
if dumpDir != "" {
batch := chain.LastBatch()
dump.add(block.Index, batch)
if err := dump.tryPersist(dumpDir, block.Index); err != nil {
return cli.NewExitError(fmt.Errorf("can't dump storage to file: %v", err), 1)
lastIndex = block.Index
if block.Index%1000 == 0 {
if err := dump.tryPersist(dumpDir, block.Index); err != nil {
return cli.NewExitError(fmt.Errorf("can't dump storage to file: %v", err), 1)
}
}
}
}