aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Messaging/ConversationService.cs21
-rw-r--r--src/slskd/Program.cs11
-rw-r--r--src/slskd/Shares/IShareRepository.cs5
-rw-r--r--src/slskd/Shares/ShareScanner.cs4
-rw-r--r--src/slskd/Shares/SqliteShareRepository.cs54
-rw-r--r--src/web/src/components/AppFooter.jsx3
6 files changed, 80 insertions, 18 deletions
diff --git a/src/slskd/Messaging/ConversationService.cs b/src/slskd/Messaging/ConversationService.cs
index a744dcca..9873b88b 100644
--- a/src/slskd/Messaging/ConversationService.cs
+++ b/src/slskd/Messaging/ConversationService.cs
@@ -398,20 +398,23 @@ namespace slskd.Messaging
private void ActivateConversation(string username)
{
- using var context = ContextFactory.CreateDbContext();
+ try
+ {
+ using var context = ContextFactory.CreateDbContext();
- var conversation = context.Conversations.FirstOrDefault(c => c.Username == username);
+ context.Database.ExecuteSql($@"
+ INSERT INTO Conversations (Username, IsActive)
+ VALUES ({username}, 1)
+ ON CONFLICT(Username) DO UPDATE SET IsActive = excluded.IsActive;
+ ");
- if (conversation != default)
- {
- conversation.IsActive = true;
+ Log.Debug("Successfully activated Conversation for {Username}", username);
}
- else
+ catch (Exception ex)
{
- context.Conversations.Add(new Conversation { Username = username, IsActive = true });
+ Log.Warning(ex, "Failed to activate Conversation for {Username}: {Message}", username, ex.Message);
+ throw;
}
-
- context.SaveChanges();
}
}
}
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs
index c998d319..b1f57ce4 100644
--- a/src/slskd/Program.cs
+++ b/src/slskd/Program.cs
@@ -1067,12 +1067,17 @@ namespace slskd
var urlBase = OptionsAtStartup.Web.UrlBase;
urlBase = urlBase.StartsWith("/") ? urlBase : "/" + urlBase;
- // use urlBase. this effectively just removes urlBase from the path.
- // inject urlBase into any html files we serve, and rewrite links to ./static or /static to
- // prepend the url base.
+ // use urlBase. this effectively just removes urlBase from the path before sending it to the next middleware
app.UsePathBase(urlBase);
+
+ // rewrite relative html links to ./static or ./favicon.ico to absolute links that include urlBase
+ // this is necessary because SPA routers (like React Router) mess with the browser's current address
app.UseHTMLRewrite("((\\.)?\\/static)", $"{(urlBase == "/" ? string.Empty : urlBase)}/static");
+ app.UseHTMLRewrite("((\\.)?\\/favicon\\.ico)", $"{(urlBase == "/" ? string.Empty : urlBase)}/favicon.ico");
+
+ // inject urlBase and port into any html files we serve as a window-scoped javascript variable
app.UseHTMLInjection($"<script>window.urlBase=\"{urlBase}\";window.port={OptionsAtStartup.Web.Port}</script>", excludedRoutes: new[] { "/api", "/swagger" });
+
Log.Information("Using base url {UrlBase}", urlBase);
// serve static content from the configured path
diff --git a/src/slskd/Shares/IShareRepository.cs b/src/slskd/Shares/IShareRepository.cs
index 3b57c16f..e9799663 100644
--- a/src/slskd/Shares/IShareRepository.cs
+++ b/src/slskd/Shares/IShareRepository.cs
@@ -53,6 +53,11 @@ namespace slskd.Shares
void BackupTo(IShareRepository repository);
/// <summary>
+ /// Checkpoints the current database, flushing the contents of the WAL (write ahead log) into the table(s).
+ /// </summary>
+ void Checkpoint();
+
+ /// <summary>
/// Counts the number of directories in the database.
/// </summary>
/// <param name="parentDirectory">The optional directory prefix used for counting subdirectories.</param>
diff --git a/src/slskd/Shares/ShareScanner.cs b/src/slskd/Shares/ShareScanner.cs
index 84ea80e3..98a3f092 100644
--- a/src/slskd/Shares/ShareScanner.cs
+++ b/src/slskd/Shares/ShareScanner.cs
@@ -403,6 +403,10 @@ namespace slskd.Shares
Log.Debug("Vacuuming...");
repository.Vacuum();
Log.Debug("Vacuumed successfully");
+
+ Log.Debug("Checkpointing...");
+ repository.Checkpoint();
+ Log.Debug("Checkpointed successfully");
}
catch (OperationCanceledException)
{
diff --git a/src/slskd/Shares/SqliteShareRepository.cs b/src/slskd/Shares/SqliteShareRepository.cs
index ed594209..6d84308a 100644
--- a/src/slskd/Shares/SqliteShareRepository.cs
+++ b/src/slskd/Shares/SqliteShareRepository.cs
@@ -84,14 +84,58 @@ namespace slskd.Shares
/// <param name="repository">The destination repository.</param>
public void BackupTo(IShareRepository repository)
{
- using var sourceConn = GetConnection(ConnectionString);
- using var backupConn = GetConnection(repository.ConnectionString);
- sourceConn.BackupDatabase(backupConn);
+ using (var sourceConn = GetConnection(ConnectionString))
+ using (var backupConn = GetConnection(repository.ConnectionString))
+ {
+ sourceConn.BackupDatabase(backupConn);
+ }
+
+ using var backupRepository = new SqliteShareRepository(repository.ConnectionString);
Log.Debug("Vacuuming backup");
- using var cmd = new SqliteCommand("VACUUM", backupConn);
- cmd.ExecuteNonQuery();
+ backupRepository.Vacuum();
Log.Debug("Backup vacuumed successfully");
+
+ Log.Debug("Checkpointing backup");
+ backupRepository.Checkpoint();
+ Log.Debug("Backup checkpointed successfully");
+ }
+
+ /// <summary>
+ /// Checkpoints the current database, flushing the contents of the WAL (write ahead log) into the table(s).
+ /// </summary>
+ public void Checkpoint()
+ {
+ const int maxAttempts = 3;
+
+ using var conn = GetConnection();
+
+ for (var attempt = 1; attempt <= maxAttempts; attempt++)
+ {
+ using var cmd = new SqliteCommand("PRAGMA wal_checkpoint(TRUNCATE);", conn);
+ using var reader = cmd.ExecuteReader();
+
+ reader.Read();
+
+ var busy = reader.GetInt32(0); // another operation prevented the checkpoint (true/false)
+ var log = reader.GetInt32(1); // number of frames in the WAL at the start
+ var checkpointed = reader.GetInt32(2); // number of frames moved from the WAL to the db
+
+ if (busy == 0 && checkpointed == log)
+ {
+ Log.Debug("WAL checkpoint complete; {Log} frames in log, {Checkpointed} checkpointed", log, checkpointed);
+ return;
+ }
+
+ Log.Warning("WAL checkpoint attempt {Attempt} of {MaxAttempts} did not fully complete; {Log} frames in log, {Checkpointed} checkpointed", attempt, maxAttempts, log, checkpointed);
+
+ if (attempt < maxAttempts)
+ {
+ System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
+ }
+ }
+
+ throw new TimeoutException($"Failed to fully checkpoint the shares database after {maxAttempts} attempts");
}
/// <summary>
diff --git a/src/web/src/components/AppFooter.jsx b/src/web/src/components/AppFooter.jsx
index 9a295eb5..b07af7a2 100644
--- a/src/web/src/components/AppFooter.jsx
+++ b/src/web/src/components/AppFooter.jsx
@@ -1,3 +1,4 @@
+import { urlBase } from '../config';
import { formatBytes } from '../lib/util';
import React from 'react';
import { Icon, Menu } from 'semantic-ui-react';
@@ -69,7 +70,7 @@ const AppFooter = ({
<img
alt=""
className="footer-logo"
- src="/favicon.ico"
+ src={`${urlBase}/favicon.ico`}
/>
slskd
{current && <span className="footer-version">{current}</span>}