WIP: BROKEN FOR ELECTRON: Fixes in progress

This commit is contained in:
Matthew Raymer
2025-05-28 13:09:51 +00:00
parent 29908b77e3
commit 229d9184b2
20 changed files with 671 additions and 615 deletions

View File

@@ -123,35 +123,77 @@ export async function retrieveSettingsForActiveAccount(): Promise<Settings> {
}
}
/**
* Logs a message to the database with proper handling of concurrent writes
* @param message - The message to log
* @author Matthew Raymer
*/
export async function logToDb(message: string): Promise<void> {
const platform = PlatformServiceFactory.getInstance();
const todayKey = new Date().toDateString();
const fullMessage = `${new Date().toISOString()} ${message}`;
// Check if we have any logs for today
const result = await platform.dbQuery(
"SELECT message FROM logs WHERE date = ?",
[todayKey],
);
try {
// Try to insert first, if it fails due to UNIQUE constraint, update instead
try {
await platform.dbExec("INSERT INTO logs (date, message) VALUES (?, ?)", [
todayKey,
fullMessage,
]);
} catch (error) {
// If insert fails due to UNIQUE constraint, update instead
if (
error instanceof Error &&
error.message.includes("UNIQUE constraint failed")
) {
const result = await platform.dbQuery(
"SELECT message FROM logs WHERE date = ?",
[todayKey],
);
if (!result || result.values.length === 0) {
// If no logs for today, clear all previous logs
await platform.dbExec("DELETE FROM logs");
if (result && result.values.length > 0) {
const prevMessages = result.values[0][0] as string;
const updatedMessage = `${prevMessages}\n${fullMessage}`;
// Insert new log
const fullMessage = `${new Date().toISOString()} ${message}`;
await platform.dbExec("INSERT INTO logs (date, message) VALUES (?, ?)", [
todayKey,
fullMessage,
]);
} else {
// Append to existing log
const prevMessages = result.values[0][0] as string;
const fullMessage = `${prevMessages}\n${new Date().toISOString()} ${message}`;
await platform.dbExec("UPDATE logs SET message = ? WHERE date = ?", [
updatedMessage,
todayKey,
]);
}
} else {
// If it's a different error, rethrow it
throw error;
}
}
await platform.dbExec("UPDATE logs SET message = ? WHERE date = ?", [
fullMessage,
todayKey,
]);
// Clean up old logs (keep only last 7 days) - do this less frequently
// Only clean up if the date is different from the last cleanup
const lastCleanupKey = "last_log_cleanup";
const result = await platform.dbQuery(
"SELECT value FROM settings WHERE key = ?",
[lastCleanupKey],
);
const lastCleanup = result?.values[0]?.[0] as string;
if (!lastCleanup || lastCleanup !== todayKey) {
const sevenDaysAgo = new Date();
sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
await platform.dbExec("DELETE FROM logs WHERE date < ?", [
sevenDaysAgo.toDateString(),
]);
// Update last cleanup date
await platform.dbExec(
"INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)",
[lastCleanupKey, todayKey],
);
}
} catch (error) {
// Log to console as fallback
// eslint-disable-next-line no-console
console.error("Failed to log to database:", error);
// eslint-disable-next-line no-console
console.error("Original message:", message);
}
}