From 399e4bf654d52949ff809238a880e8a458255cdc Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Mon, 16 Sep 2019 13:48:12 -0400 Subject: [PATCH] SP-229 Detect unclean shell shutdown and automatically upload an error report --- src/client/spark/Daemons/ErrorReporter.cs | 9 ++++ src/client/spark/Daemons/OopsDaemon.cs | 55 +++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/client/spark/Daemons/OopsDaemon.cs diff --git a/src/client/spark/Daemons/ErrorReporter.cs b/src/client/spark/Daemons/ErrorReporter.cs index 3c77d0538..8af305dfd 100644 --- a/src/client/spark/Daemons/ErrorReporter.cs +++ b/src/client/spark/Daemons/ErrorReporter.cs @@ -49,6 +49,15 @@ namespace bts.spark ErrorMessage = message, }); } + public async Task report_silently(string contact, string description, string userdata) { + var request = append_logs(new ErrorReport { + User = _username, + ErrorMessage = description, + UserDescription = userdata, + Contact = contact + }.immutable()); + var reply = await _svc.send_report_async(request); + } public static ErrorReportImmutable append_logs(ErrorReportImmutable src) { var result = src.CloneToMutable(); diff --git a/src/client/spark/Daemons/OopsDaemon.cs b/src/client/spark/Daemons/OopsDaemon.cs new file mode 100644 index 000000000..791394414 --- /dev/null +++ b/src/client/spark/Daemons/OopsDaemon.cs @@ -0,0 +1,55 @@ +using System; +using System.ComponentModel.Composition; +using mbts.ui; +using mbts; +using Newtonsoft.Json; +using System.Threading.Tasks; + +namespace bts.spark { + [Export(typeof(IShellDaemon))] + class OopsDaemon : IShellDaemon, IDisposable { + [ImportingConstructor] + public OopsDaemon(ErrorReporter e) { + var pid = get_pid(); + if (pid.HasValue) { + handle_unclean_shutdown(e, pid.Value); + } + save_pid(); + } + + public void Dispose() { + clear_pid(); + } + + void handle_unclean_shutdown(ErrorReporter e, int oldpid) { + string extra = ""; + try { + var conf = mbts.config.Configuration.get(); + extra = JsonConvert.SerializeObject(conf, Formatting.Indented); + } + catch { } + Task.Run(() => e.report_silently( + $"Host: {Environment.MachineName}", + $"pid: {oldpid}\n{extra}", + "Automated crash report") + ); + } + + const string _subkey = @"Software\BTS"; + private void save_pid() { + using (var key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(_subkey)) { + key.SetValue("pid", System.Diagnostics.Process.GetCurrentProcess().Id, Microsoft.Win32.RegistryValueKind.DWord); + } + } + private void clear_pid() { + using (var key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(_subkey)) { + key.DeleteValue("pid"); + } + } + private int? get_pid() { + using (var key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(_subkey)) { + return key.GetValue("pid") as int?; + } + } + } +}