Skip to content

Commit

Permalink
Fix Shutdown on Windows, some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Jan 10, 2024
1 parent 4003bd3 commit aea9969
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 44 deletions.
11 changes: 3 additions & 8 deletions Shoko.CLI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#region
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using Shoko.Server.Commands;
using Shoko.Server.Filters;
using Shoko.Server.Repositories;
using Shoko.Server.Server;
using Shoko.Server.Settings;
using Shoko.Server.Utilities;
Expand All @@ -18,7 +13,7 @@ namespace Shoko.CLI;

public static class Program
{
private static ILogger _logger;
private static ILogger _logger = null!;
public static void Main()
{
try
Expand All @@ -31,7 +26,7 @@ public static void Main()
}
Utils.SetInstance();
Utils.InitLogger();
var logFactory = new LoggerFactory().AddNLog();
var logFactory = LoggerFactory.Create(o => o.AddNLog());
_logger = logFactory.CreateLogger("Main");

try
Expand Down Expand Up @@ -98,7 +93,7 @@ private static void OnCmdProcessorHasherOnQueueStateChangedEvent(QueueStateEvent
private static void OnInstanceOnPropertyChanged(object? _, PropertyChangedEventArgs e)
{
if (e.PropertyName == "StartupFailedMessage" && ServerState.Instance.StartupFailed)
Console.WriteLine("Startup failed! Error message: " + ServerState.Instance.StartupFailedMessage);
Console.WriteLine(@"Startup failed! Error message: " + ServerState.Instance.StartupFailedMessage);
}

private static void OnUtilsOnYesNoRequired(object? _, Utils.CancelReasonEventArgs e) => e.Cancel = true;
Expand Down
2 changes: 0 additions & 2 deletions Shoko.Server/Import/Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using FluentNHibernate.Utils;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using Quartz;
using QuartzJobFactory;
using Shoko.Commons.Extensions;
using Shoko.Models.Enums;
using Shoko.Models.Server;
Expand Down
2 changes: 0 additions & 2 deletions Shoko.Server/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
using Shoko.Server.Providers.TraktTV;
using Shoko.Server.Providers.TvDB;
using Shoko.Server.Scheduling;
using Shoko.Server.Scheduling.Jobs.Actions;
using Shoko.Server.Scheduling.Jobs.Shoko;
using Shoko.Server.Services.Connectivity;
using Shoko.Server.Settings;
using Shoko.Server.Utilities;
Expand Down
79 changes: 47 additions & 32 deletions Shoko.TrayService/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Hardcodet.Wpf.TaskbarNotification;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using Quartz;
using Shoko.Server;
using Shoko.Server.Server;
using Shoko.Server.Settings;
Expand All @@ -23,11 +27,9 @@ public partial class App
{
private static TaskbarIcon? _icon;
private ILogger _logger = null!;
private static App s_instance = null!;

private void OnStartup(object a, StartupEventArgs e)
{
s_instance = this;
Console.CancelKeyPress += OnConsoleOnCancelKeyPress;
InitialiseTaskbarIcon();

Expand All @@ -41,22 +43,11 @@ private void OnStartup(object a, StartupEventArgs e)
Console.WriteLine(ex.ToString());
}

Mutex mutex;
try
{
mutex = Mutex.OpenExisting(Utils.DefaultInstance + "Mutex");
Shutdown();
}
catch (Exception ex)
{
// since we didn't find a mutex with that name, create one
Debug.WriteLine("Exception thrown:" + ex.Message + " Creating a new mutex...");
mutex = new Mutex(true, Utils.DefaultInstance + "Mutex");
}
if (Mutex.TryOpenExisting(Utils.DefaultInstance + "Mutex", out _)) Shutdown();

Utils.SetInstance();
Utils.InitLogger();
var logFactory = new LoggerFactory().AddNLog();
var logFactory = LoggerFactory.Create(o => o.AddNLog());
_logger = logFactory.CreateLogger("App.xaml");

try
Expand All @@ -71,19 +62,16 @@ private void OnStartup(object a, StartupEventArgs e)
catch (Exception exception)
{
_logger.LogCritical(exception, "The server has failed to start");
s_instance.Shutdown();
Shutdown();
}
}

private void AddEventHandlers()
{
ShokoEventHandler.Instance.Shutdown += OnInstanceOnServerShutdown;
ShokoEventHandler.Instance.Shutdown += (_, _) => DispatchShutdown();
Utils.YesNoRequired += (_, args) => args.Cancel = true;
}

public static void OnInstanceOnServerShutdown(object? o, EventArgs eventArgs)
=> s_instance.Shutdown();

private void InitialiseTaskbarIcon()
{
_icon = new TaskbarIcon{
Expand All @@ -101,18 +89,15 @@ private ContextMenu CreateContextMenu()
{
var menu = new ContextMenu();
var webui = new MenuItem{Header = "Open WebUI"};
webui.Click += OnWebuiOpenWebUIClick;
webui.Click += OnTrayOpenWebUIClick;
menu.Items.Add(webui);
webui = new MenuItem{Header = "Exit"};
webui.Click += OnWebuiExit;
webui.Click += OnTrayExit;
menu.Items.Add(webui);
return menu;
}

private void OnWebuiExit(object? sender, RoutedEventArgs args)
=> Dispatcher.Invoke(Shutdown);

private void OnWebuiOpenWebUIClick(object? sender, RoutedEventArgs args)
private void OnTrayOpenWebUIClick(object? sender, RoutedEventArgs args)
{
try
{
Expand All @@ -125,13 +110,43 @@ private void OnWebuiOpenWebUIClick(object? sender, RoutedEventArgs args)
}
}

private void OnTrayExit(object? sender, RoutedEventArgs args)
{
var lifetime = Utils.ServiceContainer.GetRequiredService<IHostApplicationLifetime>();
Task.Run(() => lifetime.StopApplication());
// stupid
Task.Run(async () => await ShutdownQuartz());
}

private async Task ShutdownQuartz()
{
var quartz = Utils.ServiceContainer.GetServices<IHostedService>().FirstOrDefault(a => a is QuartzHostedService);
if (quartz == null)
{
_logger.LogError("Could not get QuartzHostedService");
return;
}

await quartz.StopAsync(default);
}

private void OnConsoleOnCancelKeyPress(object? sender, ConsoleCancelEventArgs args)
=> Dispatcher.Invoke(() =>
{
args.Cancel = true;
Shutdown();
}
);
{
args.Cancel = true;
DispatchShutdown();
}

private void DispatchShutdown()
{
try
{
Dispatcher.Invoke(Shutdown);
}
catch (TaskCanceledException)
{
// ignore
}
}

protected override void OnExit(ExitEventArgs e)
{
Expand Down

0 comments on commit aea9969

Please sign in to comment.