From: sr55 Date: Sat, 14 Apr 2018 19:45:02 +0000 (+0100) Subject: WinGui: Cleanup the QueueRecoveryHelper and make it a little smarter about how it... X-Git-Tag: 1.1.1~39 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9fd668e3d7a5e06ddc20e9355dac6032e0107710;p=handbrake WinGui: Cleanup the QueueRecoveryHelper and make it a little smarter about how it handles multi-instance. --- diff --git a/win/CS/HandBrakeWPF/Helpers/QueueRecoveryHelper.cs b/win/CS/HandBrakeWPF/Helpers/QueueRecoveryHelper.cs index 83494ee36..4b9aa4f81 100644 --- a/win/CS/HandBrakeWPF/Helpers/QueueRecoveryHelper.cs +++ b/win/CS/HandBrakeWPF/Helpers/QueueRecoveryHelper.cs @@ -12,14 +12,12 @@ namespace HandBrakeWPF.Helpers using System; using System.Collections.Generic; using System.Diagnostics; - using System.Globalization; using System.IO; using System.Linq; using System.Text.RegularExpressions; using System.Windows; using System.Xml.Serialization; - using HandBrake.ApplicationServices.Model; using HandBrake.ApplicationServices.Utilities; using HandBrakeWPF.Services.Interfaces; @@ -46,27 +44,29 @@ namespace HandBrakeWPF.Helpers try { string tempPath = DirectoryUtilities.GetUserStoragePath(VersionHelper.IsNightly()); - List queueFiles = new List(); DirectoryInfo info = new DirectoryInfo(tempPath); - IEnumerable logFiles = info.GetFiles("*.xml").Where(f => f.Name.StartsWith("hb_queue_recovery")); + IEnumerable foundFiles = info.GetFiles("*.xml").Where(f => f.Name.StartsWith("hb_queue_recovery")); + var queueFiles = GetFilesExcludingActiveProcesses(foundFiles); - if (!logFiles.Any()) + if (!queueFiles.Any()) { return queueFiles; - } + } List removeFiles = new List(); + List acceptedFiles = new List(); + XmlSerializer ser = new XmlSerializer(typeof(List)); - foreach (FileInfo file in logFiles) + foreach (string file in queueFiles) { try { - using (FileStream strm = new FileStream(file.FullName, FileMode.Open, FileAccess.Read)) + using (FileStream strm = new FileStream(file, FileMode.Open, FileAccess.Read)) { List list = ser.Deserialize(strm) as List; if (list != null && list.Count == 0) { - removeFiles.Add(file.FullName); + removeFiles.Add(file); } if (list != null && list.Count != 0) @@ -74,7 +74,11 @@ namespace HandBrakeWPF.Helpers List tasks = list.Where(l => l.Status != QueueItemStatus.Completed).ToList(); if (tasks.Count != 0) { - queueFiles.Add(file.Name); + acceptedFiles.Add(Path.GetFileName(file)); + } + else + { + removeFiles.Add(file); } } } @@ -85,23 +89,9 @@ namespace HandBrakeWPF.Helpers } } - // Cleanup old/unused queue files for now. - foreach (string file in removeFiles) - { - Match m = Regex.Match(file, @"([0-9]+).xml"); - if (m.Success) - { - int processId = int.Parse(m.Groups[1].ToString()); - if (GeneralUtilities.IsPidACurrentHandBrakeInstance(processId)) - { - continue; - } - } - - File.Delete(file); - } + CleanupFiles(removeFiles); - return queueFiles; + return acceptedFiles; } catch (Exception exc) { @@ -161,62 +151,58 @@ namespace HandBrakeWPF.Helpers bool isRecovered = false; foreach (string file in queueFiles) { - // Skip over the file if it belongs to another HandBrake instance. - Match m = Regex.Match(file, @"([0-9]+).xml"); - if (m.Success) - { - int processId = int.Parse(m.Groups[1].ToString()); - if (processId != GeneralUtilities.ProcessId && GeneralUtilities.IsPidACurrentHandBrakeInstance(processId)) - { - continue; - } - } - // Recover the Queue encodeQueue.RestoreQueue(Path.Combine(appDataPath, file)); isRecovered = true; // Cleanup - if (!file.Contains(GeneralUtilities.ProcessId.ToString(CultureInfo.InvariantCulture))) - { - try - { - // Once we load it in, remove it as we no longer need it. - File.Delete(Path.Combine(appDataPath, file)); - } - catch (Exception) - { - // Keep quite, nothing much we can do if there are problems. - // We will continue processing files. - } - } + CleanupFiles(new List { Path.Combine(appDataPath, file) }); } return isRecovered; } - else + + CleanupFiles(queueFiles); + return false; + } + + private static List GetFilesExcludingActiveProcesses(IEnumerable foundFiles) + { + List queueFiles = new List(); + + // Remove any files where we have an active instnace. + foreach (FileInfo file in foundFiles) { - foreach (string file in queueFiles) + string fileProcessId = file.Name.Replace("hb_queue_recovery", string.Empty).Replace(".xml", string.Empty); + int processId = 0; + if (!string.IsNullOrEmpty(fileProcessId) && int.TryParse(fileProcessId, out processId)) { - if (File.Exists(Path.Combine(appDataPath, file))) + if (!GeneralUtilities.IsPidACurrentHandBrakeInstance(processId)) { - // Check that the file doesn't belong to another running instance. - Match m = Regex.Match(file, @"([0-9]+).xml"); - if (m.Success) - { - int processId = int.Parse(m.Groups[1].ToString()); - if (GeneralUtilities.IsPidACurrentHandBrakeInstance(processId)) - { - continue; - } - } + queueFiles.Add(file.FullName); + } + } + } + + return queueFiles; + } - // Delete it if it doesn't - File.Delete(Path.Combine(appDataPath, file)); + private static void CleanupFiles(List removeFiles) + { + // Cleanup old/unused queue files for now. + foreach (string file in removeFiles) + { + Match m = Regex.Match(file, @"([0-9]+).xml"); + if (m.Success) + { + int processId = int.Parse(m.Groups[1].ToString()); + if (GeneralUtilities.IsPidACurrentHandBrakeInstance(processId)) + { + continue; } } - return false; + File.Delete(file); } } }