diff options
author | sr55 <[email protected]> | 2012-10-13 19:48:12 +0000 |
---|---|---|
committer | sr55 <[email protected]> | 2012-10-13 19:48:12 +0000 |
commit | 029c9b1db2fbf86fc8ded15580cd8023e4b180fc (patch) | |
tree | 227743486b489e989f0b1c67234ebefa4316b10c | |
parent | 2a18d05b52ea084b4bb968f3622872a8a08d83fa (diff) |
WinGui: Refactor multi-instance support to use Process ID. Fix old log file cleanup function.
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@5016 b64f7644-9d1e-0410-96f1-a4d463321fa5
8 files changed, 78 insertions, 70 deletions
diff --git a/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs b/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs index 8cca6e7e5..8ef67b28d 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs @@ -213,7 +213,7 @@ namespace HandBrake.ApplicationServices.Services.Base {
string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
"\\HandBrake\\logs";
- string tempLogFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", GeneralUtilities.GetInstanceCount));
+ string tempLogFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", GeneralUtilities.ProcessId));
string encodeDestinationPath = Path.GetDirectoryName(destination);
string destinationFile = Path.GetFileName(destination);
@@ -259,8 +259,8 @@ namespace HandBrake.ApplicationServices.Services.Base {
ShutdownFileWriter();
string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
- string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", GeneralUtilities.GetInstanceCount));
- string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", GeneralUtilities.GetInstanceCount));
+ string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", GeneralUtilities.ProcessId));
+ string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", GeneralUtilities.ProcessId));
try
{
diff --git a/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IQueueProcessor.cs b/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IQueueProcessor.cs index c9285ebfe..0a952e516 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IQueueProcessor.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IQueueProcessor.cs @@ -154,11 +154,6 @@ namespace HandBrake.ApplicationServices.Services.Interfaces void Remove(QueueTask job);
/// <summary>
- /// Temp workaround until this can be fixed properly.
- /// </summary>
- void ResetInstanceId();
-
- /// <summary>
/// Reset a Queued Item from Error or Completed to Waiting
/// </summary>
/// <param name="job">
diff --git a/win/CS/HandBrake.ApplicationServices/Services/QueueProcessor.cs b/win/CS/HandBrake.ApplicationServices/Services/QueueProcessor.cs index 0b3290839..7aefd285f 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/QueueProcessor.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/QueueProcessor.cs @@ -75,7 +75,7 @@ namespace HandBrake.ApplicationServices.Services this.EncodeService = encodeService;
// If this is the first instance, just use the main queue file, otherwise add the instance id to the filename.
- this.queueFile = string.Format("hb_queue_recovery{0}.xml", GeneralUtilities.GetInstanceCount);
+ this.queueFile = string.Format("hb_queue_recovery{0}.xml", GeneralUtilities.ProcessId);
}
#endregion
@@ -331,14 +331,6 @@ namespace HandBrake.ApplicationServices.Services }
/// <summary>
- /// Temp workaround until this can be fixed properly.
- /// </summary>
- public void ResetInstanceId()
- {
- this.queueFile = string.Format("hb_queue_recovery{0}.xml", GeneralUtilities.GetInstanceCount);
- }
-
- /// <summary>
/// Reset a Queued Item from Error or Completed to Waiting
/// </summary>
/// <param name="job">
diff --git a/win/CS/HandBrake.ApplicationServices/Services/ScanService.cs b/win/CS/HandBrake.ApplicationServices/Services/ScanService.cs index bb4ec44c6..e7c7a2852 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/ScanService.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/ScanService.cs @@ -246,7 +246,7 @@ namespace HandBrake.ApplicationServices.Services "\\HandBrake\\logs";
string dvdInfoPath = Path.Combine(
logDir,
- string.Format("last_scan_log{0}.txt", GeneralUtilities.GetInstanceCount));
+ string.Format("last_scan_log{0}.txt", GeneralUtilities.ProcessId));
if (!Directory.Exists(logDir))
{
diff --git a/win/CS/HandBrake.ApplicationServices/Utilities/GeneralUtilities.cs b/win/CS/HandBrake.ApplicationServices/Utilities/GeneralUtilities.cs index bebdc734d..378898c05 100644 --- a/win/CS/HandBrake.ApplicationServices/Utilities/GeneralUtilities.cs +++ b/win/CS/HandBrake.ApplicationServices/Utilities/GeneralUtilities.cs @@ -14,14 +14,12 @@ namespace HandBrake.ApplicationServices.Utilities using System.Diagnostics;
using System.Globalization;
using System.IO;
+ using System.Linq;
using System.Text;
+ using System.Text.RegularExpressions;
using System.Windows.Forms;
- using Caliburn.Micro;
-
using HandBrake.ApplicationServices.Model;
- using HandBrake.ApplicationServices.Services;
- using HandBrake.ApplicationServices.Services.Interfaces;
/// <summary>
/// A Set of Static Utilites
@@ -33,12 +31,7 @@ namespace HandBrake.ApplicationServices.Utilities /// <summary>
/// The Default Log Directory
/// </summary>
- private static readonly string LogDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
- "\\HandBrake\\logs";
- /// <summary>
- /// The Instance ID
- /// </summary>
- private static int instanceId = 0;
+ private static readonly string LogDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
#endregion
@@ -47,23 +40,11 @@ namespace HandBrake.ApplicationServices.Utilities /// <summary>
/// Gets the number of HandBrake instances running.
/// </summary>
- public static string GetInstanceCount
- {
- get
- {
- return instanceId == 0 ? string.Empty : instanceId.ToString(CultureInfo.InvariantCulture);
- }
- }
-
- /// <summary>
- /// Gets a value indicating whether HandBrake is running in multi instance mode
- /// </summary>
- /// <returns>True if the UI has another instance running</returns>
- public static bool IsMultiInstance
+ public static int ProcessId
{
get
{
- return Process.GetProcessesByName("HandBrake").Length > 1 ? true : false;
+ return Process.GetCurrentProcess().Id;
}
}
@@ -85,19 +66,16 @@ namespace HandBrake.ApplicationServices.Utilities var info = new DirectoryInfo(LogDir);
FileInfo[] logFiles = info.GetFiles("*.txt");
- // Delete Them
+ // Delete old and excessivly large files (> ~50MB).
foreach (FileInfo file in logFiles)
{
if (file.LastWriteTime < DateTime.Now.AddDays(-daysToKeep))
{
- if (!file.Name.Contains("last_scan_log.txt") && !file.Name.Contains("last_encode_log.txt"))
- {
- File.Delete(file.FullName);
- }
- else if (file.Length > 104857600)
- {
- File.Delete(file.FullName);
- }
+ File.Delete(file.FullName);
+ }
+ else if (file.Length > 50000000)
+ {
+ File.Delete(file.FullName);
}
}
}
@@ -118,7 +96,7 @@ namespace HandBrake.ApplicationServices.Utilities public static StringBuilder CreateCliLogHeader(string version, int build)
{
var logHeader = new StringBuilder();
-
+
logHeader.AppendLine(String.Format("HandBrake {0} {1}", version, build));
logHeader.AppendLine(String.Format("OS: {0}", Environment.OSVersion));
logHeader.AppendLine(String.Format("CPU: {0}", SystemInfo.GetCpuCount));
@@ -161,8 +139,8 @@ namespace HandBrake.ApplicationServices.Utilities drives.Add(
new DriveInformation
{
- Id = id,
- VolumeLabel = curDrive.VolumeLabel,
+ Id = id,
+ VolumeLabel = curDrive.VolumeLabel,
RootDirectory = curDrive.RootDirectory.ToString()
});
id++;
@@ -188,12 +166,20 @@ namespace HandBrake.ApplicationServices.Utilities }
/// <summary>
- /// Set the Instance ID
+ /// The find hand brake instance ids.
/// </summary>
- public static void SetInstanceId()
+ /// <param name="id">
+ /// The id.
+ /// </param>
+ /// <returns>
+ /// The <see cref="bool"/>. True if it's a running HandBrake instance.
+ /// </returns>
+ public static bool IsPidACurrentHandBrakeInstance(int id)
{
- instanceId = Process.GetProcessesByName("HandBrake").Length;
+ List<int> ids = Process.GetProcessesByName("HandBrake").Select(process => process.Id).ToList();
+ return ids.Contains(id);
}
+
#endregion
}
}
\ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Helpers/QueueRecoveryHelper.cs b/win/CS/HandBrakeWPF/Helpers/QueueRecoveryHelper.cs index f37fae656..b434fc0b9 100644 --- a/win/CS/HandBrakeWPF/Helpers/QueueRecoveryHelper.cs +++ b/win/CS/HandBrakeWPF/Helpers/QueueRecoveryHelper.cs @@ -11,8 +11,11 @@ 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;
@@ -68,12 +71,19 @@ namespace HandBrakeWPF.Helpers }
// Cleanup old/unused queue files for now.
- if (!GeneralUtilities.IsMultiInstance)
+ foreach (string file in removeFiles)
{
- foreach (string file in removeFiles)
+ Match m = Regex.Match(file, @"([0-9]+).xml");
+ if (m.Success)
{
- File.Delete(file);
+ int processId = int.Parse(m.Groups[1].ToString());
+ if (GeneralUtilities.IsPidACurrentHandBrakeInstance(processId))
+ {
+ continue;
+ }
}
+
+ File.Delete(file);
}
return queueFiles;
@@ -116,16 +126,41 @@ namespace HandBrakeWPF.Helpers foreach (string file in queueFiles)
{
encodeQueue.RestoreQueue(appDataPath + file); // Start Recovery
+ if (!file.Contains(GeneralUtilities.ProcessId.ToString(CultureInfo.InvariantCulture)))
+ {
+ try
+ {
+ // Once we load it in, remove it as we no longer need it.
+ File.Delete(file);
+ }
+ catch (Exception)
+ {
+ // Keep quite, nothing much we can do if there are problems.
+ // We will continue processing files.
+ }
+ }
}
}
else
{
- if (GeneralUtilities.IsMultiInstance) return; // Don't tamper with the files if we are multi instance
-
foreach (string file in queueFiles)
{
if (File.Exists(Path.Combine(appDataPath, file)))
+ {
+ // 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;
+ }
+ }
+
+ // Delete it if it doesn't
File.Delete(Path.Combine(appDataPath, file));
+ }
}
}
}
diff --git a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs index 70f62c435..4b90e8feb 100644 --- a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs @@ -15,6 +15,7 @@ namespace HandBrakeWPF.ViewModels using System.Globalization;
using System.IO;
using System.Linq;
+ using System.Threading;
using System.Windows;
using System.Windows.Media.Imaging;
@@ -196,8 +197,6 @@ namespace HandBrakeWPF.ViewModels public MainViewModel(IUserSettingService userSettingService, IScanServiceWrapper scanService, IEncodeServiceWrapper encodeService, IPresetService presetService,
IErrorService errorService, IShellViewModel shellViewModel, IUpdateService updateService, IDriveDetectService driveDetectService, INotificationService notificationService)
{
- GeneralUtilities.SetInstanceId();
-
this.scanService = scanService;
this.encodeService = encodeService;
this.presetService = presetService;
@@ -207,7 +206,6 @@ namespace HandBrakeWPF.ViewModels this.driveDetectService = driveDetectService;
this.userSettingService = userSettingService;
this.queueProcessor = IoC.Get<IQueueProcessor>();
- this.queueProcessor.ResetInstanceId();
// Setup Properties
this.WindowTitle = "HandBrake";
@@ -825,6 +823,13 @@ namespace HandBrakeWPF.ViewModels this.SourceMenu = this.GenerateSourceMenu();
this.driveDetectService.StartDetection(this.DriveTrayChanged);
+
+ // Log Cleaning
+ if (userSettingService.GetUserSetting<bool>(UserSettingConstants.ClearOldLogs))
+ {
+ Thread clearLog = new Thread(() => GeneralUtilities.ClearLogFiles(30));
+ clearLog.Start();
+ }
}
/// <summary>
diff --git a/win/CS/HandBrakeWPF/Views/OptionsView.xaml b/win/CS/HandBrakeWPF/Views/OptionsView.xaml index 88f2fae61..3e883a7ff 100644 --- a/win/CS/HandBrakeWPF/Views/OptionsView.xaml +++ b/win/CS/HandBrakeWPF/Views/OptionsView.xaml @@ -24,11 +24,6 @@ <Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="8,4" />
- <!--<Style.Triggers>
- <DataTrigger Binding="{Binding IsBuildIn}" Value="True">
- <Setter Property="Foreground" Value="DarkBlue" />
- </DataTrigger>
- </Style.Triggers>-->
</Style>
<Options:OptionsTabConverter x:Key="tabConverter" />
|