private void SafeRun()
        {
            try {
                _services.Logger.Info("Starting Prosumer Results Archiving Thread", _myStage, nameof(ProsumerComponentResultArchiver));
                //main thread
                while (!_cancellationTokenSource.Token.IsCancellationRequested)
                {
                    try {
                        var prosumer = _myqueue.Take(_cancellationTokenSource.Token);
                        ArchiveOneEntry(prosumer);
                    }

                    catch (OperationCanceledException) {
                        _services.Logger.Info("canceled waiting", Stage.ProfileGeneration, "Archiver");
                    }
#pragma warning disable CA1031 // Do not catch general exception types
                    catch (Exception ex) {
#pragma warning restore CA1031 // Do not catch general exception types
                        _services.Logger.ErrorM(ex.GetType().FullName + ": " +
                                                ex.Message + " " + ex.StackTrace, _myStage, "ComponentArchiver");
                    }
                }
                _services.Logger.Info("Shutting down Prosumer Results Archiving Thread, " + _myqueue.Count + " entries left in queue", _myStage, nameof(ProsumerComponentResultArchiver));
                //clean up queue
                while (_myqueue.Count > 0)
                {
                    var prosumer = _myqueue.Take();
                    ArchiveOneEntry(prosumer);
                }

                if (_generationSa != null)
                {
                    _generationSa.SaveDictionaryToDatabase(_services.Logger);
                }

                if (_loadSa != null)
                {
                    _loadSa.SaveDictionaryToDatabase(_services.Logger);
                }

                _services.Logger.Info("Finished Shutting down Prosumer Results Archiving Thread, archived " + _archiveCount + " prosumers", _myStage, nameof(ProsumerComponentResultArchiver));
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex) {
#pragma warning restore CA1031 // Do not catch general exception types
                _services.Logger.ErrorM(ex.Message + " " + ex.StackTrace, _myStage, "ComponentArchiver");
            }
        }
        public bool PrepareLoadProfileIfNeeded([NotNull] ProviderParameterDto parameters)
        {
            Module.SetPrint(0);
            //var relevantPotentials = _pvPotentials.Where(x => x.HouseGuid == houseComponent.HouseGuid);
            PvSystemEntry entry = (PvSystemEntry)parameters.HouseComponent;
            int           idx   = 0;

            foreach (var area in entry.PVAreas)
            {
                var key    = MakeKeyFromPVArea(area);
                var keystr = key.GetKey();
                //key has been checked in this run
                if (_checkedKeys.Contains(keystr))
                {
                    continue;
                }

                _checkedKeys.Add(keystr);
                bool isInDb = _saveableEntries.CheckForName(keystr, MyLogger);
                if (isInDb)
                {
                    continue;
                }

                Info("Missing pv profile for " + keystr + ", generating...");
                PVSystemSettings pvs = new PVSystemSettings(key, 1, 1, MyLogger, idx++);
                var profile          = pvs.Run(Services.RunningConfig);
                _saveableEntries.AddRow(profile);
                _saveableEntries.SaveDictionaryToDatabase(MyLogger);
            }

            return(true);
        }
        public void TestCreationSavingAndKeyCheck()
        {
            SqlConnectionPreparer ms = new SqlConnectionPreparer(Config);
            var     db = ms.GetDatabaseConnection(Stage.Testing, Constants.PresentSlice);
            Profile p  = new Profile("myProfile", new List <double>().AsReadOnly(), EnergyOrPower.Power);
            SaveableEntry <Profile> sa = SaveableEntry <Profile> .GetSaveableEntry(db, SaveableEntryTableType.PVGeneration, Logger);

            sa.MakeCleanTableForListOfFields(true);
            sa.MakeTableForListOfFieldsIfNotExists(true);
            string myKey = p.Name;

            if (sa.CheckForName(myKey, Logger))
            {
                throw new FlaException("Key already exists in cleared db");
            }

            sa.AddRow(p);
            sa.SaveDictionaryToDatabase(Logger);
            if (!sa.CheckForName(myKey, Logger))
            {
                throw new FlaException("Saving failed. Key not in db");
            }
        }