Skip to content

Commit bfd6e03

Browse files
committed
Drop EFCore in prod since we're not using it for anything more than migrations
1 parent f45eb31 commit bfd6e03

File tree

3 files changed

+48
-55
lines changed

3 files changed

+48
-55
lines changed

src/CookieTracker.cs

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Linq;
77
using System.Threading;
88
using System.Threading.Tasks;
9-
using Microsoft.EntityFrameworkCore;
109
using Microsoft.Extensions.Configuration;
1110
using Microsoft.Extensions.Logging;
1211
using Npgsql;
@@ -18,52 +17,50 @@ namespace OoLunar.CookieClicker
1817
{
1918
public sealed class CookieTracker : IAsyncDisposable
2019
{
21-
private readonly Dictionary<Ulid, CachedCookie> UnbakedCookies = new();
22-
private readonly CookieDatabaseContext DatabaseContext;
20+
private readonly Dictionary<Ulid, CachedCookie> _unbakedCookies = new();
21+
private readonly NpgsqlConnection _databaseConnection;
2322
private readonly ILogger<CookieTracker> _logger;
24-
private readonly SemaphoreSlim Semaphore = new(1, 1);
25-
private readonly PeriodicTimer Timer;
26-
private readonly Task BakingTask;
27-
private readonly FrozenDictionary<DatabaseOperation, NpgsqlCommand> DatabaseCommands;
23+
private readonly SemaphoreSlim _semaphore = new(1, 1);
24+
private readonly PeriodicTimer _timer;
25+
private readonly Task _bakingTask;
26+
private readonly FrozenDictionary<DatabaseOperation, NpgsqlCommand> _databaseCommands;
2827

29-
public CookieTracker(CookieDatabaseContext databaseContext, IConfiguration configuration, ILogger<CookieTracker> logger)
28+
public CookieTracker(IConfiguration configuration, ILogger<CookieTracker> logger)
3029
{
31-
ArgumentNullException.ThrowIfNull(databaseContext, nameof(databaseContext));
3230
ArgumentNullException.ThrowIfNull(configuration, nameof(configuration));
3331
ArgumentNullException.ThrowIfNull(logger, nameof(logger));
3432

3533
_logger = logger;
36-
Timer = new PeriodicTimer(TimeSpan.FromSeconds(configuration.GetValue("CookieTracker:Period", 30)));
34+
_timer = new PeriodicTimer(TimeSpan.FromSeconds(configuration.GetValue("CookieTracker:Period", 30)));
3735

38-
DatabaseContext = databaseContext;
39-
NpgsqlConnection connection = (NpgsqlConnection)DatabaseContext.Database.GetDbConnection();
40-
connection.Open();
41-
DatabaseCommands = new Dictionary<DatabaseOperation, NpgsqlCommand>
36+
_databaseConnection = NpgsqlDataSource.Create(CookieDatabaseContext.GetConnectionString(configuration)).CreateConnection();
37+
_databaseConnection.Open();
38+
_databaseCommands = new Dictionary<DatabaseOperation, NpgsqlCommand>
4239
{
43-
[DatabaseOperation.Create] = GetInsertCommand(connection),
44-
[DatabaseOperation.Read] = GetSelectCommand(connection),
45-
[DatabaseOperation.Update] = GetUpdateCommand(connection),
46-
[DatabaseOperation.Delete] = GetDeleteCommand(connection)
40+
[DatabaseOperation.Create] = GetInsertCommand(_databaseConnection),
41+
[DatabaseOperation.Read] = GetSelectCommand(_databaseConnection),
42+
[DatabaseOperation.Update] = GetUpdateCommand(_databaseConnection),
43+
[DatabaseOperation.Delete] = GetDeleteCommand(_databaseConnection)
4744
}.ToFrozenDictionary();
4845

49-
BakingTask = StartBakingAsync();
46+
_bakingTask = StartBakingAsync();
5047
}
5148

5249
public void CreateCookie(Cookie cookie)
5350
{
5451
ArgumentNullException.ThrowIfNull(cookie, nameof(cookie));
55-
UnbakedCookies.Add(cookie.Id, new(cookie, false));
52+
_unbakedCookies.Add(cookie.Id, new(cookie, false));
5653
}
5754

5855
public ulong Click(Ulid cookieId)
5956
{
6057
// Check if the cookie is in the cache. If it isn't, pull it from the database.
61-
if (!UnbakedCookies.TryGetValue(cookieId, out CachedCookie? unbakedCookie))
58+
if (!_unbakedCookies.TryGetValue(cookieId, out CachedCookie? unbakedCookie))
6259
{
63-
Semaphore.Wait();
60+
_semaphore.Wait();
6461
try
6562
{
66-
DbCommand command = DatabaseCommands[DatabaseOperation.Read];
63+
DbCommand command = _databaseCommands[DatabaseOperation.Read];
6764
command.Parameters[0].Value = cookieId.ToGuid();
6865
using DbDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow);
6966
if (!reader.Read())
@@ -76,11 +73,11 @@ public ulong Click(Ulid cookieId)
7673
Id = new Ulid(reader.GetFieldValue<Guid>(0)),
7774
Clicks = (ulong)reader.GetFieldValue<decimal>(1)
7875
}, true);
79-
UnbakedCookies.Add(cookieId, unbakedCookie);
76+
_unbakedCookies.Add(cookieId, unbakedCookie);
8077
}
8178
finally
8279
{
83-
Semaphore.Release();
80+
_semaphore.Release();
8481
}
8582
}
8683

