Spaces:
Sleeping
Sleeping
Commit ·
f00923d
1
Parent(s): 977fb40
fix: restore database filename and add emergency cleanup logic to resolve migration hangs
Browse files- FlowAPI/Program.cs +28 -20
FlowAPI/Program.cs
CHANGED
|
@@ -114,36 +114,44 @@ namespace FlowAPI
|
|
| 114 |
{
|
| 115 |
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
| 116 |
|
| 117 |
-
// Ensure the database directory exists
|
| 118 |
var connStr = builder.Configuration.GetConnectionString("DefaultConnection");
|
| 119 |
-
|
|
|
|
|
|
|
| 120 |
{
|
| 121 |
-
|
| 122 |
-
|
| 123 |
{
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
{
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
{
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
| 133 |
}
|
| 134 |
}
|
| 135 |
}
|
| 136 |
-
|
| 137 |
-
db.Database.Migrate();
|
| 138 |
-
|
| 139 |
-
// Set journal mode to TRUNCATE for stability on Docker/Cloud filesystems
|
| 140 |
-
using (var command = db.Database.GetDbConnection().CreateCommand())
|
| 141 |
{
|
| 142 |
-
|
| 143 |
-
command.CommandText = "PRAGMA journal_mode=TRUNCATE; PRAGMA synchronous=NORMAL; PRAGMA locking_mode=NORMAL; PRAGMA temp_store=MEMORY;";
|
| 144 |
-
command.ExecuteNonQuery();
|
| 145 |
}
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
// Auto-seed achievements
|
| 148 |
var achievementService = scope.ServiceProvider.GetRequiredService<IAchievementService>();
|
| 149 |
achievementService.SeedAchievementsAsync().Wait();
|
|
|
|
| 114 |
{
|
| 115 |
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
| 116 |
|
| 117 |
+
// Ensure the database directory exists
|
| 118 |
var connStr = builder.Configuration.GetConnectionString("DefaultConnection");
|
| 119 |
+
|
| 120 |
+
// Emergency Cleanup & Optimization before Migration
|
| 121 |
+
try
|
| 122 |
{
|
| 123 |
+
db.Database.OpenConnection();
|
| 124 |
+
using (var command = db.Database.GetDbConnection().CreateCommand())
|
| 125 |
{
|
| 126 |
+
// 1. Basic settings for stability
|
| 127 |
+
command.CommandText = "PRAGMA journal_mode=TRUNCATE; PRAGMA synchronous=NORMAL; PRAGMA busy_timeout=10000;";
|
| 128 |
+
command.ExecuteNonQuery();
|
| 129 |
+
|
| 130 |
+
// 2. DELETE OVERSIZED RECORDS (Possible cause of I/O hang)
|
| 131 |
+
command.CommandText = "SELECT name FROM sqlite_master WHERE type='table' AND name='TaskNodes';";
|
| 132 |
+
var tableExists = command.ExecuteScalar();
|
| 133 |
+
if (tableExists != null)
|
| 134 |
{
|
| 135 |
+
// Delete any nodes where Data (image) is larger than 2MB to unblock the DB
|
| 136 |
+
command.CommandText = "DELETE FROM TaskNodes WHERE length(Data) > 2000000;";
|
| 137 |
+
int deleted = command.ExecuteNonQuery();
|
| 138 |
+
if (deleted > 0) Console.WriteLine($"[Emergency] Deleted {deleted} oversized nodes to restore stability.");
|
| 139 |
+
|
| 140 |
+
// Compact the database
|
| 141 |
+
command.CommandText = "VACUUM;";
|
| 142 |
+
command.ExecuteNonQuery();
|
| 143 |
}
|
| 144 |
}
|
| 145 |
}
|
| 146 |
+
catch (Exception ex)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
{
|
| 148 |
+
Console.WriteLine($"[Warning] Emergency cleanup failed: {ex.Message}");
|
|
|
|
|
|
|
| 149 |
}
|
| 150 |
|
| 151 |
+
// Run Migrations with a reasonable timeout
|
| 152 |
+
db.Database.SetCommandTimeout(120); // 2 minutes
|
| 153 |
+
db.Database.Migrate();
|
| 154 |
+
|
| 155 |
// Auto-seed achievements
|
| 156 |
var achievementService = scope.ServiceProvider.GetRequiredService<IAchievementService>();
|
| 157 |
achievementService.SeedAchievementsAsync().Wait();
|