protected override async Task ActionAsync(CancellationToken cancel) { var count = Prison.ReleaseEligibleInmates(Config.ReleaseUtxoFromPrisonAfter).Count(); if (count > 0) { Logger.LogInfo($"{count} UTXOs are released from prison."); } // If something changed, send prison to file. if (LastKnownChange != Prison.ChangeId) { await SerializePrisonAsync().ConfigureAwait(false); LastKnownChange = Prison.ChangeId; } }
private static Prison DeserializePrison(string prisonFilePath) { IoHelpers.EnsureContainingDirectoryExists(prisonFilePath); var inmates = new List <Inmate>(); if (File.Exists(prisonFilePath)) { try { foreach (var inmate in File.ReadAllLines(prisonFilePath).Select(Inmate.FromString)) { inmates.Add(inmate); } } catch (Exception ex) { Logger.LogError(ex); Logger.LogWarning($"Deleting {prisonFilePath}"); File.Delete(prisonFilePath); } } var prison = new Prison(inmates); var(noted, banned) = prison.CountInmates(); if (noted > 0) { Logger.LogInfo($"{noted} noted UTXOs are found in prison."); } if (banned > 0) { Logger.LogInfo($"{banned} banned UTXOs are found in prison."); } return(prison); }
public async Task SerializePrisonAsync() { IoHelpers.EnsureContainingDirectoryExists(PrisonFilePath); await File.WriteAllLinesAsync(PrisonFilePath, Prison.GetInmates().Select(x => x.ToString())).ConfigureAwait(false); }