Exemple #1
0
        private static void PerformanceCacheDbTest(SelectTestDefinition[] testDefinitions, int testCount)
        {
            var pool = new DbContextPool <OrderContext>(OrderContextOptions.Create(true, null));

            Db.OeEntitySetAdapterCollection entitySetAdapters = new OrderDbDataAdapter(false, false, null).EntitySetAdapters;

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            var stopWatch = new Stopwatch();

            stopWatch.Start();
            for (int i = 0; i < testCount; i++)
            {
                foreach (SelectTestDefinition testDefinition in testDefinitions)
                {
                    OrderContext dbContext = pool.Rent();
                    testDefinition.ExecutorDb(entitySetAdapters, dbContext);
                    pool.Return(dbContext);
                }
            }
            stopWatch.Stop();
            Console.WriteLine("Entity Framework " + stopWatch.Elapsed);
        }
Exemple #2
0
        public async Task <string> ToOsuString(DbContextPool <SoraDbContext> ctxPool)
        {
            var returnString = string.Empty;

            if (_bm == null)
            {
                Logger.Err("Failed to set Beatmap! Beatmap is null!");
                return($"{(int) RankedStatus.NeedUpdate}|false\n");
            }

            var count = 0;

            if (_ownScore != null)
            {
                var ctx = ctxPool.Rent();
                try
                {
                    returnString += $"{_ownScore?.ToOsuString(ctx)}\n";
                }
                finally
                {
                    ctxPool.Return(ctx);
                }
            }

            await foreach (var score in _scores)
            {
                count++;
                var ctx = ctxPool.Rent();
                try
                {
                    returnString += $"{score.ToOsuString(ctx)}\n";
                }
                finally
                {
                    ctxPool.Return(ctx);
                }
            }

            return(ScoreboardHeader(count) + returnString);
        }
Exemple #3
0
        public override void CloseDataContext(Object dataContext)
        {
            var dbContext = (T)dataContext;

            if (_dbContextPool == null)
            {
                dbContext.Dispose();
            }
            else
            {
                _dbContextPool.Return(dbContext);
            }
        }
Exemple #4
0
        public override IEnumerable <DynamicPropertyInfo> GetStructuralProperties(String tableName)
        {
            SchemaContext schemaContext = _dbContextPool.Rent();

            foreach (DbColumn column in schemaContext.GetColumns(tableName))
            {
                DatabaseGeneratedOption databaseGenerated;
                if (column.IsIdentity.GetValueOrDefault())
                {
                    databaseGenerated = DatabaseGeneratedOption.Identity;
                }
                else if (column.IsExpression.GetValueOrDefault())
                {
                    databaseGenerated = DatabaseGeneratedOption.Computed;
                }
                else
                {
                    databaseGenerated = DatabaseGeneratedOption.None;
                }
                yield return(new DynamicPropertyInfo(column.ColumnName, column.DataType, databaseGenerated));
            }
            _dbContextPool.Return(schemaContext);
        }
Exemple #5
0
        protected override async Task <IReadOnlyDictionary <int, Speaker> > LoadBatchAsync(
            IReadOnlyList <int> keys,
            CancellationToken cancellationToken)
        {
            ApplicationDbContext dbContext = _dbContextPool.Rent();

            try
            {
                return(await dbContext.Speakers
                       .Where(s => keys.Contains(s.Id))
                       .ToDictionaryAsync(t => t.Id, cancellationToken));
            }
            finally
            {
                _dbContextPool.Return(dbContext);
            }
        }
Exemple #6
0
        protected override async Task <ILookup <int, Session> > LoadGroupedBatchAsync(
            IReadOnlyList <int> keys,
            CancellationToken cancellationToken)
        {
            ApplicationDbContext dbContext = _dbContextPool.Rent();

            try
            {
                var sessions = await dbContext.Tracks
                               .Where(track => keys.Contains(track.Id))
                               .Include(track => track.Sessions)
                               .SelectMany(track => track.Sessions)
                               .ToListAsync();

                return(sessions.ToLookup(t => t.TrackId !.Value));
            }
            finally
            {
                _dbContextPool.Return(dbContext);
            }
        }
