Start acknowledgement publication timing after its atomic snapshot
This commit is contained in:
parent
717be089b9
commit
0e331eacb5
@ -170,7 +170,7 @@ public sealed partial class TrayCoordinator(SessionManager sessions,CloudGateway
|
||||
public async Task Heartbeat(CancellationToken cancellation)
|
||||
{
|
||||
while(!cancellation.IsCancellationRequested){await Task.Delay(TimeSpan.FromMilliseconds(500),cancellation);var available=Documents.Where(d=>d.Authentication==cloud.Status().AuthenticationContext&&cloud.Status().Health?.Authentication=="verified").ToArray();
|
||||
foreach(var doc in available){var freshObservation=FreshWordObservation(doc);if(doc.LastPresenceKey==""&&DateTime.UtcNow-doc.LastPresenceUtc<TimeSpan.FromSeconds(2))continue;var key=$"{freshObservation}:{doc.Capture?.RequestId}:{(doc.Capture!=null?doc.Progress/5:0)}:{doc.WordSaved}:{doc.Book}:{doc.Revision}:{doc.ChapterId}:{doc.SceneId}:{doc.Active}:{doc.NavigationAcknowledgedId}:{doc.NavigationResult}:{(ParagraphCapable(doc.Instance)||!doc.SceneId.HasValue?doc.Cursor:0)}";if(key==doc.LastPresenceKey&&DateTime.UtcNow-doc.LastPresenceUtc<TimeSpan.FromSeconds(acknowledgedNavigationInstances.Contains(doc.Instance)?2:15))continue;try{var presenceClock=System.Diagnostics.Stopwatch.StartNew();var presenceStarted=System.Diagnostics.Stopwatch.GetTimestamp();Guid? publishedNavigation;long publishedAckTimestamp;string publishedResult;lock(gate){publishedNavigation=doc.NavigationAcknowledgedId;publishedAckTimestamp=doc.NavigationAckTimestamp;publishedResult=doc.NavigationResult;}var reply=await cloud.Presence(doc.Instance,doc.Session,new(){CanAcknowledgeNavigation=acknowledgedNavigationInstances.Contains(doc.Instance),NavigationAcknowledgedId=publishedNavigation,NavigationResult=publishedResult,CaptureRequestId=doc.Capture?.RequestId,CaptureProgress=doc.Progress,ChapterIssue=ChapterIssue(doc),CanChapterSync=ChapterCapable(doc.Instance),CanParagraphSync=ParagraphCapable(doc.Instance),CanCapture=captureInstances.Contains(doc.Instance),WordRunning=true,WordSaved=freshObservation?doc.WordSaved:null,CursorStart=freshObservation?doc.Cursor:null,ManuscriptId=doc.Manuscript,BookId=doc.Book,Revision=doc.Revision,Sequence=doc.PresenceSequence=Interlocked.Increment(ref presenceSequence),ChapterId=freshObservation?doc.ChapterId:null,SceneId=freshObservation?doc.SceneId:null,Active=doc.Active,DocumentTitle=doc.Title},doc.Authentication);if(reply.Navigation?.Kind=="navigate")TraceNavigation(reply.Navigation.Id,"presence-acquisition-roundtrip",presenceClock.Elapsed.TotalMilliseconds);if(publishedNavigation.HasValue&&doc.LastTracedNavigationAck!=publishedNavigation.Value){if(publishedAckTimestamp!=0)TraceNavigation(publishedNavigation.Value,"adapter-result-to-ack-presence",System.Diagnostics.Stopwatch.GetElapsedTime(publishedAckTimestamp,presenceStarted).TotalMilliseconds);TraceNavigation(publishedNavigation.Value,"ack-presence-roundtrip",presenceClock.Elapsed.TotalMilliseconds);doc.LastTracedNavigationAck=publishedNavigation.Value;}lock(gate){if(documents.GetValueOrDefault(doc.Session)!=doc||(ChapterCapable(doc.Instance)&&reply.Revision!=doc.Revision)||doc.Authentication!=cloud.Status().AuthenticationContext)continue;if(ChapterCapable(doc.Instance)&&chapterWork.TryGetValue(doc.Session,out var currentChapters)&¤tChapters.Manifest!=null&&reply.ParagraphRevision>0&&reply.ParagraphRevision<currentChapters.Manifest.Revision)continue;doc.LastPresenceKey=key;doc.LastPresenceUtc=DateTime.UtcNow;doc.ChapterProgress=reply.ChapterProgress;if(ChapterCapable(doc.Instance))ChapterCursorEvidence.Apply(doc,reply);else{doc.ChapterTitle=reply.ChapterTitle;doc.SceneTitle=reply.SceneTitle;}ObserveChapterPresence(doc,reply);}
|
||||
foreach(var doc in available){var freshObservation=FreshWordObservation(doc);if(doc.LastPresenceKey==""&&DateTime.UtcNow-doc.LastPresenceUtc<TimeSpan.FromSeconds(2))continue;var key=$"{freshObservation}:{doc.Capture?.RequestId}:{(doc.Capture!=null?doc.Progress/5:0)}:{doc.WordSaved}:{doc.Book}:{doc.Revision}:{doc.ChapterId}:{doc.SceneId}:{doc.Active}:{doc.NavigationAcknowledgedId}:{doc.NavigationResult}:{(ParagraphCapable(doc.Instance)||!doc.SceneId.HasValue?doc.Cursor:0)}";if(key==doc.LastPresenceKey&&DateTime.UtcNow-doc.LastPresenceUtc<TimeSpan.FromSeconds(acknowledgedNavigationInstances.Contains(doc.Instance)?2:15))continue;try{var presenceClock=System.Diagnostics.Stopwatch.StartNew();Guid? publishedNavigation;long publishedAckTimestamp;string publishedResult;lock(gate){publishedNavigation=doc.NavigationAcknowledgedId;publishedAckTimestamp=doc.NavigationAckTimestamp;publishedResult=doc.NavigationResult;}var presenceStarted=System.Diagnostics.Stopwatch.GetTimestamp();var reply=await cloud.Presence(doc.Instance,doc.Session,new(){CanAcknowledgeNavigation=acknowledgedNavigationInstances.Contains(doc.Instance),NavigationAcknowledgedId=publishedNavigation,NavigationResult=publishedResult,CaptureRequestId=doc.Capture?.RequestId,CaptureProgress=doc.Progress,ChapterIssue=ChapterIssue(doc),CanChapterSync=ChapterCapable(doc.Instance),CanParagraphSync=ParagraphCapable(doc.Instance),CanCapture=captureInstances.Contains(doc.Instance),WordRunning=true,WordSaved=freshObservation?doc.WordSaved:null,CursorStart=freshObservation?doc.Cursor:null,ManuscriptId=doc.Manuscript,BookId=doc.Book,Revision=doc.Revision,Sequence=doc.PresenceSequence=Interlocked.Increment(ref presenceSequence),ChapterId=freshObservation?doc.ChapterId:null,SceneId=freshObservation?doc.SceneId:null,Active=doc.Active,DocumentTitle=doc.Title},doc.Authentication);if(reply.Navigation?.Kind=="navigate")TraceNavigation(reply.Navigation.Id,"presence-acquisition-roundtrip",presenceClock.Elapsed.TotalMilliseconds);if(publishedNavigation.HasValue&&doc.LastTracedNavigationAck!=publishedNavigation.Value){if(publishedAckTimestamp!=0)TraceNavigation(publishedNavigation.Value,"adapter-result-to-ack-presence",System.Diagnostics.Stopwatch.GetElapsedTime(publishedAckTimestamp,presenceStarted).TotalMilliseconds);TraceNavigation(publishedNavigation.Value,"ack-presence-roundtrip",presenceClock.Elapsed.TotalMilliseconds);doc.LastTracedNavigationAck=publishedNavigation.Value;}lock(gate){if(documents.GetValueOrDefault(doc.Session)!=doc||(ChapterCapable(doc.Instance)&&reply.Revision!=doc.Revision)||doc.Authentication!=cloud.Status().AuthenticationContext)continue;if(ChapterCapable(doc.Instance)&&chapterWork.TryGetValue(doc.Session,out var currentChapters)&¤tChapters.Manifest!=null&&reply.ParagraphRevision>0&&reply.ParagraphRevision<currentChapters.Manifest.Revision)continue;doc.LastPresenceKey=key;doc.LastPresenceUtc=DateTime.UtcNow;doc.ChapterProgress=reply.ChapterProgress;if(ChapterCapable(doc.Instance))ChapterCursorEvidence.Apply(doc,reply);else{doc.ChapterTitle=reply.ChapterTitle;doc.SceneTitle=reply.SceneTitle;}ObserveChapterPresence(doc,reply);}
|
||||
if(reply.ParagraphRevision>0&&!ChapterCapable(doc.Instance))lock(gate)if(documents.GetValueOrDefault(doc.Session)==doc&&reply.Revision==doc.Revision&&doc.Authentication==cloud.Status().AuthenticationContext){doc.ChapterId=reply.ChapterId;doc.SceneId=reply.SceneId;}
|
||||
if(reply.Navigation is {Kind:"wizard-capture",BookId:>0} requested&&requested.SessionId==doc.Session&&requested.Revision==doc.Revision&&doc.Book==0){await cloud.CheckConnection();lock(gate){if(doc.Book==0&&requested.SessionId==doc.Session&&requested.Revision==doc.Revision&&doc.Authentication==cloud.Status().AuthenticationContext&&cloud.Allows(doc.Authentication,requested.BookId)&&documents.GetValueOrDefault(doc.Session)==doc){doc.Book=requested.BookId;cloud.RememberBinding(doc.Manuscript,doc.Book,doc.Authentication);doc.LastPresenceKey="";}}}
|
||||
if(reply.Navigation!=null)lock(gate)if(documents.GetValueOrDefault(doc.Session)==doc&&reply.Navigation.SessionId==doc.Session&&reply.Navigation.Revision==doc.Revision){if((reply.Navigation.Kind is "wizard-capture" or "paragraph-snapshot")&&captureInstances.Contains(doc.Instance)&&(reply.Navigation.BookId==0||reply.Navigation.BookId==doc.Book)&&cloud.Allows(doc.Authentication,doc.Book)){if(uploading!=0||documents.Values.Any(d=>d.Capture!=null)){_=cloud.FailCapture(reply.Navigation.Id,doc.Authentication);continue;}var retained=doc.RetryCapture;var reusable=retained!=null&&doc.RetryAuthentication==doc.Authentication&&doc.RetryBook==doc.Book&&retained.ManuscriptId==doc.Manuscript&&retained.Revision==doc.Revision&&retained.ParagraphTracking==reply.Navigation.ParagraphTracking;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user