improve sync service to always rely on server dates

This commit is contained in:
sbwalker 2023-08-08 07:51:07 -04:00
parent 17a4985ebe
commit 337a566617
4 changed files with 16 additions and 16 deletions

View File

@ -14,6 +14,6 @@ namespace Oqtane.Services
/// </summary> /// </summary>
/// <param name="lastSyncDate"></param> /// <param name="lastSyncDate"></param>
/// <returns></returns> /// <returns></returns>
Task<Sync> GetSyncAsync(DateTime lastSyncDate); Task<Sync> GetSyncEventsAsync(DateTime lastSyncDate);
} }
} }

View File

@ -16,7 +16,7 @@ namespace Oqtane.Services
private string ApiUrl => CreateApiUrl("Sync"); private string ApiUrl => CreateApiUrl("Sync");
/// <inheritdoc /> /// <inheritdoc />
public async Task<Sync> GetSyncAsync(DateTime lastSyncDate) public async Task<Sync> GetSyncEventsAsync(DateTime lastSyncDate)
{ {
return await GetJsonAsync<Sync>($"{ApiUrl}/{lastSyncDate.ToString("yyyyMMddHHmmssfff")}"); return await GetJsonAsync<Sync>($"{ApiUrl}/{lastSyncDate.ToString("yyyyMMddHHmmssfff")}");
} }

View File

@ -98,7 +98,7 @@
User user = null; User user = null;
var editmode = false; var editmode = false;
var refresh = false; var refresh = false;
var lastsyncdate = DateTime.UtcNow.AddHours(-1); // events in the past hour var lastsyncdate = DateTime.MinValue;
var runtime = (Shared.Runtime)Enum.Parse(typeof(Shared.Runtime), Runtime); var runtime = (Shared.Runtime)Enum.Parse(typeof(Shared.Runtime), Runtime);
_error = ""; _error = "";
@ -177,28 +177,22 @@
} }
// process any sync events // process any sync events
var sync = await SyncService.GetSyncAsync(lastsyncdate); var sync = await SyncService.GetSyncEventsAsync(lastsyncdate);
lastsyncdate = sync.SyncDate; lastsyncdate = sync.SyncDate;
if (sync.SyncEvents.Any()) if (sync.SyncEvents.Any())
{ {
// reload client application if server was restarted or site runtime/rendermode was modified // reload client application if server was restarted or site runtime/rendermode was modified
if (PageState != null && site != null && sync.SyncEvents.Exists(item => item.Action == SyncEventActions.Reload && ((item.EntityName == EntityNames.Host && site.Runtime != "Server") || (item.EntityName == EntityNames.Site && item.EntityId == site.SiteId)))) if (PageState != null && site != null && sync.SyncEvents.Exists(item => item.Action == SyncEventActions.Reload && ((item.EntityName == EntityNames.Host && site.Runtime != "Server") || (item.EntityName == EntityNames.Site && item.EntityId == site.SiteId))))
{ {
if (!querystring.ContainsKey("reloaded")) NavigationManager.NavigateTo(_absoluteUri, true);
{
NavigationManager.NavigateTo(_absoluteUri + (!_absoluteUri.Contains("?") ? "?" : "&") + "reloaded", true);
return; return;
} }
}
// reload client application if current user auth information has changed // reload client application if current user auth information has changed
if (PageState != null && user != null && sync.SyncEvents.Exists(item => item.Action == SyncEventActions.Reload && item.EntityName == EntityNames.User && item.EntityId == user.UserId)) if (PageState != null && user != null && sync.SyncEvents.Exists(item => item.Action == SyncEventActions.Reload && item.EntityName == EntityNames.User && item.EntityId == user.UserId))
{ {
if (!querystring.ContainsKey("reloaded")) NavigationManager.NavigateTo(_absoluteUri, true);
{
NavigationManager.NavigateTo(_absoluteUri + (!_absoluteUri.Contains("?") ? "?" : "&") + "reloaded", true);
return; return;
} }
}
// when site information has changed the PageState needs to be refreshed // when site information has changed the PageState needs to be refreshed
if (sync.SyncEvents.Exists(item => item.EntityName == EntityNames.Site && item.EntityId == SiteState.Alias.SiteId)) if (sync.SyncEvents.Exists(item => item.EntityName == EntityNames.Site && item.EntityId == SiteState.Alias.SiteId))
{ {

View File

@ -23,10 +23,16 @@ namespace Oqtane.Controllers
[HttpGet("{lastSyncDate}")] [HttpGet("{lastSyncDate}")]
public Sync Get(string lastSyncDate) public Sync Get(string lastSyncDate)
{ {
DateTime currentdate = DateTime.UtcNow;
DateTime lastdate = DateTime.ParseExact(lastSyncDate, "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture);
if (lastdate == DateTime.MinValue)
{
lastdate = currentdate;
}
Sync sync = new Sync Sync sync = new Sync
{ {
SyncDate = DateTime.UtcNow, SyncDate = currentdate,
SyncEvents = _syncManager.GetSyncEvents(_alias.TenantId, DateTime.ParseExact(lastSyncDate, "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture)) SyncEvents = _syncManager.GetSyncEvents(_alias.TenantId, lastdate)
}; };
return sync; return sync;
} }