Example #1
0
        /// <summary>
        /// Re-query the provided model to ensure it is in a sane state. This method requires explicit implementation per model type.
        /// </summary>
        /// <param name="model"></param>
        /// <param name="contextFactory"></param>
        public static void Requery(this IHasPrimaryKey model, IDatabaseContextFactory contextFactory)
        {
            switch (model)
            {
            case SkinInfo skinInfo:
                requeryFiles(skinInfo.Files, contextFactory);
                break;

            case ScoreInfo scoreInfo:
                requeryFiles(scoreInfo.Beatmap.BeatmapSet.Files, contextFactory);
                requeryFiles(scoreInfo.Files, contextFactory);
                break;

            case BeatmapSetInfo beatmapSetInfo:
                var context = contextFactory.Get();

                foreach (var beatmap in beatmapSetInfo.Beatmaps)
                {
                    // Workaround System.InvalidOperationException
                    // The instance of entity type 'RulesetInfo' cannot be tracked because another instance with the same key value for {'ID'} is already being tracked.
                    beatmap.Ruleset = context.RulesetInfo.Find(beatmap.RulesetID);
                }

                requeryFiles(beatmapSetInfo.Files, contextFactory);
                break;

            default:
                throw new ArgumentException($"{nameof(Requery)} does not have support for the provided model type", nameof(model));
            }

            void requeryFiles <T>(List <T> files, IDatabaseContextFactory databaseContextFactory) where T : class, INamedFileInfo
Example #2
0
 private string?getRulesetShortNameFromLegacyID(long rulesetId) =>
 efContextFactory?.Get().RulesetInfo.FirstOrDefault(r => r.ID == rulesetId)?.ShortName;
Example #3
0
 private DbSet <TModel> queryModel() => ContextFactory.Get().Set <TModel>();
Example #4
0
        public void Flush()
        {
            bool AddLogsToDatabase(IAuditDatabaseContext databaseContext)
            {
                var _hadLogs = false;

                using (var session = _warewolfQueue.OpenSession())
                {
                    for (int insertCount = 0; insertCount <= 250; insertCount++)
                    {
                        var auditLog = session.Dequeue <AuditLog>();
                        if (auditLog is null)
                        {
                            break;
                        }
                        databaseContext.Audits.Add(auditLog);
                        _hadLogs = true;
                    }
                }
                return(_hadLogs);
            }

            void TryGetDatabaseContext(ref IAuditDatabaseContext databaseContext)
            {
                int count = MAX_DATABASE_TRIES;

                do
                {
                    try
                    {
                        databaseContext = _databaseContextFactory.Get();
                    }
                    catch (Exception)
                    {
                        count--;
                        if (count <= 0)
                        {
                            throw;
                        }
                    }
                    Thread.Sleep(DATABASE_RETRY_DELAY);
                } while (databaseContext is null && --count >= 0);
            }

            if (_warewolfQueue.IsEmpty())
            {
                return;
            }

            IAuditDatabaseContext database = null;
            var hadLogs = false;

            do
            {
                TryGetDatabaseContext(ref database);

                using (database)
                {
                    using (var transaction = database.Database.BeginTransaction())
                    {
                        try
                        {
                            try
                            {
                                hadLogs = AddLogsToDatabase(database);
                            }
                            finally
                            {
                                Flush(database, MAX_DATABASE_TRIES);
                            }
                            transaction.Commit();
                        } catch
                        {
                            transaction.Rollback();
                        }
                    }
                }
            } while (hadLogs);
        }