public static void Email(Exception ex) { __errorCount++; if (__errorCount > 10) { return; } try { ScGimpWebJobConfig config = new ScGimpWebJobConfig(); using (SmtpClient client = CreateSmtpClient(config)) { MailMessage mailMessage = new MailMessage { From = new MailAddress(config.Options.From, "Star Citizen Gimp"), IsBodyHtml = false }; mailMessage.To.Add(config.TraceRecipients); mailMessage.Body = FormatErrorBody(ex); mailMessage.Subject = "Star Citizen Gimp web job error."; client.Send(mailMessage); } } catch (Exception mailEx) { System.Diagnostics.Trace.WriteLine(mailEx.ToString()); } }
private static SmtpClient CreateSmtpClient(ScGimpWebJobConfig config) { SmtpClient client = new SmtpClient(config.Options.Host) { Port = config.Options.Port ?? 587, Credentials = new NetworkCredential(config.Options.Username, config.Options.Password) }; return(client); }
public static async Task Worker(TextWriter log, CancellationToken token) { try { _log = log; ScGimpWebJobConfig config = new ScGimpWebJobConfig(); _debug = config.Debug; object provider = null; if (config.Debug) { provider = new DummySubscriberProvider(); await log.WriteLineAsync($"WARNING: Debug mode is enabled."); } else { provider = new DbProvider(); } await log.WriteLineAsync($"Gimp subscriber provider: {provider.GetType().FullName}"); await log.WriteLineAsync($"Gimp discord webhook provider: {provider.GetType().FullName}"); await log.WriteLineAsync($"Gimp notification log provider: {provider.GetType().FullName}"); using ( ScGimp gimp = new ScGimp ( (ISubscriberProvider)provider, (IDiscordWebhookProvider)provider, (INotificationLogProvider)provider, config.Options ) ) { gimp.Error += OnError; gimp.Processing += OnProcessing; gimp.Processed += OnProcessed; gimp.RssFeedUpdate += OnRssFeedUpdate; gimp.SpectrumFeedUpdate += OnSpectrumFeedUpdate; gimp.StoreFeedUpdate += OnStoreFeedUpdate; await log.WriteLineAsync($"Gimp process type: {Enum.GetName(typeof(ScGimpProcessType), gimp.GetScGimpProcessType())}"); await log.WriteLineAsync($"Gimp default sleep: {gimp.Options.Sleep.ToString()}"); await log.WriteLineAsync($"Gimp after hours sleep multiplier: {gimp.Options.AfterHoursSleepMultiplier.ToString()}"); await log.WriteLineAsync($"Gimp outside comm link sleep multiplier: {gimp.Options.OutsideCommLinkSleepMultiplier.ToString()}"); await log.WriteLineAsync($"CIG comm link end: {gimp.Options.CigCommLinkEnd.ToString()}"); await log.WriteLineAsync($"CIG comm link start: {gimp.Options.CigCommLinkStart.ToString()}"); await log.WriteLineAsync($"CIG working hours end: {gimp.Options.CigWorkingHoursEnd.ToString()}"); await log.WriteLineAsync($"CIG working hours start: {gimp.Options.CigWorkingHoursStart.ToString()}"); await log.WriteLineAsync($"Starting the gimp."); await gimp.Start(); await log.WriteLineAsync($"The gimp has started."); while (!token.IsCancellationRequested) { try { // auto restart if not running. if ( gimp.Status == null || gimp.Status.Value == TaskStatus.Faulted || gimp.Status.Value == TaskStatus.Canceled || gimp.Status.Value == TaskStatus.RanToCompletion || ( gimp.LastUpdated.HasValue && gimp.LastUpdated.Value.ToUniversalTime() < DateTimeOffset.UtcNow.AddHours(-1d) ) ) { await log.WriteLineAsync($"Restarting the gimp."); await gimp.Start(); await log.WriteLineAsync($"The gimp has restarted."); } await Task.Delay(TimeSpan.FromMinutes(5), token); } catch (TaskCanceledException) { } catch (Exception ex) { LogException(ex); } } gimp.Processing -= OnProcessing; gimp.Processed -= OnProcessed; gimp.RssFeedUpdate -= OnRssFeedUpdate; gimp.SpectrumFeedUpdate -= OnSpectrumFeedUpdate; gimp.StoreFeedUpdate -= OnStoreFeedUpdate; gimp.Error -= OnError; await log.WriteLineAsync($"Stopping the gimp."); await gimp.Stop(); await log.WriteLineAsync($"The gimp has stopped."); } } catch (TaskCanceledException) { } catch (Exception exception) { LogException(exception); } }