@@ -90,18 +87,18 @@ public ulong Click(Ulid cookieId)
9087

9188
private async Task StartBakingAsync()
9289
{
93-
DbCommand createCommand = DatabaseCommands[DatabaseOperation.Create];
94-
DbCommand updateCommand = DatabaseCommands[DatabaseOperation.Update];
95-
foreach (DbCommand command in DatabaseCommands.Values)
90+
DbCommand createCommand = _databaseCommands[DatabaseOperation.Create];
91+
DbCommand updateCommand = _databaseCommands[DatabaseOperation.Update];
92+
foreach (DbCommand command in _databaseCommands.Values)
9693
{
9794
await command.PrepareAsync();
9895
}
9996

100-
while (await Timer.WaitForNextTickAsync())
97+
while (await _timer.WaitForNextTickAsync())
10198
{
102-
await Semaphore.WaitAsync();
103-
CachedCookie[] unbakedCookies = UnbakedCookies.Values.ToArray();
104-
Semaphore.Release();
99+
await _semaphore.WaitAsync();
100+
CachedCookie[] unbakedCookies = _unbakedCookies.Values.ToArray();
101+
_semaphore.Release();
105102

106103
List<Guid> updatedCookieIds = new();
107104
List<decimal> updatedCookieCount = new();
@@ -120,7 +117,7 @@ private async Task StartBakingAsync()
120117
}
121118

122119
// Remove the cookie from the unbaked cookies dictionary. Prefer the returned result over the current result.
123-
if (!UnbakedCookies.Remove(cookie.Cookie.Id, out CachedCookie? unbakedCookie))
120+
if (!_unbakedCookies.Remove(cookie.Cookie.Id, out CachedCookie? unbakedCookie))
124121
{
125122
unbakedCookie = cookie;
126123
}
@@ -143,7 +140,7 @@ private async Task StartBakingAsync()
143140
continue;
144141
}
145142

146-
await Semaphore.WaitAsync();
143+
await _semaphore.WaitAsync();
147144
try
148145
{
149146
if (newCookieIds.Count != 0)
@@ -162,17 +159,17 @@ private async Task StartBakingAsync()
162159
}
163160
finally
164161
{
165-
Semaphore.Release();
162+
_semaphore.Release();
166163
}
167164
}
168165
}
169166

170167
public async ValueTask DisposeAsync()
171168
{
172-
Timer.Dispose();
173-
await BakingTask;
174-
await DatabaseContext.DisposeAsync();
175-
Semaphore.Dispose();
169+
_timer.Dispose();
170+
await _bakingTask;
171+
await _databaseConnection.DisposeAsync();
172+
_semaphore.Dispose();
176173
}
177174

