Skip to content

Commit

Permalink
Merge pull request #169 from peppy/better-disable-processors
Browse files Browse the repository at this point in the history
  • Loading branch information
smoogipoo authored Nov 14, 2023
2 parents 2fae652 + e63565b commit 87a0efe
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Threading;
using System.Threading.Tasks;
using McMaster.Extensions.CommandLineUtils;
Expand All @@ -10,11 +11,18 @@ namespace osu.Server.Queues.ScoreStatisticsProcessor.Commands.Queue
[Command("watch", Description = "Begins processing of the queue.")]
public class WatchQueueCommand
{
private readonly ScoreStatisticsQueueProcessor queue = new ScoreStatisticsQueueProcessor();
/// <summary>
/// A comma-separated list of processors to disable.
/// </summary>
[Option("--disable", Description = "A comma-separated list of processors to disable.")]
public string DisabledProcessors { get; set; } = string.Empty;

public Task<int> OnExecuteAsync(CancellationToken cancellationToken)
{
ScoreStatisticsQueueProcessor queue = new ScoreStatisticsQueueProcessor(DisabledProcessors.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries));

queue.Run(cancellationToken);

return Task.FromResult(0);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ namespace osu.Server.Queues.ScoreStatisticsProcessor.Processors
[UsedImplicitly]
public class MedalProcessor : IProcessor
{
private static readonly bool process_user_medals = Environment.GetEnvironmentVariable("PROCESS_USER_MEDALS") != "0";

private static readonly List<IMedalAwarder> medal_awarders = new List<IMedalAwarder>();

private ImmutableArray<Medal>? availableMedals;
Expand Down Expand Up @@ -49,9 +47,6 @@ public void RevertFromUserStats(SoloScoreInfo score, UserStats userStats, int pr

public void ApplyToUserStats(SoloScoreInfo score, UserStats userStats, MySqlConnection conn, MySqlTransaction transaction)
{
if (!process_user_medals)
return;

int[] alreadyAchieved = conn.Query<int>("SELECT achievement_id FROM osu_user_achievements WHERE user_id = @userId", new
{
userId = score.UserID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public class UserTotalPerformanceProcessor : IProcessor

private long lastStoreRefresh;

private static readonly bool process_user_totals = Environment.GetEnvironmentVariable("PROCESS_USER_TOTALS") != "0";

// This processor needs to run after the score's PP value has been processed.
public int Order => ScorePerformanceProcessor.ORDER + 1;

Expand All @@ -38,9 +36,6 @@ public void RevertFromUserStats(SoloScoreInfo score, UserStats userStats, int pr

public void ApplyToUserStats(SoloScoreInfo score, UserStats userStats, MySqlConnection conn, MySqlTransaction transaction)
{
if (!process_user_totals)
return;

var dbInfo = LegacyDatabaseHelper.GetRulesetSpecifics(score.RulesetID);

int warnings = conn.QuerySingleOrDefault<int>($"SELECT `user_warnings` FROM {dbInfo.UsersTable} WHERE `user_id` = @UserId", new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Dapper;
using Dapper.Contrib.Extensions;
using MySqlConnector;
using osu.Framework.Extensions.TypeExtensions;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Rulesets;
using osu.Server.QueueProcessor;
Expand Down Expand Up @@ -40,13 +41,14 @@ public class ScoreStatisticsQueueProcessor : QueueProcessor<ScoreItem>

private readonly ElasticQueueProcessor elasticQueueProcessor = new ElasticQueueProcessor();

public ScoreStatisticsQueueProcessor()
public ScoreStatisticsQueueProcessor(string[]? disabledProcessors = null)
: base(new QueueConfiguration { InputQueueName = "score-statistics" })
{
DapperExtensions.InstallDateTimeOffsetMapper();

// add each processor automagically.
foreach (var t in typeof(ScoreStatisticsQueueProcessor).Assembly.GetTypes().Where(t => !t.IsInterface && typeof(IProcessor).IsAssignableFrom(t)))
List<Type> enabledTypes = createProcessors(disabledProcessors);

foreach (var t in enabledTypes)
{
if (Activator.CreateInstance(t) is IProcessor processor)
processors.Add(processor);
Expand All @@ -55,6 +57,45 @@ public ScoreStatisticsQueueProcessor()
processors = processors.OrderBy(p => p.Order).ToList();
}

private List<Type> createProcessors(string[]? disabledProcessors)
{
List<Type> enabledTypes = typeof(ScoreStatisticsQueueProcessor)
.Assembly
.GetTypes()
.Where(t => !t.IsInterface && typeof(IProcessor).IsAssignableFrom(t))
.ToList();

List<Type> disabledTypes = new List<Type>();

if (disabledProcessors?.Length > 0)
{
foreach (string s in disabledProcessors)
{
var match = enabledTypes.FirstOrDefault(t => t.ReadableName() == s);

if (match == null)
throw new ArgumentException($"Could not find matching processor to disable (\"{s}\")");

enabledTypes.Remove(match);
disabledTypes.Add(match);
}
}

Console.WriteLine("Active processors:");
foreach (var type in enabledTypes)
Console.WriteLine($"- {type.ReadableName()}");

Console.WriteLine();

Console.WriteLine("Disabled processors:");
foreach (var type in disabledTypes)
Console.WriteLine($"- {type.ReadableName()}");

Console.WriteLine();

return enabledTypes;
}

protected override void ProcessResult(ScoreItem item)
{
if (item.Score.ScoreInfo.LegacyScoreId != null)
Expand Down

0 comments on commit 87a0efe

Please sign in to comment.