Skip to content

Commit

Permalink
feat: improve behavior on crash (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger authored Apr 19, 2022
1 parent ff4d372 commit f1ba212
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 51 deletions.
49 changes: 17 additions & 32 deletions GlazeWM.Bootstrapper/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using GlazeWM.Infrastructure.WindowsApi;
using GlazeWM.Infrastructure.WindowsApi.Events;
using System;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Windows.Forms;
Expand Down Expand Up @@ -58,44 +57,30 @@ SystemEventService systemEventService

public void Run()
{
try
{
// Set the process-default DPI awareness.
SetProcessDpiAwarenessContext(DpiAwarenessContext.Context_PerMonitorAwareV2);
// Set the process-default DPI awareness.
SetProcessDpiAwarenessContext(DpiAwarenessContext.Context_PerMonitorAwareV2);

// Launch bar WPF application. Spawns bar window when monitors are added, so the service needs
// to be initialized before populating initial state.
_barService.StartApp();
// Launch bar WPF application. Spawns bar window when monitors are added, so the service needs
// to be initialized before populating initial state.
_barService.StartApp();

// Populate initial monitors, windows, workspaces and user config.
PopulateInitialState();
// Populate initial monitors, windows, workspaces and user config.
PopulateInitialState();

// Listen on registered keybindings.
_keybindingService.Start();
// Listen on registered keybindings.
_keybindingService.Start();

// Listen for window events (eg. close, focus).
_windowEventService.Start();
// Listen for window events (eg. close, focus).
_windowEventService.Start();

// Listen for system-related events (eg. changes to display settings).
_systemEventService.Start();
// Listen for system-related events (eg. changes to display settings).
_systemEventService.Start();

// Add application to system tray.
_systemTrayService.AddToSystemTray();
// Add application to system tray.
_systemTrayService.AddToSystemTray();

_bus.Events.Where(@event => @event is ApplicationExitingEvent)
.Subscribe((@event) => OnApplicationExit());
}
catch (Exception error)
{
// Alert the user of the error.
// TODO: This throws duplicate errors if a command errors and it was invoked by another
// handler.
if (error is FatalUserException)
MessageBox.Show(error.Message);

File.AppendAllText("./errors.log", $"\n\n{error.Message + error.StackTrace}");
throw error;
}
_bus.Events.Where(@event => @event is ApplicationExitingEvent)
.Subscribe((@event) => OnApplicationExit());
}

/// <summary>
Expand Down
79 changes: 60 additions & 19 deletions GlazeWM.Infrastructure/Bussing/Bus.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using Microsoft.Extensions.DependencyInjection;
using GlazeWM.Infrastructure.WindowsApi.Events;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reactive.Subjects;
using System.Windows.Forms;

namespace GlazeWM.Infrastructure.Bussing
{
Expand All @@ -19,17 +22,25 @@ public sealed class Bus
/// </summary>
public CommandResponse Invoke<T>(T command) where T : Command
{
Debug.WriteLine($"Command {command.Name} invoked.");
try
{
Debug.WriteLine($"Command {command.Name} invoked.");

// Create a `Type` object representing the constructed `ICommandHandler` generic.
var handlerType = typeof(ICommandHandler<>).MakeGenericType(command.GetType());
// Create a `Type` object representing the constructed `ICommandHandler` generic.
var handlerType = typeof(ICommandHandler<>).MakeGenericType(command.GetType());

var handlerInstance = ServiceLocator.Provider.GetRequiredService(handlerType)
as ICommandHandler<T>;
var handlerInstance = ServiceLocator.Provider.GetRequiredService(handlerType)
as ICommandHandler<T>;

lock (_lockObj)
lock (_lockObj)
{
return handlerInstance.Handle(command);
}
}
catch (Exception error)
{
return handlerInstance.Handle(command);
HandleError(error);
throw error;
}
}

Expand All @@ -38,24 +49,54 @@ public CommandResponse Invoke<T>(T command) where T : Command
/// </summary>
public void RaiseEvent<T>(T @event) where T : Event
{
Debug.WriteLine($"Event {@event.Name} emitted.");
try
{
Debug.WriteLine($"Event {@event.Name} emitted.");

// Create a `Type` object representing the constructed `IEventHandler` generic.
var handlerType = typeof(IEventHandler<>).MakeGenericType(@event.GetType());
// Create a `Type` object representing the constructed `IEventHandler` generic.
var handlerType = typeof(IEventHandler<>).MakeGenericType(@event.GetType());

var handlerInstances = ServiceLocator.Provider.GetServices(handlerType)
as IEnumerable<IEventHandler<T>>;
var handlerInstances = ServiceLocator.Provider.GetServices(handlerType)
as IEnumerable<IEventHandler<T>>;

foreach (var handler in handlerInstances)
{
lock (_lockObj)
foreach (var handler in handlerInstances)
{
handler.Handle(@event);
lock (_lockObj)
{
handler.Handle(@event);
}
}

// Emit event through subject.
Events.OnNext(@event);
}
catch (Exception error)
{
HandleError(error);
throw error;
}
}

private void HandleError(Exception error)
{
// Alert the user of the error.
if (error is FatalUserException)
MessageBox.Show(error.Message);

WriteToErrorLog(error);
RaiseEvent(new ApplicationExitingEvent());
}

// TODO: Move to dedicated logging service.
private void WriteToErrorLog(Exception error)
{
var errorLogPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"./.glaze-wm/errors.log"
);

// Emit event through subject.
Events.OnNext(@event);
Directory.CreateDirectory(Path.GetDirectoryName(errorLogPath));
File.AppendAllText(errorLogPath, $"\n\n{DateTime.Now}\n{error.Message + error.StackTrace}");
}
}
}

0 comments on commit f1ba212

Please sign in to comment.