|
2 | 2 | using System.Collections.Immutable;
|
3 | 3 | using System.Diagnostics;
|
4 | 4 | using System.Text;
|
5 |
| -using System.Text.Json; |
6 | 5 | using AsyncAwaitBestPractices;
|
7 | 6 | using AutoCtor;
|
8 | 7 | using KGySoft.CoreLibraries;
|
| 8 | +using LiteDB; |
| 9 | +using LiteDB.Async; |
9 | 10 | using Microsoft.Extensions.Logging;
|
10 | 11 | using StabilityMatrix.Core.Attributes;
|
11 | 12 | using StabilityMatrix.Core.Database;
|
|
15 | 16 | using StabilityMatrix.Core.Models.Api;
|
16 | 17 | using StabilityMatrix.Core.Models.Database;
|
17 | 18 | using StabilityMatrix.Core.Models.FileInterfaces;
|
| 19 | +using JsonSerializer = System.Text.Json.JsonSerializer; |
18 | 20 |
|
19 | 21 | namespace StabilityMatrix.Core.Services;
|
20 | 22 |
|
@@ -103,9 +105,34 @@ private async Task LoadFromDbAsync()
|
103 | 105 |
|
104 | 106 | logger.LogInformation("Loading models from database...");
|
105 | 107 |
|
106 |
| - var allModels = ( |
107 |
| - await liteDbContext.LocalModelFiles.IncludeAll().FindAllAsync().ConfigureAwait(false) |
108 |
| - ).ToImmutableArray(); |
| 108 | + ImmutableArray<LocalModelFile> allModels = []; |
| 109 | + try |
| 110 | + { |
| 111 | + allModels = |
| 112 | + [ |
| 113 | + ..( |
| 114 | + await liteDbContext.LocalModelFiles.IncludeAll().FindAllAsync().ConfigureAwait(false) |
| 115 | + ) |
| 116 | + ]; |
| 117 | + } |
| 118 | + catch (Exception e) |
| 119 | + { |
| 120 | + // Handle enum deserialize exceptions from changes |
| 121 | + if (e is LiteException or LiteAsyncException && e.InnerException is ArgumentException inner) |
| 122 | + { |
| 123 | + logger.LogWarning( |
| 124 | + e, |
| 125 | + "LiteDb Deserialize error while fetching LocalModelFiles '{Inner}', cache collections will be cleared", |
| 126 | + inner.ToString() |
| 127 | + ); |
| 128 | + |
| 129 | + await liteDbContext.ClearAllCacheCollectionsAsync().ConfigureAwait(false); |
| 130 | + } |
| 131 | + else |
| 132 | + { |
| 133 | + throw; |
| 134 | + } |
| 135 | + } |
109 | 136 |
|
110 | 137 | ModelIndex = allModels.GroupBy(m => m.SharedFolderType).ToDictionary(g => g.Key, g => g.ToList());
|
111 | 138 |
|
@@ -479,12 +506,34 @@ private async Task RefreshIndexParallelCore()
|
479 | 506 |
|
480 | 507 | if (model.LatestModelInfo == null && model.HasConnectedModel)
|
481 | 508 | {
|
482 |
| - var civitModel = await liteDbContext |
483 |
| - .CivitModels.Include(m => m.ModelVersions) |
484 |
| - .FindByIdAsync(model.ConnectedModelInfo.ModelId) |
485 |
| - .ConfigureAwait(false); |
| 509 | + try |
| 510 | + { |
| 511 | + model.LatestModelInfo = await liteDbContext |
| 512 | + .CivitModels.Include(m => m.ModelVersions) |
| 513 | + .FindByIdAsync(model.ConnectedModelInfo.ModelId) |
| 514 | + .ConfigureAwait(false); |
| 515 | + } |
| 516 | + catch (Exception e) |
| 517 | + { |
| 518 | + // Handle enum deserialize exceptions from changes |
| 519 | + if ( |
| 520 | + e is LiteException or LiteAsyncException |
| 521 | + && e.InnerException is ArgumentException inner |
| 522 | + ) |
| 523 | + { |
| 524 | + logger.LogWarning( |
| 525 | + e, |
| 526 | + "LiteDb Deserialize error while fetching CivitModels '{Inner}', cache collections will be cleared", |
| 527 | + inner.ToString() |
| 528 | + ); |
486 | 529 |
|
487 |
| - model.LatestModelInfo = civitModel; |
| 530 | + await liteDbContext.ClearAllCacheCollectionsAsync().ConfigureAwait(false); |
| 531 | + } |
| 532 | + else |
| 533 | + { |
| 534 | + throw; |
| 535 | + } |
| 536 | + } |
488 | 537 | }
|
489 | 538 | var list = newIndex.GetOrAdd(model.SharedFolderType);
|
490 | 539 | list.Add(model);
|
|
0 commit comments