forked from TrueCloudLab/neoneo-go
cli: persist restored blocks on exit
Add grace context to execute defer's on exit.
This commit is contained in:
parent
8cb9e1d85d
commit
c880435c92
2 changed files with 15 additions and 6 deletions
|
@ -111,10 +111,6 @@ func (d *dump) add(index uint32, batch *storage.MemBatch) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dump) tryPersist(prefix string, index uint32) error {
|
func (d *dump) tryPersist(prefix string, index uint32) error {
|
||||||
if index%1000 != 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := createFile(prefix, index)
|
f, err := createFile(prefix, index)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -277,9 +277,19 @@ func restoreDB(ctx *cli.Context) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gctx := newGraceContext()
|
||||||
|
var lastIndex uint32
|
||||||
dump := newDump()
|
dump := newDump()
|
||||||
|
defer func() {
|
||||||
|
_ = dump.tryPersist(dumpDir, lastIndex)
|
||||||
|
}()
|
||||||
|
|
||||||
for ; i < skip+count; i++ {
|
for ; i < skip+count; i++ {
|
||||||
|
select {
|
||||||
|
case <-gctx.Done():
|
||||||
|
return cli.NewExitError("cancelled", 1)
|
||||||
|
default:
|
||||||
|
}
|
||||||
bytes, err := readBlock(reader)
|
bytes, err := readBlock(reader)
|
||||||
block := &block.Block{}
|
block := &block.Block{}
|
||||||
newReader := io.NewBinReaderFromBuf(bytes)
|
newReader := io.NewBinReaderFromBuf(bytes)
|
||||||
|
@ -302,11 +312,14 @@ func restoreDB(ctx *cli.Context) error {
|
||||||
if dumpDir != "" {
|
if dumpDir != "" {
|
||||||
batch := chain.LastBatch()
|
batch := chain.LastBatch()
|
||||||
dump.add(block.Index, batch)
|
dump.add(block.Index, batch)
|
||||||
|
lastIndex = block.Index
|
||||||
|
if block.Index%1000 == 0 {
|
||||||
if err := dump.tryPersist(dumpDir, block.Index); err != nil {
|
if err := dump.tryPersist(dumpDir, block.Index); err != nil {
|
||||||
return cli.NewExitError(fmt.Errorf("can't dump storage to file: %v", err), 1)
|
return cli.NewExitError(fmt.Errorf("can't dump storage to file: %v", err), 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue