Skip to content

Commit

Permalink
Merge pull request #367 from dimmik/beta/soft-lock-for-queue
Browse files Browse the repository at this point in the history
soft lock on queue
  • Loading branch information
dimmik authored Nov 7, 2024
2 parents 8321986 + baebb03 commit 51513b2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
3 changes: 1 addition & 2 deletions TCBlazor/Client/Pages/TourListPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ else
{
InProgress = true;
Tour t = new Tour() { Id = tour.Id, Name = tour.Name, Duration = tour.Duration };
_ = engine.RequestEditTourProps(t, ("changename", t));
_ = engine.RequestEditTourProps(t, ("duration", t));
_ = engine.RequestEditTourProps(t, ("changename", t), ("duration", t));
return Task.CompletedTask;
}

Expand Down
2 changes: 1 addition & 1 deletion TCalcCore/Network/ITCDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface ITCDataService
Task GetAndStoreToken(string scope, string code);
Task GetAndStoreTokenForCodeMd5(string code);
Task<AuthData> GetAuthData(bool forceGetFromServer = false);
Task<Queue<SerializableTourOperation>> GetServerQueue(string tourId);
Task<Queue<SerializableTourOperation>> GetServerQueue(string tourId, bool checkOut = false);
Task<TourList> GetTourList(Func<TourList, bool, DateTimeOffset, Task> onTourListAvailable, bool forceFromServer);
Task<TourList> GetTourListFromServer();
Task<TourList> GetTourVersions(Tour tour);
Expand Down
33 changes: 30 additions & 3 deletions TCalcCore/Network/TCDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,28 @@ private Queue<SerializableTourOperation> GetLocalQueue(string tourId)
#endregion

#region Server Queue
public async Task<Queue<SerializableTourOperation>> GetServerQueue(string tourId)
public async Task<Queue<SerializableTourOperation>> GetServerQueue(string tourId, bool checkOut = false)
{
var waitInMs = 10;
var waitCnt = 300;
var counter = 0;
while (counter < waitCnt)
{
if (!CheckedOut)
{
return await GetServerQueueInternal(tourId, checkOut);
}
counter++;
await Task.Delay(waitInMs);
}
// for some reason cannot get the checked-in queue after 3s. So be it, return what we have.
logger?.Log("Failed to acquire server queue");
return await GetServerQueueInternal(tourId, false);
}
private bool CheckedOut = false;
private async Task<Queue<SerializableTourOperation>> GetServerQueueInternal(string tourId, bool checkOut)
{
CheckedOut = checkOut;
var (qc, _) = await ts.GetObject<SerializableTourOperationContainer>(
GetUpdateQueueStorageKey(tourId),
() => default,
Expand All @@ -342,7 +362,14 @@ private async Task StoreServerQueue(string tourId, Queue<SerializableTourOperati
{
operations = q.ToList()
};
await ts.SetObject(GetUpdateQueueStorageKey(tourId), qc);
try
{
await ts.SetObject(GetUpdateQueueStorageKey(tourId), qc);
}
finally
{
CheckedOut = false;
}
OnServerQueueStored?.Invoke();
}
#endregion
Expand All @@ -365,7 +392,7 @@ private async Task UpdateTour(string tourId, Tour tour)

private async Task EditTourData(string tourId, params SerializableTourOperation[] ops)
{
Queue<SerializableTourOperation> serverQueue = await GetServerQueue(tourId);
Queue<SerializableTourOperation> serverQueue = await GetServerQueue(tourId, checkOut: true);
Queue<SerializableTourOperation> localQueue = GetLocalQueue(tourId);
foreach (var op in ops)
{
Expand Down

0 comments on commit 51513b2

Please sign in to comment.