Example #1
0
 public UnstableServiceRequestHandler(IDatabaseService db,
                                      IMessageBroker broker)
 {
     this.db     = db;
     this.broker = broker;
     this.config = ServiceConfiguration.Instance;
 }
Example #2
0
 public void SetValue(ConfigFields KeyName, string Value)
 {
     try {
         _configSettings[KeyName] = Value;
     } catch (Exception ex) {
     }
 }
Example #3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app,
                              IWebHostEnvironment env,
                              IHostApplicationLifetime lifetime)
        {
            ConfigFields conf = ServiceConfiguration.Instance;

            // reference used for onShutDown/onStartup events
            this.provider = app.ApplicationServices;

            lifetime.ApplicationStopping.Register(this.onShutDown);
            lifetime.ApplicationStarted.Register(this.onStartup);

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseMiddleware <ControllerReporter>();

            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Example #4
0
        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            ConfigFields config = ServiceConfiguration.Instance;

            return(WebHost.CreateDefaultBuilder(args)
                   .UseStartup <Startup>()
                   .UseUrls("http://+:" + config.port + "/"));
        }
Example #5
0
        public int GetInt(string name)
        {
            int intValue = 0;
            var value    = ConfigFields.FirstOrDefault(f => f.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)).With(x => x.Value).With(x => x.ToString());

            int.TryParse(value, out intValue);
            return(intValue);
        }
Example #6
0
        public InMemoryRegistryCache(IDatabaseService database)
        {
            this.database = database;
            this.config   = ServiceConfiguration.Instance;

            if (Records == null)
            {
                Records = new ConcurrentDictionary <string, SensorRegistryRecord>();
            }
        }
Example #7
0
        public PeriodicRuleEngine(IEventsCache eventsCache,
                                  IServiceProvider provider)
        {
            this.eventsCache = eventsCache;

            // used for rule engine dependencyResolver
            this.serviceProvider = provider;

            this.config = ServiceConfiguration.Instance;
        }
Example #8
0
 public string GetValue(ConfigFields KeyName)
 {
     if (_configSettings.TryGetValue(KeyName, out string _value))
     {
     }
     else
     {
         _value = "ERROR: Unable to find " + KeyName;
     }
     return(_value);
 }
Example #9
0
        private static void ProcessUnstableRecords(IEnumerable <UnstableRuleRecord> oldRecords,
                                                   IEnumerable <ServiceLifetimeEvent> oldEvents,
                                                   NRules.RuleModel.IContext ctx,
                                                   IMediator mediator)
        {
            if (oldRecords == null)
            {
                Console.WriteLine("We got null for ListRecords ..");
                return;
            }

            if (oldRecords.Count() > 0)
            {
                if (oldRecords.First().downCount < oldEvents.Count())
                {
                    ctx.Retract(oldRecords.First());
                }
                else
                {
                    // Console.WriteLine("Record is already up to date ... ");
                    return;
                }
            }

            UnstableRuleRecord newRecord = new UnstableRuleRecord(
                oldEvents.First().sourceId,
                oldEvents.Count(),
                oldEvents.ToList(),
                DateTime.Now);

            ctx.Insert(newRecord);
            // Console.WriteLine("\tRecord update: "
            //              + $"s.ID: {newRecord.serviceId} been down: "
            //              + $"{newRecord.downCount}x ... ");


            ConfigFields config = ServiceConfiguration.Instance;

            if (newRecord.downCount >= config.unstableRecordsLimit)
            {
                Console.WriteLine($"Service: {newRecord.serviceId} IS UNSTABLE ... ");
                mediator.Send(new UnstableServiceRequest(newRecord));
            }

            return;
        }
Example #10
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            this.config = ServiceConfiguration.Instance;

            this.masterToken = stoppingToken;
            this.masterToken.Register(() =>
            {
                if (this.connectionRetryTokenSrc != null)
                {
                    this.connectionRetryTokenSrc.Cancel();
                }
            });
            // this will cancel connection retry if application shutDown is requested

            ServiceConfiguration.subscribeForChange((IReloadable)this);

            this.connectionRetryTokenSrc = new CancellationTokenSource();

            this.connectionRetryTask = this.EstablishConnection(this.connectionRetryTokenSrc.Token);
            // Console.WriteLine("Before await ... exec async ...");
            await this.connectionRetryTask;
            // Console.WriteLine("After await ... exec async ...");

            if (this.masterToken.IsCancellationRequested)
            {
                if (this.connection != null &&
                    this.connection.IsOpen)
                {
                    this.connection.Close();
                }

                return;
            }

            this.connectionRetryTask     = null;
            this.connectionRetryTokenSrc = null;

            this.SetupRegistryEventConsumer();
            this.SetupConfigEventConsumer();
        }
