diff --git a/Shoko.CLI/Program.cs b/Shoko.CLI/Program.cs index d345b54cd..73a58f63c 100644 --- a/Shoko.CLI/Program.cs +++ b/Shoko.CLI/Program.cs @@ -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; @@ -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 @@ -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 @@ -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; diff --git a/Shoko.Server/Import/Importer.cs b/Shoko.Server/Import/Importer.cs index 9624d093c..b9a12e76f 100755 --- a/Shoko.Server/Import/Importer.cs +++ b/Shoko.Server/Import/Importer.cs @@ -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; diff --git a/Shoko.Server/Server/Startup.cs b/Shoko.Server/Server/Startup.cs index 5bf33cd70..c2caf5ed1 100644 --- a/Shoko.Server/Server/Startup.cs +++ b/Shoko.Server/Server/Startup.cs @@ -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; diff --git a/Shoko.TrayService/App.xaml.cs b/Shoko.TrayService/App.xaml.cs index cc1d2c401..6349538af 100644 --- a/Shoko.TrayService/App.xaml.cs +++ b/Shoko.TrayService/App.xaml.cs @@ -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; @@ -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(); @@ -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 @@ -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{ @@ -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 { @@ -125,13 +110,43 @@ private void OnWebuiOpenWebUIClick(object? sender, RoutedEventArgs args) } } + private void OnTrayExit(object? sender, RoutedEventArgs args) + { + var lifetime = Utils.ServiceContainer.GetRequiredService(); + Task.Run(() => lifetime.StopApplication()); + // stupid + Task.Run(async () => await ShutdownQuartz()); + } + + private async Task ShutdownQuartz() + { + var quartz = Utils.ServiceContainer.GetServices().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) {