Refactored MainHandler

master
Inga 🏳‍🌈 4 years ago
parent 411058462e
commit d77c78f3ca
  1. 86
      RadeonResetBugFixService/MainHandler.cs
  2. 25
      RadeonResetBugFixService/Program.cs
  3. 51
      RadeonResetBugFixService/RadeonResetBugFixService.cs
  4. 31
      RadeonResetBugFixService/RadeonResetBugFixService.csproj
  5. 2
      RadeonResetBugFixService/Tasks/BasicTasks/AbstractDevicesTask.cs
  6. 2
      RadeonResetBugFixService/Tasks/BasicTasks/AbstractServiceTask.cs
  7. 2
      RadeonResetBugFixService/Tasks/BasicTasks/DisableAmdVideoTask.cs
  8. 2
      RadeonResetBugFixService/Tasks/BasicTasks/DisableBasicDisplayStartupTask.cs
  9. 2
      RadeonResetBugFixService/Tasks/BasicTasks/DisableVirtualVideoTask.cs
  10. 2
      RadeonResetBugFixService/Tasks/BasicTasks/EnableAmdVideoTask.cs
  11. 2
      RadeonResetBugFixService/Tasks/BasicTasks/EnableBasicDisplayStartupTask.cs
  12. 2
      RadeonResetBugFixService/Tasks/BasicTasks/EnableVirtualVideoTask.cs
  13. 2
      RadeonResetBugFixService/Tasks/BasicTasks/FixMonitorTask.cs
  14. 2
      RadeonResetBugFixService/Tasks/BasicTasks/LastResortDevicesRestoreTask.cs
  15. 2
      RadeonResetBugFixService/Tasks/BasicTasks/ListDevicesTask.cs
  16. 2
      RadeonResetBugFixService/Tasks/BasicTasks/SleepTask.cs
  17. 2
      RadeonResetBugFixService/Tasks/BasicTasks/StopAudioServiceTask.cs
  18. 13
      RadeonResetBugFixService/Tasks/ComplexTasks/AbstractSequentialTask.cs
  19. 16
      RadeonResetBugFixService/Tasks/ComplexTasks/DiagnoseTask.cs
  20. 25
      RadeonResetBugFixService/Tasks/ComplexTasks/ShutdownTask.cs
  21. 25
      RadeonResetBugFixService/Tasks/ComplexTasks/StartupTask.cs
  22. 2
      RadeonResetBugFixService/TasksProcessor.cs

