public void HandleStarted() { try { _healthNotifier.Notify("Initializing"); _startupManager.StartAsync().GetAwaiter().GetResult(); _healthNotifier.Notify("Application is started"); if (_hostingEnvironment.IsDevelopment()) { return; } if (_monitoringServiceClientSettings?.CurrentValue == null) { throw new ApplicationException("MonitoringServiceClient settings is not provided."); } _configurationRoot .RegisterInMonitoringServiceAsync(_monitoringServiceClientSettings.CurrentValue.MonitoringServiceUrl, _healthNotifier) .GetAwaiter() .GetResult(); } catch (Exception ex) { _log.Critical(ex); throw; } }
private async Task StartApplication() { try { // NOTE: Job not yet recieve and process IsAlive requests here await ApplicationContainer.Resolve <IStartupManager>().StartAsync(); _healthNotifier?.Notify("Started", Program.EnvInfo); } catch (Exception ex) { Log.Critical(ex); throw; } }
private async Task StartApplication() { try { // NOTE: Service not yet recieve and process requests here await ApplicationContainer.Resolve <IStartupManager>().StartAsync(); _healthNotifier.Notify("Started"); } catch (Exception ex) { _log?.Critical(ex); throw; } }
private Task CleanUp() { _healthNotifier?.Notify("Terminating"); _healStatusPeriodicalHandler.Stop(); _removeOldSpentOutputsPeriodicalHandler.Stop(); return(Task.CompletedTask); }
private async Task StartApplication() { #if !DEBUG await Configuration.RegisterInMonitoringServiceAsync(_monitoringServiceUrl, _healthNotifier); #endif _removeOldSpentOutputsPeriodicalHandler.Start(); _removeOldSpentOutputsPeriodicalHandler.Start(); _healthNotifier.Notify("Started"); }
public async Task HandleStartedAsync() { try { _healthNotifier.Notify("Initializing"); await _startupManager.StartAsync(); _healthNotifier.Notify("Application is started"); if (_hostingEnvironment.IsDevelopment()) { return; } } catch (Exception ex) { _log.Critical(ex); throw; } }
private async Task StartApplication() { try { // NOTE: Service not yet recieve and process requests here await ApplicationContainer.Resolve <IStartupManager>().StartAsync(); _healthNotifier.Notify("Started", $"Env: {Program.EnvInfo}"); #if !DEBUG if (!string.IsNullOrEmpty(_monitoringServiceUrl)) { await Configuration.RegisterInMonitoringServiceAsync(_monitoringServiceUrl, _healthNotifier); } #endif } catch (Exception ex) { _log.Critical(ex); throw; } }
private void CleanUp() { try { _healthNotifier?.Notify("Terminating", Program.EnvInfo); ApplicationContainer.Dispose(); } catch (Exception ex) { _log?.Critical(ex); throw; } }
private void CleanUp() { try { // NOTE: Job can't recieve and process IsAlive requests here, so you can destroy all resources _healthNotifier?.Notify("Terminating", Program.EnvInfo); ApplicationContainer.Dispose(); } catch (Exception ex) { _log?.Critical(ex); throw; } }
private async Task StartApplication() { try { // NOTE: Job not yet recieve and process IsAlive requests here #if !DEBUG await Configuration.RegisterInMonitoringServiceAsync(_monitoringServiceUrl, _healthNotifier); #endif _triggerHost = new TriggerHost(new AutofacServiceProvider(ApplicationContainer)); _triggerHostTask = _triggerHost.Start(); _healthNotifier.Notify("Started", Program.EnvInfo); } catch (Exception ex) { _log.Critical(ex); throw; } }
private async Task StartApplication() { try { // NOTE: Job not yet recieve and process IsAlive requests here await ApplicationContainer.Resolve <IStartupManager>().StartAsync(); _healthNotifier.Notify("Started", Program.EnvInfo); #if !DEBUG await Configuration.RegisterInMonitoringServiceAsync(_monitoringServiceUrl, _healthNotifier); #endif } catch (Exception ex) { _log.Critical(ex); throw; } }
private async Task StartApplication() { try { // NOTE: Service not yet recieve and process requests here _healthNotifier.Notify("Started"); #if !DEBUG if (!string.IsNullOrEmpty(_monitoringServiceUrl)) { await Configuration.RegisterInMonitoringServiceAsync(_monitoringServiceUrl, _healthNotifier); } #endif } catch (Exception ex) { _log.Critical(ex); throw; } }
public async Task <IReadOnlyCollection <TradeHistoryItem> > GetNextBatchAsync() { // If we got the last batch in the previous iteration, there is no reason to execute one more query // with empty result. Just return. if (_gotTheLastBatch) { return(Array.Empty <TradeHistoryItem>()); } try { var result = new List <TradeHistoryItem>(); // Renew the connection on every call. using (var sqlConnection = new SqlConnection(_sqlConnString)) { sqlConnection.Open(); if (sqlConnection.State != ConnectionState.Open) { throw new InvalidOperationException("Can't fetch from DB while connection is not opened."); } _log.Info(nameof(GetNextBatchAsync), $"Trying to fetch next {_sqlQueryBatchSize} rows...", $"Starting offset = {StartingRowOffset}, asset pair ID = {AssetPairId}"); using (var sqlCommand = BuildCurrentQueryCommand(sqlConnection)) { sqlCommand.CommandTimeout = (int)_sqlTimeout.TotalSeconds; using (var reader = await sqlCommand.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { var trade = new TradeHistoryItem { Id = reader.GetInt64(0), AssetToken = reader.GetString(1), Direction = (TradeDirection)Enum.Parse(typeof(TradeDirection), reader.GetString(2)), Volume = reader.GetDecimal(3), Price = reader.GetDecimal(4), DateTime = reader.GetDateTime(5), OppositeVolume = reader.GetDecimal(6), OrderId = Guid.Parse(reader.GetString(7)), OppositeOrderId = Guid.Parse(reader.GetString(8)), TradeId = reader.GetString(9), IsStraight = reader.GetString(1) == SearchToken // If the trade is straight or reverse. }; // We must ignore trades with negative prices and\or volumes (if any). if (trade.Price > 0 && trade.Volume > 0 && trade.OppositeVolume > 0) { result.Add(trade); } else { _healthNotifier.Notify("Got a trade with non-posotive price or volume(s) values.", trade); } } } } sqlConnection.Close(); } if (result.Count > 0) { // Now we need to remove the last several trades which have the same date and time (accuracy - to seconds). // This will guarantee that we did not peek up some orders of the same trade on this iteration, and others // on the next. On the next iteration we will read them again for the next batch. No temporary buffer, for // it can't save any observable value of time. NOTE: if we have got less records than _sqlQueryBatchSize, // this means that we obtained the last (or the single) data pack, and there is no reason to delete "tail" // trades. if (result.Count == _sqlQueryBatchSize) { var lastDateTime = result.Last().DateTime.TruncateTo(CandleTimeInterval.Sec); var resultWithoutTail = result.TakeWhile(t => t.DateTime < lastDateTime).ToList(); if (!resultWithoutTail.Any()) { throw new InvalidOperationException($"Got an SQL data batch of {result.Count} trade records with the same timestamp {lastDateTime:O}. " + $"Migration for asset pair {AssetPairId} will be terminated. Row offset was {StartingRowOffset} before the incident."); } result = resultWithoutTail; } else { _gotTheLastBatch = true; // If we have got smaller amount of records than _sqlQueryBatchSize, this only means we have the last batch now. } _log.Info(nameof(GetNextBatchAsync), $"Fetched {result.Count} rows successfully. First date is {result.First().DateTime:O}, last date is {result.Last().DateTime:O}", $"Starting offset = {StartingRowOffset}, asset pair ID = {AssetPairId}"); StartingRowOffset += result.Count; } else { _log.Info(nameof(GetNextBatchAsync), "No data to fetch.", $"Starting offset = {StartingRowOffset}, asset pair ID = {AssetPairId}"); } return(result); } catch (Exception ex) { _log.Error(nameof(GetNextBatchAsync), ex); // We can just report about the error and return an empty list - this will be interpreted as "no data". return(Array.Empty <TradeHistoryItem>()); } }
/// <summary> /// Registers calling application in monitoring service based on application url from environemnt variable. /// </summary> /// <param name="configuration">Application configuration that is used for environemnt variable search.</param> /// <param name="monitoringServiceUrl">Monitoring service url.</param> /// <param name="healthNotifier">Health notifier</param> /// <returns></returns> public static async Task RegisterInMonitoringServiceAsync( [NotNull] this IConfigurationRoot configuration, [NotNull] string monitoringServiceUrl, [NotNull] IHealthNotifier healthNotifier) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } var disableAutoRegistrationStr = configuration[DisableAutoRegistrationEnvVarName]; if (bool.TryParse(disableAutoRegistrationStr, out bool disableAutoRegistration) && disableAutoRegistration) { return; } if (string.IsNullOrWhiteSpace(monitoringServiceUrl)) { throw new ArgumentException("Argument is empty", nameof(monitoringServiceUrl)); } if (healthNotifier == null) { throw new ArgumentNullException(nameof(healthNotifier)); } var podTag = configuration[PodNameEnvVarName] ?? string.Empty; try { var myMonitoringUrl = configuration[MyMonitoringUrlEnvVarName]; if (string.IsNullOrWhiteSpace(myMonitoringUrl)) { myMonitoringUrl = MissingEnvVarUrl; healthNotifier.Notify( $"{MyMonitoringUrlEnvVarName} environment variable is not found. Using {myMonitoringUrl} for monitoring registration", podTag); } var myMonitoringName = configuration[MyMonitoringNameEnvVarName]; if (string.IsNullOrWhiteSpace(myMonitoringName)) { myMonitoringName = PlatformServices.Default.Application.ApplicationName; } var monitoringService = new MonitoringServiceFacade(monitoringServiceUrl); try { var monitoringRegistration = await monitoringService.GetService(myMonitoringName); if (monitoringRegistration.Url == myMonitoringUrl) { return; } healthNotifier.Notify($"There is a registration for {myMonitoringName} in monitoring service!", podTag); myMonitoringUrl = MissingEnvVarUrl; var instanceTag = string.IsNullOrEmpty(podTag) ? Guid.NewGuid().ToString() : podTag; myMonitoringName = $"{myMonitoringName}-{instanceTag}"; } catch { //Duplicated registration is not found - proceed with usual registration } await monitoringService.MonitorUrl( new UrlMonitoringObjectModel { Url = myMonitoringUrl, ServiceName = myMonitoringName, }); healthNotifier.Notify($"Auto-registered in Monitoring with name {myMonitoringName} on {myMonitoringUrl}", podTag); } catch (Exception ex) { healthNotifier.Notify(ex.ToAsyncString(), podTag); } }
public void SendNotification(IHealthNotifier healthNotifier, LogInformationDto logInformation) { healthNotifier.Notify(PrepareHealthMessage(logInformation), logInformation.Context); }
Task ILog.WriteMonitorAsync(string component, string process, string context, string info, DateTime?dateTime) { _healthNotifier.Notify(info, context); return(Task.CompletedTask); }