Example #1
0
 public BeatmapProcessor()
     : base(new QueueConfiguration {
     InputQueueName = "beatmap"
 })
 {
     calculator = new ServerDifficultyCalculator(new[] { 0, 1, 2, 3 });
 }
Example #2
0
        public void OnExecute(CommandLineApplication app, IConsole console)
        {
            reporter = new Reporter(console, LogFile)
            {
                IsQuiet   = Quiet,
                IsVerbose = Verbose
            };

            if (Concurrency < 1)
            {
                reporter.Error("Concurrency level must be above 1.");
                return;
            }

            threadBeatmapIds = new int[Concurrency];

            if (AppSettings.RUN_AS_SANDBOX_DOCKER)
            {
                reporter.Output("Waiting for database...");

                while (true)
                {
                    try
                    {
                        bool initialised = false;

                        using (var conn = Database.GetConnection())
                        {
                            if (conn.QuerySingle <int>("SELECT `count` FROM `osu_counts` WHERE `name` = 'docker_db_step'") >= 1)
                            {
                                initialised = true;
                            }
                        }

                        if (initialised)
                        {
                            break;
                        }
                    }
                    catch
                    {
                    }

                    Thread.Sleep(1000);
                }
            }

            var beatmaps = new ConcurrentQueue <int>(GetBeatmaps() ?? Enumerable.Empty <int>());

            totalBeatmaps = beatmaps.Count;

            var tasks = new Task[Concurrency];

            for (int i = 0; i < Concurrency; i++)
            {
                int tmp = i;

                tasks[i] = Task.Factory.StartNew(() =>
                {
                    var calc = new ServerDifficultyCalculator(Rulesets, Converts, DryRun);

                    while (beatmaps.TryDequeue(out int beatmapId))
                    {
                        threadBeatmapIds[tmp] = beatmapId;
                        reporter.Verbose($"Processing difficulty for beatmap {beatmapId}.");

                        try
                        {
                            var beatmap = BeatmapLoader.GetBeatmap(beatmapId, Verbose, ForceDownload, reporter);

                            // ensure the correct online id is set
                            beatmap.BeatmapInfo.OnlineID = beatmapId;

                            calc.ProcessBeatmap(beatmap);
                            reporter.Verbose($"Difficulty updated for beatmap {beatmapId}.");
                        }
                        catch (Exception e)
                        {
                            reporter.Error($"{beatmapId} failed with {e}");
                        }

                        Interlocked.Increment(ref processedBeatmaps);
                    }
                });
            }

            reporter.Output($"Processing {totalBeatmaps} beatmaps.");

            using (new Timer(_ => outputProgress(), null, 1000, 1000))
                using (new Timer(_ => outputHealth(), null, 5000, 5000))
                    Task.WaitAll(tasks);

            if (AppSettings.RUN_AS_SANDBOX_DOCKER)
            {
                using (var conn = Database.GetConnection())
                {
                    conn.Execute("INSERT INTO `osu_counts` (`name`, `count`) VALUES (@Name, @Count) ON DUPLICATE KEY UPDATE `count` = @Count", new
                    {
                        Name  = "docker_db_step",
                        Count = 2
                    });
                }
            }

            outputProgress();

            reporter.Output("Done.");
        }