Example #11
0
        public static T GetConfigValue <T>(ConfigFields field) where T : IConvertible
        {
            switch (field)
            {
            case ConfigFields.NAZIV_DRUSTVA:
            case ConfigFields.IBAN_PREJEMNIKA:
            case ConfigFields.BIC_BANKE:
                return((T)Convert.ChangeType(GetStringValue(configFieldsMapper[field]), typeof(T)));

            case ConfigFields.ZNESEK:
                return((T)Convert.ChangeType(GetDecimalValue(configFieldsMapper[field]), typeof(T)));

            case ConfigFields.DOLZNI_CLANI:
            case ConfigFields.DOLZNI_CLANICE:
                return((T)Convert.ChangeType(GetBoolValue(configFieldsMapper[field]), typeof(T)));

            case ConfigFields.OBDOBJE_CLANI_OD:
            case ConfigFields.OBDOBJE_CLANI_DO:
            case ConfigFields.OBDOBJE_CLANICE_OD:
            case ConfigFields.OBDOBJE_CLANICE_DO:
            case ConfigFields.OPOMINI_OD:
            case ConfigFields.OPOMINI_DO:
                return((T)Convert.ChangeType(GetIntValue(configFieldsMapper[field]), typeof(T)));

            case ConfigFields.ZNESEK_CLANI:
            case ConfigFields.ZNESEK_CLANICE:
                return((T)Convert.ChangeType(GetDecimalValue(configFieldsMapper[field]), typeof(T)));

            case ConfigFields.LASER_XOFFSET:
            case ConfigFields.LASER_YOFFSET:
            case ConfigFields.ENDLESS_XOFFSET:
            case ConfigFields.ENDLESS_YOFFSET:
                return((T)Convert.ChangeType(GetFloatValue(configFieldsMapper[field]), typeof(T)));

            case ConfigFields.DEBTS_TEMPLATE:
                return((T)Convert.ChangeType(GetStringValueFromFile(configFieldsMapper[field]), typeof(T)));
            }

            return(default(T));
        }
Example #12
0
        public Task reload(ConfigFields newConfig)
        {
            // with every timerEvent configuration is read again
            // only readInterval is kept from the initial service construction

            if (timer.Interval != newConfig.readInterval)
            {
                bool timerWasEnabled = timer.Enabled;
                timer.Stop();

                timer.Interval = newConfig.readInterval;

                if (timerWasEnabled)
                {
                    timer.Start();
                }
            }

            Console.WriteLine("Data puller reloaded ...  ");

            return(Task.CompletedTask);
        }
Example #13
0
        private string buildString(ConfigFields key)
        {
            StringBuilder _sb  = new StringBuilder();
            StringBuilder _sb2 = new StringBuilder();

            _sb.Append('~'); // Encrypted string identifier

            switch (key)
            {
            case ConfigFields.ManagerURL:
                _sb.Append(_encObj.EncryptValue(_sb2.Append("tcp://")
                                                .Append(GetValue(key))
                                                .Append(":")
                                                .Append(GetValue(ConfigFields.ManagerPort))
                                                .Append("/MerchantCaptureCommObj.rem")
                                                .ToString()
                                                ));
                break;

            case ConfigFields.SecondaryManagerURL:
                _sb.Append(_encObj.EncryptValue(_sb2.Append("tcp://")
                                                .Append(GetValue(key))
                                                .Append(":")
                                                .Append(GetValue(ConfigFields.SecondaryManagerPort))
                                                .Append("/MerchantCaptureCommObj.rem")
                                                .ToString()
                                                ));
                break;

            case ConfigFields.UIN:
                _sb.Append(_encObj.EncryptValue(GetValue(key)));
                break;

            default:
                int STOP = 1;
                break;
            }
            return(_sb.ToString());
        }
Example #14
0
        public async Task backupConfiguration(ServiceConfiguration oldConfig)
        {
            if (!await createConnection())
            {
                return;
            }

            ConfigFields config = ServiceConfiguration.Instance;

            string collectionName = config.configurationBackupCollection;
            var    collection     = database
                                    .GetCollection <ConfigBackupRecord>(collectionName);

            var newRecord = new DatedConfigRecord(oldConfig, DateTime.Now);

            var update = Builders <ConfigBackupRecord>
                         .Update
                         .Push(r => r.oldConfigs, newRecord);

            try
            {
                collection.UpdateOne(
                    r => r.serviceId == config.serviceId,
                    update,
                    new UpdateOptions {
                    IsUpsert = true
                });

                return;
            }
            catch (TimeoutException)
            {
                Console.WriteLine($"Failed to connect with the database on: "
                                  + $"{config.dbAddress} ... ");

                return;
            }
        }
