Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend/Caching: save(&restore) .bak files #281

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/GWallet.Backend/Caching.fs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,20 @@ module Caching =
Object.ReferenceEquals(x, null)

let private LoadFromDisk (files: CacheFiles): bool*CachedNetworkData*ServerRanking =
let maybeNetworkData = LoadFromDiskInternal<CachedNetworkData> files.CachedNetworkData
let networkDataBackup = SPrintF1 "%s.bak" files.CachedNetworkData.FullName |> FileInfo
let maybeNetworkData =
try
LoadFromDiskInternal<CachedNetworkData> files.CachedNetworkData
with
// data become corrupted somehow
| InvalidJson _ ->
if networkDataBackup.Exists then
let res = LoadFromDiskInternal<CachedNetworkData> networkDataBackup
networkDataBackup.CopyTo(files.CachedNetworkData.FullName, true) |> ignore<FileInfo>
res
else
reraise()

let maybeFirstRun,resultingNetworkData =
match maybeNetworkData with
| None ->
Expand All @@ -142,13 +155,27 @@ module Caching =
Infrastructure.LogError droppedCachedMsgWarning
true,CachedNetworkData.Empty
else
files.CachedNetworkData.CopyTo(networkDataBackup.FullName, true) |> ignore<FileInfo>
false,networkData

let maybeServerStats = LoadFromDiskInternal<ServerRanking> files.ServerStats
let serverStatsBackup = SPrintF1 "%s.bak" files.ServerStats.FullName |> FileInfo
let maybeServerStats =
try
LoadFromDiskInternal<ServerRanking> files.ServerStats
with
// data become corrupted somehow
| InvalidJson _ ->
if serverStatsBackup.Exists then
let res = LoadFromDiskInternal<ServerRanking> serverStatsBackup
serverStatsBackup.CopyTo(files.CachedNetworkData.FullName, true) |> ignore<FileInfo>
res
else
reraise()
match maybeServerStats with
| None ->
maybeFirstRun,resultingNetworkData,Map.empty
| Some serverStats ->
files.ServerStats.CopyTo(serverStatsBackup.FullName, true) |> ignore<FileInfo>
false,resultingNetworkData,serverStats

let rec private MergeRatesInternal (oldMap: Map<'K, CachedValue<'V>>)
Expand Down
2 changes: 1 addition & 1 deletion src/GWallet.Backend/Exceptions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ exception AccountAlreadyAdded

exception InvalidDestinationAddress of msg: string

exception InvalidJson
exception InvalidJson of content: string

exception TransactionAlreadySigned
exception TransactionNotSignedYet
Expand Down
2 changes: 1 addition & 1 deletion src/GWallet.Backend/Marshalling.fs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ module Marshalling =
if (String.IsNullOrWhiteSpace(json)) then
raise (ArgumentException("empty or whitespace json", "json"))
if not (IsValidJson json) then
raise <| InvalidJson
raise <| InvalidJson json

let deserialized =
try
Expand Down
2 changes: 1 addition & 1 deletion src/GWallet.Frontend.Console/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ module Program =
Marshalling.Deserialize watchWalletInfoJson
|> Some
with
| InvalidJson ->
| InvalidJson _content ->
None

match watchWalletInfoOpt with
Expand Down
Loading