-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
save running mode to file when updating, recover from it when restarts
- Loading branch information
1 parent
0f8bb83
commit feb42d5
Showing
6 changed files
with
121 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package runtime | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/tendermint/tendermint/config" | ||
"github.com/tendermint/tendermint/libs/common" | ||
|
||
appConfig "github.com/binance-chain/node/app/config" | ||
"github.com/binance-chain/node/common/log" | ||
) | ||
|
||
const fileName = "recover_params.json" | ||
|
||
type runtimeParams struct { | ||
Mode Mode `json:"mode"` | ||
} | ||
|
||
func RecoverFromFile(cfg *appConfig.BinanceChainContext) error { | ||
params := mustReadFromFile(cfg.Config) | ||
var mode Mode | ||
if params == nil { | ||
mode = Mode(cfg.StartMode) | ||
} else { | ||
mode = params.Mode | ||
} | ||
err := SetRunningMode(mode) | ||
return err | ||
} | ||
|
||
func mustSaveToFile(cfg *config.Config, params *runtimeParams) { | ||
path := filepath.Join(cfg.RootDir, "config", fileName) | ||
contents, err := json.MarshalIndent(params, "", " ") | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = common.WriteFileAtomic(path, contents, 0600) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func mustReadFromFile(cfg *config.Config) *runtimeParams { | ||
path := filepath.Join(cfg.RootDir, "config", fileName) | ||
contents, err := common.ReadFile(path) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
log.Debug("path does not exist", "path", path) | ||
return nil | ||
} | ||
panic(err) | ||
} | ||
|
||
var res runtimeParams | ||
err = json.Unmarshal(contents, &res) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return &res | ||
} | ||
|