Example #15
0
        public Task reload(ConfigFields newConfig)
        {
            bool timerWasEnabled = timer.Enabled;

            timer.Stop();

            // currently there is no point in recreating ruleEngine
            // it doesn't depend on current configuration
            // rules can't be changed (each rule is a single class)
            // setupRuleEngine();

            this.config = newConfig;

            timer.Interval = config.ruleEngineTriggerInterval;

            if (timerWasEnabled)
            {
                timer.Start();
            }

            Console.WriteLine("PeriodicRuleEngine reloaded using new config ... ");

            return(Task.CompletedTask);
        }
Example #16
0
        private async void timerEvent(Object source, ElapsedEventArgs arg)
        {
            // prevent calling this method again until the current execution is done
            // in case timer.Interval is shorter than the time required to pull data
            // from all the sensors
            timer.Stop();

            ConfigFields config = ServiceConfiguration.Instance;

            List <SensorRegistryRecord> availableSensors =
                await mediator.Send(new GetAllSensorsRequest());

            int triedToPullCount = 0;

            Console.WriteLine($"Available sensors count: {availableSensors.Count} ... ");
            foreach (var singleSensor in availableSensors)
            {
                long alreadyReadCount = 0;
                if (!lastReadIndexes.TryGetValue(
                        singleSensor.Name,
                        out alreadyReadCount))
                {
                    // get the count of already read records from this sensor
                    // db access
                    alreadyReadCount = await mediator
                                       .Send(new GetRecordsCountRequest(singleSensor.Name));

                    if (alreadyReadCount == -1)
                    {
                        Console.WriteLine("Failed to get read count for: "
                                          + $"{singleSensor.Name}");
                        // something went wrong
                        // most possibly failed to connect with the db

                        continue;                         // read from the next senor
                    }

                    lastReadIndexes.Add(singleSensor.Name, alreadyReadCount);
                }

                if (alreadyReadCount >= singleSensor.AvailableRecords)
                {
                    long diff = alreadyReadCount - singleSensor.AvailableRecords;
                    Console.WriteLine($"{singleSensor.Name} -> no new records "
                                      + $"waiting for: {diff} reads ... ");

                    continue;                     // read from the next sensor
                }

                string sensorAddr = $"http://{singleSensor.Address}:{singleSensor.Port}";
                string api_url    = $"{sensorAddr}/{config.dataRangeUrl}?"
                                    + $"sensorName={singleSensor.Name}&"
                                    + $"index={alreadyReadCount}";

                Uri sensorUri = new Uri(api_url);
                Console.Write($"{singleSensor.Name}@{sensorAddr} "
                              + $" | from: {alreadyReadCount} -> ");

                HttpResponseMessage response = null;
                try
                {
                    triedToPullCount++;
                    response = await this.httpClient.GetAsync(sensorUri);
                }
                catch (AggregateException e)
                {
                    // most possibly this exception happened because data pulling
                    // is started with old list of sensors
                    // sensors are removed from registry (shutdown events are received)
                    // after 'current sensors list is returned'
                    if (e.InnerException is HttpRequestException)
                    {
                        string message =
                            ((HttpRequestException)e.InnerException).Message;

                        Console.WriteLine(
                            $"\nHttp req. exception, message: {message} ... "
                            + $"sensor may be down.\n"
                            + $"Sensor addr. : {sensorUri.ToString()}\n");

                        var newEvent = new CollectorPullEvent(
                            config.serviceId,
                            sensorUri.ToString(),
                            false,
                            0,
                            e.Message);

                        // handler for this request is not async
                        // but i guess it is a good idea to await it anyway
                        await mediator.Send(new PublishCollectorPullEventRequest(newEvent));

                        continue;                         // pull from the next sensor
                    }
                }
                catch (HttpRequestException e)
                {
                    // same handler as for above catch ...
                    // i guess aggregateException is removed after some update
                    string message = e.Message;

                    Console.WriteLine(
                        $"\nHttp req. exception, message: {message} ... "
                        + $"sensor may be down.\n"
                        + $"Sensor addr. : {sensorUri.ToString()}\n");

                    var newEvent = new CollectorPullEvent(
                        config.serviceId,
                        sensorUri.ToString(),
                        false,
                        0,
                        e.Message);

                    // handler for this request is not async
                    // but i guess it is a good idea to await it anyway
                    await mediator.Send(new PublishCollectorPullEventRequest(newEvent));

                    continue;                     // pull from the next sensor
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Unexpected exception while pulling data: "
                                      + $" {e.ToString()}");
                    continue;                     // pull from the next sensor
                }

                if (response == null || !response.IsSuccessStatusCode)
                {
                    // this will continue previous Console.Write( ... );
                    Console.WriteLine($" bad response ... ");

                    var newEvent = new CollectorPullEvent(
                        config.serviceId,
                        sensorUri.ToString(),
                        false,
                        0,
                        "Sensor returned bad response.");

                    // handler for this request is not async
                    // but i guess it is a good idea to await it anyway
                    await mediator.Send(new PublishCollectorPullEventRequest(newEvent));

                    continue;                     // pull from the next sensor
                }

                string txtContent = await response.Content.ReadAsStringAsync();

                // var dataRecords = System
                //      .Text
                //      .Json
                //      .JsonSerializer
                //      .Deserialize<SensorDataRecords>(txtResponseContent);
                // newtonsoft serializes properties to camelCase so when they get
                // pulled from sensor system.text.json can't deserialize it because
                // class properties are actually in PascalCase, that is the reason
                // to use newtonsoft - easier than forcing it to serialize in PascalCase

                var dataRecords = JsonConvert
                                  .DeserializeObject <SensorDataRecords>(txtContent);

                Console.WriteLine($"returned {dataRecords.RecordsCount} rows ... ");

                var pullEvent = new CollectorPullEvent(
                    config.serviceId,
                    sensorUri.ToString(),
                    true,
                    dataRecords.RecordsCount);

                await mediator.Send(new PublishCollectorPullEventRequest(pullEvent));

                var addResult = await mediator.Send(
                    new AddRecordsToSensorRequest(
                        singleSensor.Name,
                        dataRecords.CsvHeader,
                        dataRecords.CsvValues)
                    );

                if (addResult != true)
                {
                    // adding the new records was not successfull
                    // most possibly database is down ...
                    // this records will have to be pulled again

                    continue;
                }

                // update read index for every sensor that returned records
                // at this point lastReadIndex[singleSensor.Name] has to exists
                // (look at the beginning of this for loop)
                lastReadIndexes[singleSensor.Name] =
                    alreadyReadCount + dataRecords.RecordsCount;
            }

            Console.WriteLine($"Tried to pull from: {triedToPullCount} ...");
            // 'schedule' the next data pulling
            timer.Start();
        }