Exemple #7
0
        protected override void ThreadWorker()
        {
            while (!CancellationToken.IsCancellationRequested)
            {
                var dbContext = _dbContextPool.Rent();
                try
                {
                    var beatmaps = dbContext.BeatmapSet
                                   .Where(x => !x.Disabled)
                                   .AsEnumerable()
                                   .Where(x => x.LastChecked != null)
                                   .Where(
                        x => (
                            (
                                (
                                    x.RankedStatus == BeatmapSetOnlineStatus.None ||
                                    x.RankedStatus == BeatmapSetOnlineStatus.Loved
                                ) &&
                                (x.LastChecked.Value + TimeSpan.FromDays(90)).Subtract(DateTime.Now).TotalMilliseconds < 0
                            ) ||
                            (
                                (
                                    x.RankedStatus == BeatmapSetOnlineStatus.Pending ||
                                    x.RankedStatus == BeatmapSetOnlineStatus.Qualified ||
                                    x.RankedStatus == BeatmapSetOnlineStatus.WIP
                                ) &&
                                (x.LastChecked.Value + TimeSpan.FromDays(30)).Subtract(DateTime.Now).TotalMilliseconds < 0
                            ) ||
                            (
                                (
                                    x.RankedStatus == BeatmapSetOnlineStatus.Graveyard ||
                                    x.RankedStatus == BeatmapSetOnlineStatus.Ranked ||
                                    x.RankedStatus == BeatmapSetOnlineStatus.Approved
                                ) &&
                                (x.LastChecked.Value + TimeSpan.FromDays(365)).Subtract(DateTime.Now).TotalMilliseconds < 0
                            )
                            )

                        && !x.Disabled
                        );

                    foreach (var beatmap in beatmaps)
                    {
                        if (Tasks.Count > 128)
                        {
                            Task.WaitAll(Tasks.ToArray(), CancellationToken); // wait for all tasks
                            Tasks.Clear();                                    // Remove all previous tasks.
                        }

                        _requestLimiter.Limit();

                        Tasks.Add(Crawl(beatmap.SetId));
                    }
                }
                catch (Exception e)
                {
                    Logger.Error(e, "an Unknown error occured during HouseKeeping", LoggingTarget.Database);
                    SentrySdk.CaptureException(e);
                } finally {
                    _dbContextPool.Return(dbContext);
                }

                Thread.Sleep(TimeSpan.FromHours(8)); // Update every 8 hours...
            }
        }
Exemple #8
0
        private static void Main(string[] args)
        {
            var startup = new Startup();
            DbContextPool <PisstaubeDbContext>?dbContextPool = null;

            var builder = new ContainerBuilder();

            startup.ConfigureServices(builder);

            builder.Register(c =>
            {
                var pool = c.Resolve <DbContextPool <PisstaubeDbContext> >();

                var context = pool.Rent();

                return(context);
            }).As <PisstaubeDbContext>()
            .OnRelease(c =>
            {
                dbContextPool?.Return(c);
            });

            builder.Register(c => {
                var contextPool = c.Resolve <DbContextPool <PisstaubeDbContext> >();
                dbContextPool   = contextPool;


                var apiProvider           = c.Resolve <IAPIProvider>();
                var cacheDbContextFactory = c.Resolve <PisstaubeCacheDbContextFactory>();
                var searchEngine          = c.Resolve <IBeatmapSearchEngineProvider>();
                var dbContext             = c.Resolve <PisstaubeDbContext>();

                startup.Configure(apiProvider, cacheDbContextFactory, searchEngine, dbContext);

                return(startup);
            }).AutoActivate();

            var container = builder.Build();

            var osuCrawler    = container.Resolve <OsuCrawler>();
            var houseKeeper   = container.Resolve <DatabaseHouseKeeper>();
            var redis         = container.Resolve <ConnectionMultiplexer>();
            var setDownloader = container.Resolve <SetDownloader>();

            if (Environment.GetEnvironmentVariable("CRAWLER_DISABLED")?.ToLower() != "true")
            {
                osuCrawler.Start();
            }

            if (Environment.GetEnvironmentVariable("UPDATER_DISABLED")?.ToLower() != "true")
            {
                houseKeeper.Start();
            }

            var db  = redis.GetDatabase(int.Parse(Environment.GetEnvironmentVariable("REDIS_DATABASE") ?? "-1"));
            var sub = redis.GetSubscriber();

            sub.Subscribe($"chimu:downloads", (channel, value) =>
            {
                Logger.LogPrint("Download was requested...");

                Logger.LogPrint(channel);
                Logger.LogPrint(value);

                DownloadMapRequest request = null;
                try {
                    request = JsonUtil.Deserialize <DownloadMapRequest>(value);
                } catch (Exception e) {
                    Logger.Error(e, "Failed to download Beatmap");
                }

                try
                {
                    var response = setDownloader.DownloadMap(int.Parse(request.SetId), request.NoVideo == "0");

                    response._ID = request._ID;

                    db.Publish("chimu:s:downloads", JsonUtil.Serialize(response));

                    Logger.LogPrint("Success");
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, "Failed to download Beatmap");
                }
            });

            while (true)
            {
                Thread.Sleep(1000);
            }
        }