@ -24,97 +24,17 @@
$"radeonfix_{date:yyyyMMdd}_{date:HHmmss}.log"); $"radeonfix_{date:yyyyMMdd}_{date:HHmmss}.log");
} }
public void HandleLog(string message) public void HandleEntryPoint(string name, Action<ILogger> handle)
{
using (ILogger fileLogger = new FileLogger(this.LogFilename))
{
fileLogger.Log(message);
}
}
public void HandleStartup(string reason)
{
using (var fileLogger = new FileLogger(this.LogFilename))
{
using (ILogger logger = new TaskLoggerWrapper(fileLogger, "Startup"))
{
logger.Log($"Reason: {reason}");
try
{
lock (this.Mutex)
{
TasksProcessor.ProcessTasks(
logger,
new ITask[]
{
new EnableBasicDisplayStartupTask(),
new SleepTask(TimeSpan.FromSeconds(40)),
new EnableAmdVideoTask(this.StartupDevicesStatus),
new DisableVirtualVideoTask(this.StartupDevicesStatus),
new SleepTask(TimeSpan.FromSeconds(20)),
new FixMonitorTask(),
new DisableVirtualVideoTask(this.StartupDevicesStatus),
new FixMonitorTask(),
});
}
}
catch (Exception e)
{
logger.LogError(e.ToString());
}
}
}
}
public void HandleShutdown(string reason)
{
using (var fileLogger = new FileLogger(this.LogFilename))
{
using (ILogger logger = new TaskLoggerWrapper(fileLogger, "Shutdown"))
{
logger.Log($"Reason: {reason}");
try
{
lock (this.Mutex)
{
TasksProcessor.ProcessTasks(
logger,
new ITask[]
{
new StopAudioServiceTask(),
new EnableVirtualVideoTask(this.ShutdownDevicesStatus),
new DisableAmdVideoTask(this.ShutdownDevicesStatus),
new LastResortDevicesRestoreTask(this.StartupDevicesStatus),
new LastResortDevicesRestoreTask(this.StartupDevicesStatus), // just in case
new DisableBasicDisplayStartupTask(this.StartupDevicesStatus),
});
}
}
catch (Exception e)
{
logger.LogError(e.ToString());
}
}
}
}
public void HandleDiagnose(string reason)
{ {
using (var fileLogger = new FileLogger(this.LogFilename)) using (var fileLogger = new FileLogger(this.LogFilename))
{ {
using (ILogger logger = new TaskLoggerWrapper(fileLogger, "Diagnose")) using (ILogger logger = new TaskLoggerWrapper(fileLogger, name))
{ {
logger.Log($"Reason: {reason}");
try try
{ {
lock (this.Mutex) lock (this.Mutex)
{ {
TasksProcessor.ProcessTasks( handle(logger);
logger,
new ITask[]
{
new ListDevicesTask(),
});
} }
} }
catch (Exception e) catch (Exception e)

@ -2,6 +2,7 @@
{ {
using System; using System;
using System.ServiceProcess; using System.ServiceProcess;
using Tasks.ComplexTasks;
using ThirdParty.ServiceHelpers; using ThirdParty.ServiceHelpers;
public static class Program public static class Program
@ -133,19 +134,19 @@
DoInstall(); DoInstall();
} }
private static void DoStartup() private static void DoStartup() => new MainHandler().HandleEntryPoint(
{ "Program.DoStartup",
new MainHandler().HandleStartup("Program.DoStartup"); (logger) => TasksProcessor.ProcessTask(logger, new StartupTask())
} );
private static void DoShutdown() private static void DoShutdown() => new MainHandler().HandleEntryPoint(
{ "Program.DoShutdown",
new MainHandler().HandleShutdown("Program.DoShutdown"); (logger) => TasksProcessor.ProcessTask(logger, new ShutdownTask())
} );
private static void DoDiagnose() private static void DoDiagnose() => new MainHandler().HandleEntryPoint(
{ "Program.DoDiagnose",
new MainHandler().HandleDiagnose("Program.DoDiagnose"); (logger) => TasksProcessor.ProcessTask(logger, new DiagnoseTask())
} );
} }
} }

@ -4,6 +4,7 @@
using System.Reflection; using System.Reflection;
using System.ServiceProcess; using System.ServiceProcess;
using Microsoft.Win32; using Microsoft.Win32;
using Tasks.ComplexTasks;
public partial class RadeonResetBugFixService : ServiceBase public partial class RadeonResetBugFixService : ServiceBase
{ {
@ -35,38 +36,22 @@
deferredStopMethodInfo.Invoke(this, null); deferredStopMethodInfo.Invoke(this, null);
} }
private void Process(string reason, Action<string> handle)
{
this.Handler.HandleLog($"{reason} initiated");
try
{
handle(reason);
this.Handler.HandleLog($"{reason} successfully finished");
}
catch (Exception e)
{
this.Handler.HandleLog($"{reason} error: {e}");
}
}
protected override void OnShutdown() protected override void OnShutdown()
{ {
this.Process( this.Handler.HandleEntryPoint(
"ServiceBase.OnShutdown", "ServiceBase.OnShutdown",
(string reason) => (logger) => this.CallStop()
{ );
this.CallStop();
});
} }
protected override void OnStart(string[] args) protected override void OnStart(string[] args)
{ {
this.Process( this.Handler.HandleEntryPoint(
"ServiceBase.OnStart", "ServiceBase.OnStart",
(string reason) => (logger) =>
{ {
this.RequestAdditionalTime((int)Constants.ServiceTimeout.TotalMilliseconds); this.RequestAdditionalTime((int)Constants.ServiceTimeout.TotalMilliseconds);
this.Handler.HandleStartup(reason); TasksProcessor.ProcessTask(logger, new StartupTask());
this.EnablePreshutdown(); this.EnablePreshutdown();
SystemEvents.SessionEnding += this.OnSessionEnding; SystemEvents.SessionEnding += this.OnSessionEnding;
}); });
@ -74,12 +59,12 @@
protected override void OnStop() protected override void OnStop()
{ {
this.Process( this.Handler.HandleEntryPoint(
"ServiceBase.OnStop", "ServiceBase.OnStop",
(string reason) => (logger) =>
{ {
this.RequestAdditionalTime((int)Constants.ServiceTimeout.TotalMilliseconds); this.RequestAdditionalTime((int)Constants.ServiceTimeout.TotalMilliseconds);
this.Handler.HandleShutdown(reason); TasksProcessor.ProcessTask(logger, new ShutdownTask());
SystemEvents.SessionEnding -= this.OnSessionEnding; SystemEvents.SessionEnding -= this.OnSessionEnding;
}); });
} }
@ -88,33 +73,33 @@
{ {
const int SERVICE_CONTROL_PRESHUTDOWN = 0xf; const int SERVICE_CONTROL_PRESHUTDOWN = 0xf;
this.Process( this.Handler.HandleEntryPoint(
"ServiceBase.OnCustomCommand", "ServiceBase.OnCustomCommand",
(string reason) => (logger) =>
{ {
if (command == SERVICE_CONTROL_PRESHUTDOWN) if (command == SERVICE_CONTROL_PRESHUTDOWN)
{ {
this.Handler.HandleLog($"Custom command: preshutdown"); logger.Log("Custom command: preshutdown");
this.CallStop(); this.CallStop();
} }
else else
{ {
this.Handler.HandleLog($"Unknown custom command: {command}"); logger.Log("Unknown custom command: {command}");
} }
}); });
} }
private void OnSessionEnding(object sender, SessionEndingEventArgs args) private void OnSessionEnding(object sender, SessionEndingEventArgs args)
{ {
this.Process( this.Handler.HandleEntryPoint(
"SystemEvents.OnSessionEnding", "SystemEvents.OnSessionEnding",
(string reason) => (logger) =>
{ {
this.Handler.HandleLog($"Session end reason: ${args.Reason}"); logger.Log($"Session end reason: ${args.Reason}");
if (args.Reason == SessionEndReasons.SystemShutdown) if (args.Reason == SessionEndReasons.SystemShutdown)
{ {
this.Handler.HandleShutdown(reason); TasksProcessor.ProcessTask(logger, new ShutdownTask());
} }
}); });
} }

@ -85,23 +85,27 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Logging\TaskLoggerWrapper.cs" /> <Compile Include="Logging\TaskLoggerWrapper.cs" />
<Compile Include="RegistryHelper.cs" /> <Compile Include="RegistryHelper.cs" />
<Compile Include="Tasks\DisableBasicDisplayStartupTask.cs" /> <Compile Include="Tasks\BasicTasks\DisableBasicDisplayStartupTask.cs" />
<Compile Include="Tasks\EnableBasicDisplayStartupTask.cs" /> <Compile Include="Tasks\BasicTasks\EnableBasicDisplayStartupTask.cs" />
<Compile Include="Tasks\FixMonitorTask.cs" /> <Compile Include="Tasks\BasicTasks\FixMonitorTask.cs" />
<Compile Include="Tasks\LastResortDevicesRestoreTask.cs" /> <Compile Include="Tasks\BasicTasks\LastResortDevicesRestoreTask.cs" />
<Compile Include="Tasks\ListDevicesTask.cs" /> <Compile Include="Tasks\BasicTasks\ListDevicesTask.cs" />
<Compile Include="Tasks\SleepTask.cs" /> <Compile Include="Tasks\BasicTasks\SleepTask.cs" />
<Compile Include="Tasks\ComplexTasks\AbstractSequentialTask.cs" />
<Compile Include="Tasks\ComplexTasks\DiagnoseTask.cs" />
<Compile Include="Tasks\ComplexTasks\ShutdownTask.cs" />
<Compile Include="Tasks\ComplexTasks\StartupTask.cs" />
<Compile Include="ThirdParty\MonitorChanger.cs" /> <Compile Include="ThirdParty\MonitorChanger.cs" />
<Compile Include="ThirdParty\ServiceHelpers.cs" /> <Compile Include="ThirdParty\ServiceHelpers.cs" />
<Compile Include="TasksProcessor.cs" /> <Compile Include="TasksProcessor.cs" />
<Compile Include="Tasks\AbstractDevicesTask.cs" /> <Compile Include="Tasks\BasicTasks\AbstractDevicesTask.cs" />
<Compile Include="Tasks\AbstractServiceTask.cs" /> <Compile Include="Tasks\BasicTasks\AbstractServiceTask.cs" />
<Compile Include="Tasks\DisableAmdVideoTask.cs" /> <Compile Include="Tasks\BasicTasks\DisableAmdVideoTask.cs" />
<Compile Include="Tasks\DisableVirtualVideoTask.cs" /> <Compile Include="Tasks\BasicTasks\DisableVirtualVideoTask.cs" />
<Compile Include="Tasks\EnableAmdVideoTask.cs" /> <Compile Include="Tasks\BasicTasks\EnableAmdVideoTask.cs" />
<Compile Include="Tasks\EnableVirtualVideoTask.cs" /> <Compile Include="Tasks\BasicTasks\EnableVirtualVideoTask.cs" />
<Compile Include="Tasks\ITask.cs" /> <Compile Include="Tasks\ITask.cs" />
<Compile Include="Tasks\StopAudioServiceTask.cs" /> <Compile Include="Tasks\BasicTasks\StopAudioServiceTask.cs" />
<Compile Include="ThirdParty\DisableDevice.cs" /> <Compile Include="ThirdParty\DisableDevice.cs" />
<Compile Include="Devices\DeviceHelper.cs" /> <Compile Include="Devices\DeviceHelper.cs" />
<Compile Include="ThirdParty\ServicePreshutdownHelpers.cs" /> <Compile Include="ThirdParty\ServicePreshutdownHelpers.cs" />
@ -131,6 +135,7 @@
<Analyzer Include="..\packages\Microsoft.NetFramework.Analyzers.2.9.8\analyzers\dotnet\cs\Microsoft.NetFramework.Analyzers.dll" /> <Analyzer Include="..\packages\Microsoft.NetFramework.Analyzers.2.9.8\analyzers\dotnet\cs\Microsoft.NetFramework.Analyzers.dll" />
<Analyzer Include="..\packages\Microsoft.NetFramework.Analyzers.2.9.8\analyzers\dotnet\cs\Microsoft.NetFramework.CSharp.Analyzers.dll" /> <Analyzer Include="..\packages\Microsoft.NetFramework.Analyzers.2.9.8\analyzers\dotnet\cs\Microsoft.NetFramework.CSharp.Analyzers.dll" />
</ItemGroup> </ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup> <PropertyGroup>

@ -1,4 +1,4 @@
namespace RadeonResetBugFixService.Tasks namespace RadeonResetBugFixService.Tasks.BasicTasks
{ {
using System; using System;
using System.Linq; using System.Linq;

@ -1,4 +1,4 @@
namespace RadeonResetBugFixService.Tasks namespace RadeonResetBugFixService.Tasks.BasicTasks
{ {
using System; using System;
using System.ServiceProcess; using System.ServiceProcess;

@ -1,4 +1,4 @@
namespace RadeonResetBugFixService.Tasks namespace RadeonResetBugFixService.Tasks.BasicTasks
{ {
using Contracts; using Contracts;
using Devices; using Devices;

@ -1,4 +1,4 @@
namespace RadeonResetBugFixService.Tasks namespace RadeonResetBugFixService.Tasks.BasicTasks
{ {
using System; using System;
using Contracts; using Contracts;

@ -1,4 +1,4 @@
namespace RadeonResetBugFixService.Tasks namespace RadeonResetBugFixService.Tasks.BasicTasks
{ {
using Contracts; using Contracts;
using Devices; using Devices;

@ -1,4 +1,4 @@
namespace RadeonResetBugFixService.Tasks namespace RadeonResetBugFixService.Tasks.BasicTasks
{ {
using Contracts; using Contracts;
using Devices; using Devices;

@ -1,4 +1,4 @@
namespace RadeonResetBugFixService.Tasks namespace RadeonResetBugFixService.Tasks.BasicTasks
{ {
using Contracts; using Contracts;

@ -1,4 +1,4 @@
namespace RadeonResetBugFixService.Tasks namespace RadeonResetBugFixService.Tasks.BasicTasks
{ {
using Contracts; using Contracts;
using Devices; using Devices;

@ -1,4 +1,4 @@
namespace RadeonResetBugFixService.Tasks namespace RadeonResetBugFixService.Tasks.BasicTasks
{ {
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;

@ -1,4 +1,4 @@
namespace RadeonResetBugFixService.Tasks namespace RadeonResetBugFixService.Tasks.BasicTasks
{ {
using System; using System;
using Contracts; using Contracts;

@ -1,4 +1,4 @@
namespace RadeonResetBugFixService.Tasks namespace RadeonResetBugFixService.Tasks.BasicTasks
{ {
using System.Linq; using System.Linq;
using Contracts; using Contracts;

@ -1,4 +1,4 @@
namespace RadeonResetBugFixService.Tasks namespace RadeonResetBugFixService.Tasks.BasicTasks
{ {
using System; using System;
using System.Threading; using System.Threading;

@ -1,4 +1,4 @@
namespace RadeonResetBugFixService.Tasks namespace RadeonResetBugFixService.Tasks.BasicTasks
{ {
using System; using System;
using System.ServiceProcess; using System.ServiceProcess;

@ -0,0 +1,13 @@
namespace RadeonResetBugFixService.Tasks.ComplexTasks
{
using Contracts;
abstract class AbstractSequentialTask : ITask
{
public abstract string TaskName { get; }
protected abstract ITask[] Subtasks { get; }
void ITask.Run(ILogger logger) => TasksProcessor.ProcessTasks(logger, this.Subtasks);
}
}

@ -0,0 +1,16 @@
namespace RadeonResetBugFixService.Tasks.ComplexTasks
{
using BasicTasks;
using Contracts;
using System;
class DiagnoseTask : AbstractSequentialTask
{
public override string TaskName => "Diagnose";
protected override ITask[] Subtasks => new ITask[]
{
new ListDevicesTask(),
};
}
}

@ -0,0 +1,25 @@
namespace RadeonResetBugFixService.Tasks.ComplexTasks
{
using BasicTasks;
using Contracts;
using System;
class ShutdownTask : AbstractSequentialTask
{
private DevicesStatus ShutdownDevicesStatus { get; } = new DevicesStatus();
public override string TaskName => "Shutdown";
protected override ITask[] Subtasks => new ITask[]
{
new EnableBasicDisplayStartupTask(),
new SleepTask(TimeSpan.FromSeconds(40)),
new EnableAmdVideoTask(this.ShutdownDevicesStatus),
new DisableVirtualVideoTask(this.ShutdownDevicesStatus),
new SleepTask(TimeSpan.FromSeconds(20)),
new FixMonitorTask(),
new DisableVirtualVideoTask(this.ShutdownDevicesStatus),
new FixMonitorTask()
};
}
}

@ -0,0 +1,25 @@
namespace RadeonResetBugFixService.Tasks.ComplexTasks
{
using BasicTasks;
using Contracts;
using System;
class StartupTask : AbstractSequentialTask
{
private DevicesStatus StartupDevicesStatus { get; } = new DevicesStatus();
public override string TaskName => "Startup";
protected override ITask[] Subtasks => new ITask[]
{
new EnableBasicDisplayStartupTask(),
new SleepTask(TimeSpan.FromSeconds(40)),
new EnableAmdVideoTask(this.StartupDevicesStatus),
new DisableVirtualVideoTask(this.StartupDevicesStatus),
new SleepTask(TimeSpan.FromSeconds(20)),
new FixMonitorTask(),
new DisableVirtualVideoTask(this.StartupDevicesStatus),
new FixMonitorTask()
};
}
}

@ -8,7 +8,7 @@
static class TasksProcessor static class TasksProcessor
{ {
private static void ProcessTask(ILogger logger, ITask task) public static void ProcessTask(ILogger logger, ITask task)
{ {
using (ILogger taskLogger = new TaskLoggerWrapper(logger, task.TaskName)) using (ILogger taskLogger = new TaskLoggerWrapper(logger, task.TaskName))
{ {

Loading…
Cancel
Save