Example #17
0
        // has to be async because of the connection retry
        public async Task reload(ConfigFields newConfig)
        {
            // this.config should refer to old config
            if (config.brokerAddress == newConfig.brokerAddress &&
                config.brokerPort == newConfig.brokerPort)
            {
                // there is no need to reload this service
                return;
            }


            // in case that connection (using old config) is still not established
            if (this.connectionRetryTokenSrc != null)
            {
                // cancel previous connection retries
                this.connectionRetryTokenSrc.Cancel();
                if (this.connectionRetryTask != null)
                {
                    await this.connectionRetryTask;
                }
            }

            this.config = newConfig;

            if (this.channel != null &&
                this.channel.IsOpen)
            {
                this.channel.Close();
            }

            if (this.connection != null &&
                this.connection.IsOpen)
            {
                this.connection.Close();
            }

            this.connectionRetryTokenSrc = new CancellationTokenSource();

            this.connectionRetryTask = this.EstablishConnection(this.connectionRetryTokenSrc.Token);
            await this.connectionRetryTask;

            if (this.masterToken.IsCancellationRequested)
            {
                if (this.connection != null &&
                    this.connection.IsOpen)
                {
                    this.connection.Close();
                }

                return;
            }

            this.connectionRetryTokenSrc = null;

            if (this.connection == null ||
                !this.connection.IsOpen)
            {
                return;
            }

            this.SetupRegistryEventConsumer();
            this.SetupConfigEventConsumer();

            Console.WriteLine("Broker event receiver reloaded ... ");
        }
Example #18
0
 /// <summary>
 /// Sets the application settings to override the default.
 /// </summary>
 /// <param name="appSettings">The application settings.</param>
 /// <remarks>Default AppSettings is <see cref="ConfigurationManager.AppSettings"/></remarks>
 public static void SetAppSettings(NameValueCollection appSettings) => ConfigFields
 .ForEach(configFieldInfo => configFieldInfo.Value.SetAppSettings(appSettings));
Example #19
0
 public RabbitMqBroker()
 {
     this.config = ServiceConfiguration.Instance;
 }
Example #20
0
 public MongoStorage()
 {
     this.config = ServiceConfiguration.Instance;
 }
Example #21
0
 public string GetString(string name)
 {
     return(ConfigFields.FirstOrDefault(f => f.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)).With(x => x.Value as string));
 }
Example #22
0
 public MongoDatabaseService()
 {
     this.config = ServiceConfiguration.Instance;
 }
Example #23
0
 public bool GetBool(string name)
 {
     return((bool)(ConfigFields.FirstOrDefault(f => f.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)).With(x => x.Value)));
 }