Skip to content

Commit

Permalink
Merge pull request #178 from ptr727/keyboard-quit
Browse files Browse the repository at this point in the history
Add Ctrl+Q and Ctrl+Z to signal terminate
  • Loading branch information
ptr727 authored Jun 21, 2023
2 parents 0aac0bc + 6ab8c21 commit a1a2d50
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 58 deletions.
22 changes: 21 additions & 1 deletion PlexCleaner/ConfigFileJsonSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,27 @@ public void SetDefaults()

public bool VerifyValues()
{
return ToolsOptions.VerifyValues() && ConvertOptions.VerifyValues() && ProcessOptions.VerifyValues() && MonitorOptions.VerifyValues() && VerifyOptions.VerifyValues();
if (!ToolsOptions.VerifyValues() ||
!ConvertOptions.VerifyValues() ||
!ProcessOptions.VerifyValues() ||
!MonitorOptions.VerifyValues() ||
!VerifyOptions.VerifyValues())
{
return false;
}

// Default to English if language not set
if (string.IsNullOrEmpty(ProcessOptions.DefaultLanguage))
{
ProcessOptions.DefaultLanguage = Language.English;
}

// Always keep no linguistic content (zxx), undefined (und), and the default language
ProcessOptions.KeepLanguages.Add(Language.None);
ProcessOptions.KeepLanguages.Add(Language.Undefined);
ProcessOptions.KeepLanguages.Add(ProcessOptions.DefaultLanguage);

return true;
}

public static void WriteDefaultsToFile(string path)
Expand Down
29 changes: 1 addition & 28 deletions PlexCleaner/Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,8 @@ namespace PlexCleaner;
// Filename, State
using ProcessTuple = ValueTuple<string, SidecarFile.StatesType>;

internal class Process
internal static class Process
{
// Processing tasks
public enum Tasks
{
ClearTags,
RemoveAttachments,
IdetFilter,
FindClosedCaptions,
RepairMedia,
VerifyMetadata,
VerifyMedia,
RepairMetadata
}

public Process()
{
// Default to English if language not set
if (string.IsNullOrEmpty(Program.Config.ProcessOptions.DefaultLanguage))
{
Program.Config.ProcessOptions.DefaultLanguage = Language.English;
}

// Always keep no linguistic content (zxx), undefined (und), and the default language
Program.Config.ProcessOptions.KeepLanguages.Add(Language.None);
Program.Config.ProcessOptions.KeepLanguages.Add(Language.Undefined);
Program.Config.ProcessOptions.KeepLanguages.Add(Program.Config.ProcessOptions.DefaultLanguage);
}

private static bool ProcessFile(string fileName, out bool modified, out SidecarFile.StatesType state, out string processName)
{
// Init
Expand Down
86 changes: 57 additions & 29 deletions PlexCleaner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using InsaneGenius.Utilities;
using Serilog;
using Serilog.Debugging;
Expand All @@ -30,6 +31,11 @@ private static int Main()
// Wait for debugger to attach
WaitForDebugger();

// Register cancel and keyboard handlers
Console.CancelKeyPress += CancelEventHandler;
var consoleKeyTask = Task.Run(KeyPressHandler);
Console.WriteLine("Press Ctrl+C or Ctrl+Z or Ctrl+Q to exit.");

// Create default logger
CreateLogger(null);

Expand All @@ -43,6 +49,10 @@ private static int Main()
// Create the commandline and execute commands
var exitCode = CommandLineOptions.Invoke();

// Cancel background operations
Cancel();
consoleKeyTask.Wait();

// Stop the timer
keepAwakeTimer.Stop();
keepAwakeTimer.Dispose();
Expand All @@ -56,15 +66,62 @@ private static int Main()
return exitCode;
}

private static void KeyPressHandler()
{
for(;;)
{
// Wait on key available or cancelled
while (!Console.KeyAvailable)
{
if (WaitForCancel(100))
{
// Done
return;
}
}

// Read key and hide from console display
var keyInfo = Console.ReadKey(true);

// Break on Ctrl+Q or Ctrl+Z, Ctrl+C handled in cancel handler
if (keyInfo.Key is ConsoleKey.Q or ConsoleKey.Z
&& keyInfo.Modifiers == ConsoleModifiers.Control)
{
Log.Logger.Warning("Operation interrupted : {Modifiers}+{Key}", keyInfo.Modifiers, keyInfo.Key);

// Signal the cancel event
Cancel();

// Done
return;
}
}
}

private static void CancelEventHandler(object sender, ConsoleCancelEventArgs eventArgs)
{
Log.Logger.Warning("Operation interrupted : {SpecialKey}", eventArgs.SpecialKey);

// Keep running and do graceful exit
eventArgs.Cancel = true;

// Signal the cancel event
Cancel();
}


private static void WaitForDebugger()
{
// Do not use any dependencies as this code gets called very early in launch

// Use the raw commandline and look for --debug
if (Environment.CommandLine.Contains("--debug"))
{
// Wait for a debugger to be attached
Console.WriteLine("Waiting for debugger to attach...");
while (!System.Diagnostics.Debugger.IsAttached)
{
// Wait a bit and try again
Thread.Sleep(100);
}
Console.WriteLine("Debugger attached.");
Expand Down Expand Up @@ -331,35 +388,6 @@ internal static int GetVersionInfoCommand(CommandLineOptions options)
return MakeExitCode(Tools.VerifyTools());
}

// Add a reference to this class in the event handler arguments
private static void CancelHandlerEx(object s, ConsoleCancelEventArgs e)
{
CancelHandler(e);
}

private static void CancelHandler(ConsoleCancelEventArgs e)
{
Log.Logger.Warning("Cancel key pressed");

// Keep running and do graceful exit
e.Cancel = true;

// Signal the cancel event
Cancel();
}

private Program()
{
// Register cancel handler
Console.CancelKeyPress += CancelHandlerEx;
}

~Program()
{
// Unregister cancel handler
Console.CancelKeyPress -= CancelHandlerEx;
}

private static Program CreateFileList(CommandLineOptions options)
{
// Create program and enumerate files
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Docker images are published on [Docker Hub](https://hub.docker.com/r/ptr727/plex

## Release Notes

- Version 3.2:
- Added `Ctrl-Q` and `Ctrl-Z` as additional break commands, `Ctrl+C` may terminate the shell command vs. cleanly exiting the process.
- Version 3.1:
- Added `--preprocess` option to the `monitor` command, that will pre-process all monitored folders.
- Version 3.0:
Expand Down

0 comments on commit a1a2d50

Please sign in to comment.