178175
private static NpgsqlCommand GetSelectCommand(NpgsqlConnection connection)

src/Database/CookieDatabaseContext.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,18 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) => modelBuild
3535
.ValueGeneratedOnAdd();
3636
});
3737

38-
internal static void ConfigureOptions(DbContextOptionsBuilder optionsBuilder, IConfiguration configuration)
38+
internal static NpgsqlConnectionStringBuilder GetConnectionString(IConfiguration configuration) => new()
3939
{
40-
NpgsqlConnectionStringBuilder connectionBuilder = new()
41-
{
42-
ApplicationName = configuration.GetValue("Database:ApplicationName", "Cookie Clicker"),
43-
Database = configuration.GetValue("Database:DatabaseName", "cookie_clicker"),
44-
Host = configuration.GetValue("Database:Host", "localhost"),
45-
Username = configuration.GetValue("Database:Username", "cookie_clicker"),
46-
Port = configuration.GetValue("Database:Port", 5432),
47-
Password = configuration.GetValue<string>("Database:Password")
48-
};
40+
ApplicationName = configuration.GetValue("Database:ApplicationName", "Cookie Clicker"),
41+
Database = configuration.GetValue("Database:DatabaseName", "cookie_clicker"),
42+
Host = configuration.GetValue("Database:Host", "localhost"),
43+
Username = configuration.GetValue("Database:Username", "cookie_clicker"),
44+
Port = configuration.GetValue("Database:Port", 5432),
45+
Password = configuration.GetValue<string>("Database:Password")
46+
};
4947

50-
optionsBuilder.UseNpgsql(connectionBuilder.ToString(), options => options.EnableRetryOnFailure(5).CommandTimeout(5))
51-
.UseSnakeCaseNamingConvention()
52-
.EnableThreadSafetyChecks(false);
53-
}
48+
internal static void ConfigureOptions(DbContextOptionsBuilder optionsBuilder, IConfiguration configuration) => optionsBuilder.UseNpgsql(GetConnectionString(configuration).ToString(), options => options.EnableRetryOnFailure(5).CommandTimeout(5))
49+
.UseSnakeCaseNamingConvention()
50+
.EnableThreadSafetyChecks(false);
5451
}
5552
}

src/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using Microsoft.Extensions.Configuration;
1818
using Microsoft.Extensions.DependencyInjection;
1919
using Microsoft.Extensions.Logging;
20-
using OoLunar.CookieClicker.Database;
2120
using OoLunar.CookieClicker.GenHttp;
2221
using OoLunar.CookieClicker.Headers;
2322
using OoLunar.CookieClicker.Routes;
@@ -30,14 +29,14 @@ public static async Task<int> Main(string[] args)
3029
{
3130
ServiceCollection serviceCollection = new();
3231
ConfigurationBuilder configurationBuilder = new();
32+
configurationBuilder.Sources.Clear();
3333
configurationBuilder.AddJsonFile("config.json", true, true);
3434
configurationBuilder.AddEnvironmentVariables("CookieClicker_");
3535
configurationBuilder.AddCommandLine(args);
3636

3737
IConfiguration configuration = configurationBuilder.Build();
3838
serviceCollection.AddSingleton(configuration);
3939
serviceCollection.AddLogging(loggingBuilder => HttpLogger.ConfigureLogging(loggingBuilder, configuration));
40-
serviceCollection.AddDbContext<CookieDatabaseContext>((services, options) => CookieDatabaseContext.ConfigureOptions(options, services.GetRequiredService<IConfiguration>()), ServiceLifetime.Scoped);
4140
serviceCollection.AddSingleton<CookieTracker>();
4241
serviceCollection.AddSingleton<DiscordHeaderAuthentication>();
4342
serviceCollection.AddSingleton<DiscordSlashCommandHandler>();

0 commit comments

Comments
 (0)