public Service1() { InitializeComponent(); this.CanStop = true; this.CanPauseAndContinue = false; this.AutoLog = true; var configuration = new Configuration(); var credentials = new VssBasicCredential(string.Empty, configuration.TfsToken); var connection = new VssConnection(configuration.TfsUri, credentials); var tfsClient = connection.GetClient <WorkItemTrackingHttpClient>(); var receiver = new TfsNotUpdatedReceiver(tfsClient, configuration); var receiver2 = new TfsReceiver1(tfsClient, configuration); var slackClient = new SlackClient(configuration.SlackUri.ToString()); var sender = new SlackSender(slackClient, configuration); var logger = new EventLogLogger(configuration); var aggregatedReceiver = new AggregatedReceiver(receiver, receiver2); var aggregatedSender = new AggregatedSender(sender); this.notifier = new Notifier(aggregatedReceiver, aggregatedSender, logger, configuration); }
public void WhenSendMessagesSuccess() { var restResponceMock = new Mock <IRestResponse>(); restResponceMock.SetupGet(x => x.ResponseStatus).Returns(ResponseStatus.Completed); var slackClientMock = new Mock <ISlackClient>(); slackClientMock.Setup(x => x.PostAsync(It.IsAny <SlackMessage>())).ReturnsAsync(restResponceMock.Object); var configurationMock = new Mock <IConfiguration>(); configurationMock.SetupGet(x => x.SlackChannel); configurationMock.SetupGet(x => x.SlackUserName); var sender = new SlackSender(slackClientMock.Object, configurationMock.Object); var messages = new[] { new Message { Title = "Message1" }, new Message { Title = "Message2" } }; var actual = sender.SendMessages(messages, CancellationToken.None).Result; Assert.AreEqual(true, actual); slackClientMock.Verify(x => x.PostAsync(It.IsAny <SlackMessage>()), Times.Exactly(2)); }
public static async Task RestoreAsync(RestoreCommand restoreCommand, ILogger logger, CancellationToken cancellationToken) { await using var connection = restoreCommand.CreateConnectionMars(); var state = new RestoreState(logger, restoreCommand, connection); state.LogStarting(); await state.EnsuredIndexsExistsAsync(); await Task.WhenAll( state.LoadServerInfosAsync(), state.LoadDatabasesAsync(), state.LoadRestoreHistoryAsync(), state.PrepareRestoreJobAsync()); logger.Information($"{state.Directories.Count} directories found"); logger.Information("Starting..."); var po = new ParallelizeOption { FailMode = Fail.Smart, MaxDegreeOfParallelism = restoreCommand.Threads }; IRestoreMethod method = restoreCommand.Brentozar ? (IRestoreMethod) new BrentozarRestoreMethod(state, connection) : new NativeRestoreMethod(state); await state.Directories .ParallelizeAsync((item, cancel) => RestoreBackupAsync(method, item, state), po, cancellationToken) .ForEachAsync((value, token) => { state.Accumulated = state.Accumulated.Add(value.Item.Elapsed); return(Task.CompletedTask); }); var report = await state.GetReportStateAsync(); state.LogFinished(report); var slack = new SlackSender(new SlackClient(logger)); await slack.ReportAsync(report, state.RestoreCommand.SlackChannel, state.RestoreCommand.SlackSecret, state.RestoreCommand.SlackOnlyOnError, state.RestoreCommand.SlackTitle); await report.SendMailAsync(state.RestoreCommand.Email, state.RestoreCommand.Smtp, cancellationToken); if (report.Status.HasFlag(ReportStatus.Error)) { Environment.ExitCode = 1; } }
public Task OnMessage(Message theMessage, string slackWebhookToSendAlert, string slackChannelToSendAlert) { Task returnTask; try { string dataJSON = Encoding.UTF8.GetString(theMessage.Body); MonitorMessage msg = JsonConvert.DeserializeObject <MonitorMessage>(dataJSON); _logger.LogWarning($"Error scenario found on {msg.MonitoredService} service"); StringBuilder sb = new StringBuilder(); sb.AppendLine($"*:warning: Infrastructure Problem Detected*"); sb.AppendLine($"_{msg.MonitoredService}_"); sb.Append($"_{msg.StartTime}_"); SlackSender connector = new SlackSender(slackWebhookToSendAlert, slackChannelToSendAlert); returnTask = connector.GetJSONResponse(sb.ToString()); } catch (Exception ex) { _logger.LogError($"ERROR - {ex.Message} {ex.StackTrace}"); returnTask = Task.FromException(ex); } return(returnTask); }