From 9e20d9d143dcd1ba9fb6d041502b24c8799097b0 Mon Sep 17 00:00:00 2001 From: pavelbannov Date: Mon, 18 Dec 2023 17:18:40 +0300 Subject: [PATCH] bug 65562 --- products/ASC.Files/Core/Utils/FileTracker.cs | 100 +++++++++---------- 1 file changed, 47 insertions(+), 53 deletions(-) diff --git a/products/ASC.Files/Core/Utils/FileTracker.cs b/products/ASC.Files/Core/Utils/FileTracker.cs index e1c737f98cc..0427a4d4d8f 100644 --- a/products/ASC.Files/Core/Utils/FileTracker.cs +++ b/products/ASC.Files/Core/Utils/FileTracker.cs @@ -58,7 +58,13 @@ public bool ProlongEditing(T fileId, Guid tabId, Guid userId, int tenantId, b } else { - tracker.EditingBy.Add(tabId, new TrackInfo(userId, tabId == userId, editingAlone, tenantId)); + tracker.EditingBy.Add(tabId, new TrackInfo + { + UserId = userId, + NewScheme = tabId == userId, + EditingAlone = editingAlone, + TenantId = tenantId + }); } } else @@ -102,36 +108,14 @@ public void Remove(T fileId, Guid tabId = default, Guid userId = default) SetTracker(fileId, null); } - public void RemoveAllOther(Guid userId, T fileId) - { - var tracker = GetTracker(fileId); - if (tracker != null) - { - var listForRemove = tracker.EditingBy - .Where(b => b.Value.UserId != userId); - - if (listForRemove.Count() != tracker.EditingBy.Count) - { - foreach (var forRemove in listForRemove) - { - tracker.EditingBy.Remove(forRemove.Key); - } - - SetTracker(fileId, tracker); - - return; - } - } - SetTracker(fileId, null); - } - public bool IsEditing(T fileId) { var tracker = GetTracker(fileId); if (tracker != null) { + var now = DateTime.UtcNow; var listForRemove = tracker.EditingBy - .Where(e => !e.Value.NewScheme && (DateTime.UtcNow - e.Value.TrackTime).Duration() > TrackTimeout); + .Where(e => !e.Value.NewScheme && (now - e.Value.TrackTime).Duration() > TrackTimeout); foreach (var editTab in listForRemove) { @@ -206,7 +190,8 @@ private void SetTracker(T fileId, FileTracker tracker) { if (tracker != null) { - _cache.Insert(Tracker + fileId, tracker, CacheTimeout, EvictionCallback(fileId, tracker)); + tracker = tracker with { }; + _cache.Insert(Tracker + fileId, tracker with {}, CacheTimeout, EvictionCallback()); } else { @@ -215,9 +200,21 @@ private void SetTracker(T fileId, FileTracker tracker) } } - private Action EvictionCallback(T fileId, FileTracker fileTracker) + private Action EvictionCallback() { - return async (_, _, reason, _) => + return (cacheFileId, fileTracker, reason, _) => + { + if(int.TryParse(cacheFileId?.ToString(), out var internalFileId)) + { + Callback(internalFileId, fileTracker as FileTracker, reason).Wait(); + } + else + { + Callback(cacheFileId?.ToString(), fileTracker as FileTracker, reason).Wait(); + } + }; + + async Task Callback(T fileId, FileTracker fileTracker, EvictionReason reason) { if (reason != EvictionReason.Expired) { @@ -241,50 +238,47 @@ private Action EvictionCallback(T fil var daoFactory = scope.ServiceProvider.GetRequiredService(); var docKey = await helper.GetDocKeyAsync(await daoFactory.GetFileDao().GetFileAsync(fileId)); - + if (await tracker.StartTrackAsync(fileId.ToString(), docKey)) { - _cache.Insert(Tracker + fileId, fileTracker, CacheTimeout, EvictionCallback(fileId, fileTracker)); + _cache.Insert(Tracker + fileId, fileTracker with {}, CacheTimeout, EvictionCallback()); } } catch (Exception e) { _logger.ErrorWithException(e); } - }; + } } } -public class FileTracker +public record FileTracker { - - internal Dictionary EditingBy { get; private set; } + internal Dictionary EditingBy { get; } internal FileTracker(Guid tabId, Guid userId, bool newScheme, bool editingAlone, int tenantId) { - EditingBy = new Dictionary { { tabId, new TrackInfo(userId, newScheme, editingAlone, tenantId) } }; + EditingBy = new() + { + { tabId, new TrackInfo + { + UserId = userId, + NewScheme = newScheme, + EditingAlone = editingAlone, + TenantId = tenantId + } + } + }; } internal class TrackInfo { - public DateTime CheckRightTime { get; set; } - public DateTime TrackTime { get; set; } - public Guid UserId { get; set; } - public int TenantId { get; set; } - public bool NewScheme { get; set; } - public bool EditingAlone { get; set; } - - public TrackInfo() { } - - public TrackInfo(Guid userId, bool newScheme, bool editingAlone, int tenantId) - { - CheckRightTime = DateTime.UtcNow; - TrackTime = DateTime.UtcNow; - NewScheme = newScheme; - UserId = userId; - EditingAlone = editingAlone; - TenantId = tenantId; - } + public DateTime CheckRightTime { get; set; } = DateTime.UtcNow; + public DateTime TrackTime { get; set; } = DateTime.UtcNow; + public required Guid UserId { get; init; } + public required int TenantId { get; init; } + public required bool NewScheme { get; init; } + public required bool EditingAlone { get; init; } } }