private List <Task> GcPages(ActivePagesByTopic activePage, List <Task> tasks) { var now = DateTime.UtcNow; //Garbage collect pages foreach (var loadedPage in _messagesContentCache.GetLoadedPages(activePage.Snapshot.TopicId)) { if (activePage.Pages.Any(activePageId => loadedPage.PageId.Value == activePageId.Value)) { continue; } if (now - loadedPage.LastAccessTime < GcTimeout) { continue; } var task = _taskSchedulerByTopic.ExecuteTaskAsync(activePage.Snapshot.TopicId, loadedPage.PageId, "GC page", () => GcThreadTopicSynchronizedAsync(activePage.Snapshot.TopicId, loadedPage.PageId)); tasks ??= new List <Task>(); tasks.Add(task); } return(tasks); }
public async ValueTask Sync() { if (!_appGlobalFlags.Initialized) { return; } var topics = _messagesContentCache.GetLoadedPages(); foreach (var(topicId, pages) in topics) { if (_appGlobalFlags.DebugTopic == topicId) { _appLogger.AddLog(LogProcess.Debug, topicId, "Sync Topic Event", $"Found pages {pages.Count} to sync "); } foreach (var page in pages) { if (page is WritableContentPage { NotSavedAmount: > 0 } writableContentCachePage) { if (_appGlobalFlags.DebugTopic == topicId) { _appLogger.AddLog(LogProcess.Debug, topicId, $"Sync Page {page.PageId}", $"Page with hash {page.GetHashCode()} has {page.NotSavedAmount} unsynched messages"); } await _taskSchedulerByTopic.ExecuteTaskAsync(topicId, page.PageId, "Upload messages to blob", async() => { if (_appGlobalFlags.DebugTopic == topicId) { _appLogger.AddLog(LogProcess.Debug, topicId, $"Sync Page {page.PageId}", "Start uploading messages"); } var result = await _messagesContentPersistentStorage.SyncAsync(topicId, page.PageId); if (_appGlobalFlags.DebugTopic == topicId) { _appLogger.AddLog(LogProcess.Debug, topicId, $"Sync Page {page.PageId}", $"Upload result is {result}. Messages after upload {page.NotSavedAmount}"); } while (result != SyncResult.Done) { _appLogger.AddLog(LogProcess.PagesLoaderOrGc, topicId, "PageId: " + page.PageId.Value, "There are messages to upload but no writer found. Creating one..."); await _messagesContentPersistentStorage.CreateNewPageAsync(topicId, page.PageId, () => writableContentCachePage); result = await _messagesContentPersistentStorage.SyncAsync(topicId, page.PageId); } }); }