|
| 1 | +using NetTopologySuite.Algorithm; |
| 2 | +using System.Diagnostics; |
| 3 | + |
| 4 | +namespace BotSharp.Core.Conversations.Services; |
| 5 | + |
| 6 | +public partial class ConversationService |
| 7 | +{ |
| 8 | + public async Task<bool> MigrateLatestStates(int batchSize = 100, int errorLimit = 10) |
| 9 | + { |
| 10 | + var db = _services.GetRequiredService<IBotSharpRepository>(); |
| 11 | + var isSuccess = true; |
| 12 | + var errorCount = 0; |
| 13 | + var batchNum = 0; |
| 14 | + var info = string.Empty; |
| 15 | + var error = string.Empty; |
| 16 | + |
| 17 | +#if DEBUG |
| 18 | + Console.WriteLine($"\r\n#Start migrating Conversation Latest States...\r\n"); |
| 19 | +#else |
| 20 | + _logger.LogInformation($"#Start migrating Conversation Latest States..."); |
| 21 | +#endif |
| 22 | + var sw = Stopwatch.StartNew(); |
| 23 | + |
| 24 | + var convIds = db.GetConversationsToMigrate(batchSize); |
| 25 | + |
| 26 | + while (!convIds.IsNullOrEmpty()) |
| 27 | + { |
| 28 | + batchNum++; |
| 29 | + var innerSw = Stopwatch.StartNew(); |
| 30 | +#if DEBUG |
| 31 | + Console.WriteLine($"\r\n#Start migrating Conversation Latest States (batch number: {batchNum})\r\n"); |
| 32 | +#else |
| 33 | + _logger.LogInformation($"#Start migrating Conversation Latest States (batch number: {batchNum})"); |
| 34 | +#endif |
| 35 | + |
| 36 | + for (int i = 0; i < convIds.Count; i++) |
| 37 | + { |
| 38 | + var convId = convIds.ElementAt(i); |
| 39 | + try |
| 40 | + { |
| 41 | + var done = db.MigrateConvsersationLatestStates(convId); |
| 42 | + info = $"Conversation {convId} latest states have been migrated ({i + 1}/{convIds.Count})!"; |
| 43 | +#if DEBUG |
| 44 | + Console.WriteLine($"\r\n{info}\r\n"); |
| 45 | +#else |
| 46 | + _logger.LogInformation($"{info}"); |
| 47 | +#endif |
| 48 | + } |
| 49 | + catch (Exception ex) |
| 50 | + { |
| 51 | + errorCount++; |
| 52 | + error = $"Conversation {convId} latest states fail to be migrated! ({i + 1}/{convIds.Count})\r\n{ex.Message}\r\n{ex.InnerException}"; |
| 53 | +#if DEBUG |
| 54 | + Console.WriteLine($"\r\n{error}\r\n"); |
| 55 | +#else |
| 56 | + _logger.LogError($"{error}"); |
| 57 | +#endif |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (errorCount >= errorLimit) |
| 62 | + { |
| 63 | + error = $"\r\nErrors exceed limit => stop the migration!\r\n"; |
| 64 | +#if DEBUG |
| 65 | + Console.WriteLine($"{error}"); |
| 66 | +#else |
| 67 | + _logger.LogError($"{error}"); |
| 68 | +#endif |
| 69 | + innerSw.Stop(); |
| 70 | + isSuccess = false; |
| 71 | + break; |
| 72 | + } |
| 73 | + |
| 74 | + innerSw.Stop(); |
| 75 | + info = $"#Done migrating Conversation Latest States (batch number: {batchNum}) " + |
| 76 | + $"(Total time: {innerSw.Elapsed.Hours} hrs, {innerSw.Elapsed.Minutes} mins, {innerSw.Elapsed.Seconds} seconds)"; |
| 77 | +#if DEBUG |
| 78 | + Console.WriteLine($"\r\n{info}\r\n"); |
| 79 | +#else |
| 80 | + _logger.LogInformation($"{info}"); |
| 81 | +#endif |
| 82 | + |
| 83 | + await Task.Delay(100); |
| 84 | + convIds = db.GetConversationsToMigrate(batchSize); |
| 85 | + } |
| 86 | + |
| 87 | + sw.Stop(); |
| 88 | + info = $"#Done with migrating Conversation Latest States! " + |
| 89 | + $"(Total time: {sw.Elapsed.Days} days, {sw.Elapsed.Hours} hrs, {sw.Elapsed.Minutes} mins, {sw.Elapsed.Seconds} seconds)"; |
| 90 | +#if DEBUG |
| 91 | + Console.WriteLine($"\r\n{info}\r\n"); |
| 92 | +#else |
| 93 | + _logger.LogInformation($"{info}"); |
| 94 | +#endif |
| 95 | + |
| 96 | + return isSuccess; |
| 97 | + } |
| 98 | +} |
0 commit comments