Skip to content

Commit

Permalink
WinGui: Log rather than crash when a queue backup fails for whatever …
Browse files Browse the repository at this point in the history
…reason. #4079

(cherry picked from commit b215df6)
  • Loading branch information
sr55 committed Jan 10, 2022
1 parent ea9727d commit 09690d6
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions win/CS/HandBrakeWPF/Services/Queue/QueueService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,36 @@ public void BackupQueue(string exportPath)
{
lock (this.queueFileLock)
{
string appDataPath = DirectoryUtilities.GetUserStoragePath(HandBrakeVersionHelper.IsNightly());
string tempPath = !string.IsNullOrEmpty(exportPath)
? exportPath
: Path.Combine(appDataPath, string.Format(this.queueFile, string.Empty));

// Make a copy of the file before we replace it. This way, if we crash we can recover.
if (File.Exists(tempPath))
try
{
File.Copy(tempPath, tempPath + ".last", true);
}
string appDataPath = DirectoryUtilities.GetUserStoragePath(HandBrakeVersionHelper.IsNightly());
string tempPath = !string.IsNullOrEmpty(exportPath)
? exportPath
: Path.Combine(appDataPath, string.Format(this.queueFile, string.Empty));

using (StreamWriter writer = new StreamWriter(tempPath))
{
List<QueueTask> tasks = this.queue.Where(item => item.Status != QueueItemStatus.Completed).ToList();
// Make a copy of the file before we replace it. This way, if we crash we can recover.
if (File.Exists(tempPath))
{
File.Copy(tempPath, tempPath + ".last", true);
}

string queueJson = JsonSerializer.Serialize(tasks, JsonSettings.Options);
writer.Write(queueJson);
}
using (StreamWriter writer = new StreamWriter(tempPath))
{
List<QueueTask> tasks = this.queue.Where(item => item.Status != QueueItemStatus.Completed)
.ToList();

string queueJson = JsonSerializer.Serialize(tasks, JsonSettings.Options);
writer.Write(queueJson);
}

if (File.Exists(tempPath + ".last"))
if (File.Exists(tempPath + ".last"))
{
File.Delete(tempPath + ".last");
}
}
catch (Exception exc)
{
File.Delete(tempPath + ".last");
this.logService.LogMessage(string.Format("{1} # {0}{1} {2}{1}", "Queue Backup Failed!", Environment.NewLine, exc));
}
}
}
Expand Down

0 comments on commit 09690d6

Please sign in to comment.