Skip to content

Commit 147b801

Browse files
committed
Remove inheritance of ListIndicesCommand completely
It was ugly.
1 parent 00a5642 commit 147b801

File tree

6 files changed

+61
-48
lines changed

6 files changed

+61
-48
lines changed

osu.ElasticIndexer/Commands/Index/CloseIndexCommand.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66
using System.Threading;
77
using McMaster.Extensions.CommandLineUtils;
88
using osu.Server.QueueProcessor;
9+
using StackExchange.Redis;
910

1011
namespace osu.ElasticIndexer.Commands.Index
1112
{
1213
[Command("close", Description = "Closes unused indices.")]
13-
public class CloseIndexCommand : ListIndicesCommand
14+
public class CloseIndexCommand
1415
{
1516
[Argument(0, "name", "The index to close. All unused indices are closed if not specified.")]
1617
public string? Name { get; set; }
1718

18-
public override int OnExecute(CancellationToken token)
19-
{
20-
if (base.OnExecute(token) != 0)
21-
return -1;
19+
private readonly ConnectionMultiplexer redis = RedisAccess.GetConnection();
20+
private readonly OsuElasticClient elasticClient = new OsuElasticClient();
2221

23-
var indices = string.IsNullOrEmpty(Name) ? ElasticClient.GetIndices($"{AppSettings.AliasName}_*") : ElasticClient.GetIndex(Name);
22+
public int OnExecute(CancellationToken token)
23+
{
24+
var indices = string.IsNullOrEmpty(Name) ? elasticClient.GetIndices($"{AppSettings.AliasName}_*") : elasticClient.GetIndex(Name);
2425

2526
var closeableIndices = indices.Where(entry => entry.Value.Aliases.Count == 0);
2627

@@ -47,17 +48,17 @@ public override int OnExecute(CancellationToken token)
4748

4849
foreach (var entry in closeableIndices)
4950
{
50-
if (entry.Key.Name == Redis.GetCurrentSchema())
51+
if (entry.Key.Name == redis.GetCurrentSchema())
5152
{
5253
Console.WriteLine(ConsoleColor.Red, $"Index {entry.Key.Name} is set as current schema. Cannot close.");
5354
return -1;
5455
}
5556

5657
System.Console.WriteLine($"Removing {entry.Key.Name} from active schemas..");
57-
Redis.RemoveActiveSchema(entry.Key.Name);
58+
redis.RemoveActiveSchema(entry.Key.Name);
5859

5960
Console.WriteLine($"Closing {entry.Key.Name}...");
60-
var response = ElasticClient.Indices.Close(entry.Key.Name);
61+
var response = elasticClient.Indices.Close(entry.Key.Name);
6162

6263
if (!response.IsValid)
6364
{
@@ -68,7 +69,7 @@ public override int OnExecute(CancellationToken token)
6869

6970
Console.WriteLine("done.");
7071

71-
return 0;
72+
return ListIndicesCommand.ListSchemas(redis, elasticClient);
7273
}
7374
}
7475
}

osu.ElasticIndexer/Commands/Index/DeleteIndexCommand.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
using System.Linq;
66
using System.Threading;
77
using McMaster.Extensions.CommandLineUtils;
8+
using osu.Server.QueueProcessor;
9+
using StackExchange.Redis;
810

911
namespace osu.ElasticIndexer.Commands.Index
1012
{
1113
[Command("delete", Description = "Deletes closed indices.")]
12-
public class DeleteIndexCommand : ListIndicesCommand
14+
public class DeleteIndexCommand
1315
{
1416
[Argument(0, "name", "The index to delete. All closed indices are deleted if not specified.")]
1517
public string? Name { get; set; }
1618

19+
private readonly ConnectionMultiplexer redis = RedisAccess.GetConnection();
1720
private readonly OsuElasticClient elasticClient = new OsuElasticClient();
1821

19-
public override int OnExecute(CancellationToken token)
22+
public int OnExecute(CancellationToken token)
2023
{
21-
if (base.OnExecute(token) != 0)
22-
return -1;
23-
2424
string? index = string.IsNullOrEmpty(Name) ? $"{AppSettings.AliasName}_*" : Name;
2525
var response = elasticClient.Cat.Indices(x => x.Index(index));
2626
var closed = response.Records.Where(record => record.Status == "close");
@@ -56,7 +56,7 @@ public override int OnExecute(CancellationToken token)
5656

5757
Console.WriteLine("done.");
5858

59-
return 0;
59+
return ListIndicesCommand.ListSchemas(redis, elasticClient);
6060
}
6161
}
6262
}

osu.ElasticIndexer/Commands/Index/ListIndicesCommand.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212
namespace osu.ElasticIndexer.Commands.Index
1313
{
1414
[Command("list", Description = "Lists indices.")]
15-
public class ListIndicesCommand
15+
public sealed class ListIndicesCommand
1616
{
17-
protected readonly OsuElasticClient ElasticClient = new OsuElasticClient();
17+
private readonly OsuElasticClient elasticClient = new OsuElasticClient();
18+
private readonly ConnectionMultiplexer redis = RedisAccess.GetConnection();
1819

19-
protected readonly ConnectionMultiplexer Redis = RedisAccess.GetConnection();
20+
public int OnExecute(CancellationToken token)
21+
{
22+
return ListSchemas(redis, elasticClient);
23+
}
2024

21-
public virtual int OnExecute(CancellationToken token)
25+
public static int ListSchemas(ConnectionMultiplexer redis, OsuElasticClient elastic)
2226
{
23-
string?[] activeSchemas = Redis.GetActiveSchemas();
24-
string currentSchema = Redis.GetCurrentSchema();
27+
string?[] activeSchemas = redis.GetActiveSchemas();
28+
string currentSchema = redis.GetCurrentSchema();
2529

2630
Console.WriteLine("# Redis tracking");
2731
Console.WriteLine();
@@ -30,8 +34,8 @@ public virtual int OnExecute(CancellationToken token)
3034
Console.WriteLine($"Current schema: {currentSchema}");
3135
Console.WriteLine();
3236

33-
var indices = ElasticClient.GetIndices($"{AppSettings.AliasName}_*");
34-
var records = ElasticClient.Cat.Indices(descriptor => descriptor.Index(Indices.All)).Records;
37+
var indices = elastic.GetIndices($"{AppSettings.AliasName}_*");
38+
var records = elastic.Cat.Indices(descriptor => descriptor.Index(Indices.All)).Records;
3539

3640
Console.WriteLine($"# Elasticsearch indices ({indices.Count})");
3741
Console.WriteLine();

osu.ElasticIndexer/Commands/Index/NukeAllIndicesCommand.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@
44
using System.Threading;
55
using McMaster.Extensions.CommandLineUtils;
66
using osu.Server.QueueProcessor;
7+
using StackExchange.Redis;
78

89
namespace osu.ElasticIndexer.Commands.Index;
910

1011
[Command("nuke", Description = "Resets everything with fire.")]
11-
public class NukeAllIndicesCommand : ListIndicesCommand
12+
public class NukeAllIndicesCommand
1213
{
13-
public override int OnExecute(CancellationToken token)
14+
private readonly ConnectionMultiplexer redis = RedisAccess.GetConnection();
15+
private readonly OsuElasticClient elasticClient = new OsuElasticClient();
16+
17+
public int OnExecute(CancellationToken token)
1418
{
1519
string prefix = $"{AppSettings.AliasName}_*";
1620

1721
// Don't exit on error as we're likely trying to fix an error.
18-
base.OnExecute(token);
22+
if (ListIndicesCommand.ListSchemas(redis, elasticClient) != 0)
23+
return -1;
1924

2025
Console.WriteLine();
2126
Console.WriteLine($"PREPARING TO NUKE ALL ELASTICSEARCH INDICES WITH PREFIX {prefix} WITH FIRE!");
@@ -26,24 +31,24 @@ public override int OnExecute(CancellationToken token)
2631
return -1;
2732

2833
System.Console.WriteLine("Unsetting current schema..");
29-
Redis.ClearCurrentSchema();
34+
redis.ClearCurrentSchema();
3035

3136
System.Console.WriteLine("Removing all active schemas..");
3237

33-
foreach (string? schema in Redis.GetActiveSchemas())
38+
foreach (string? schema in redis.GetActiveSchemas())
3439
{
3540
if (schema != null)
36-
Redis.RemoveActiveSchema(schema);
41+
redis.RemoveActiveSchema(schema);
3742
}
3843

3944
System.Console.WriteLine("Removing all indices..");
4045

41-
var indices = ElasticClient.GetIndices(prefix);
46+
var indices = elasticClient.GetIndices(prefix);
4247

4348
foreach (var index in indices)
4449
{
4550
System.Console.WriteLine($"Deleting {index.Key}..");
46-
ElasticClient.Indices.Delete(index.Key);
51+
elasticClient.Indices.Delete(index.Key);
4752
}
4853

4954
return 0;

osu.ElasticIndexer/Commands/Index/OpenIndexCommand.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,24 @@
55
using System.Threading;
66
using McMaster.Extensions.CommandLineUtils;
77
using osu.Server.QueueProcessor;
8+
using StackExchange.Redis;
89

910
namespace osu.ElasticIndexer.Commands.Index
1011
{
1112
[Command("open", Description = "Opens an index.")]
12-
public class OpenIndexCommand : ListIndicesCommand
13+
public class OpenIndexCommand
1314
{
1415
[Argument(0, "name", "The index to open.")]
1516
[Required]
1617
public string Name { get; } = string.Empty;
1718

18-
public override int OnExecute(CancellationToken token)
19-
{
20-
if (base.OnExecute(token) != 0)
21-
return -1;
19+
private readonly ConnectionMultiplexer redis = RedisAccess.GetConnection();
20+
private readonly OsuElasticClient elasticClient = new OsuElasticClient();
2221

22+
public int OnExecute(CancellationToken token)
23+
{
2324
Console.WriteLine($"Opening {Name}..");
24-
var response = new OsuElasticClient().Indices.Open(Name);
25+
var response = elasticClient.Indices.Open(Name);
2526

2627
if (!response.IsValid)
2728
{
@@ -31,7 +32,8 @@ public override int OnExecute(CancellationToken token)
3132

3233
Console.WriteLine($"Adding {Name} to active schemas..");
3334
RedisAccess.GetConnection().AddActiveSchema(Name);
34-
return 0;
35+
36+
return ListIndicesCommand.ListSchemas(redis, elasticClient);
3537
}
3638
}
3739
}

osu.ElasticIndexer/Commands/Index/UpdateAliasCommand.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
using System.Threading;
66
using McMaster.Extensions.CommandLineUtils;
77
using osu.Server.QueueProcessor;
8+
using StackExchange.Redis;
89

910
namespace osu.ElasticIndexer.Commands.Index
1011
{
1112
[Command("alias", Description = "Updates alias to the latest index of a given version.")]
12-
public class UpdateAliasCommand : ListIndicesCommand
13+
public class UpdateAliasCommand
1314
{
1415
[Option("--close", Description = "Closes the previously aliased index when switching.")]
1516
public bool Close { get; set; }
@@ -18,15 +19,18 @@ public class UpdateAliasCommand : ListIndicesCommand
1819
[Required]
1920
public string Schema { get; set; } = string.Empty;
2021

21-
public override int OnExecute(CancellationToken token)
22+
private readonly ConnectionMultiplexer redis = RedisAccess.GetConnection();
23+
private readonly OsuElasticClient elasticClient = new OsuElasticClient();
24+
25+
public int OnExecute(CancellationToken token)
2226
{
2327
if (string.IsNullOrWhiteSpace(Schema))
2428
{
2529
Console.WriteLine("A schema version is required.");
2630
return 1;
2731
}
2832

29-
var index = ElasticClient.GetIndexForSchema(Schema);
33+
var index = elasticClient.GetIndexForSchema(Schema);
3034

3135
if (index == null)
3236
{
@@ -37,14 +41,11 @@ public override int OnExecute(CancellationToken token)
3741
// TODO: should check if completed?
3842
string? indexName = index.Value.Key.Name;
3943

40-
ElasticClient.UpdateAlias(AppSettings.AliasName, indexName, Close);
41-
42-
Redis.SetCurrentSchema(indexName);
44+
elasticClient.UpdateAlias(AppSettings.AliasName, indexName, Close);
4345

44-
if (base.OnExecute(token) != 0)
45-
return -1;
46+
redis.SetCurrentSchema(indexName);
4647

47-
return 0;
48+
return ListIndicesCommand.ListSchemas(redis, elasticClient);
4849
}
4950
}
5051
}

0 commit comments

Comments
 (0)