Bugfix for update deadlock and increased version to 1.2.1.1

This commit is contained in:
Daniel Schick 2024-05-14 10:48:11 +02:00
parent c5df95625d
commit 97d4fa9faf

View File

@ -38,8 +38,9 @@ namespace BreCalClient
#region Fields #region Fields
private static Int32 _uiUpdateRunning = 0; //private static int _uiUpdateRunning = 0;
private static SemaphoreSlim uiLock = new SemaphoreSlim(1);
private Credentials? _credentials; private Credentials? _credentials;
private readonly ConcurrentDictionary<int, ShipcallControlModel> _allShipcallsDict = new(); private readonly ConcurrentDictionary<int, ShipcallControlModel> _allShipcallsDict = new();
@ -50,7 +51,7 @@ namespace BreCalClient
private readonly UserApi _userApi; private readonly UserApi _userApi;
private readonly TimesApi _timesApi; private readonly TimesApi _timesApi;
private readonly StaticApi _staticApi; private readonly StaticApi _staticApi;
private readonly ShipApi _shipApi; private readonly ShipApi _shipApi;
private CancellationTokenSource _tokenSource = new(); private CancellationTokenSource _tokenSource = new();
private LoginResult? _loginResult; private LoginResult? _loginResult;
@ -91,16 +92,16 @@ namespace BreCalClient
_staticApi = new StaticApi(Properties.Settings.Default.API_URL); _staticApi = new StaticApi(Properties.Settings.Default.API_URL);
_staticApi.Configuration.ApiKeyPrefix["Authorization"] = "Bearer"; _staticApi.Configuration.ApiKeyPrefix["Authorization"] = "Bearer";
_shipApi = new ShipApi(Properties.Settings.Default.API_URL); _shipApi = new ShipApi(Properties.Settings.Default.API_URL);
_shipApi.Configuration.ApiKeyPrefix["Authorization"] = "Bearer"; _shipApi.Configuration.ApiKeyPrefix["Authorization"] = "Bearer";
const int maxDelayInMilliseconds = 32 * 1000; const int maxDelayInMilliseconds = 32 * 1000;
var jitterer = new Random(); var jitterer = new Random();
var retryPolicy = var retryPolicy =
// Policy.Handle<HttpRequestException>() // Policy.Handle<HttpRequestException>()
Policy.HandleResult<RestSharp.RestResponse>(resp => resp.StatusCode == HttpStatusCode.Unauthorized) Policy.HandleResult<RestSharp.RestResponse>(resp => resp.StatusCode == HttpStatusCode.Unauthorized)
//.OrResult<RestSharp.RestResponse> //.OrResult<RestSharp.RestResponse>
.WaitAndRetryAsync(1, .WaitAndRetryAsync(1,
retryAttempt => retryAttempt =>
{ {
var calculatedDelayInMilliseconds = Math.Pow(2, retryAttempt) * 1000; var calculatedDelayInMilliseconds = Math.Pow(2, retryAttempt) * 1000;
@ -171,7 +172,7 @@ namespace BreCalClient
this._staticApi.Configuration.ApiKey["Authorization"] = _loginResult.Token; this._staticApi.Configuration.ApiKey["Authorization"] = _loginResult.Token;
this._shipApi.Configuration.ApiKey["Authorization"] = _loginResult.Token; this._shipApi.Configuration.ApiKey["Authorization"] = _loginResult.Token;
this.LoadStaticLists(); this.LoadStaticLists();
this.labelUsername.Text = $"{_loginResult.FirstName} {_loginResult.LastName}"; this.labelUsername.Text = $"{_loginResult.FirstName} {_loginResult.LastName}";
} }
} }
labelGeneralStatus.Text = $"Connection {ConnectionStatus.SUCCESSFUL}"; labelGeneralStatus.Text = $"Connection {ConnectionStatus.SUCCESSFUL}";
@ -211,9 +212,9 @@ namespace BreCalClient
this._timesApi.Configuration.ApiKey["Authorization"] = _loginResult.Token; this._timesApi.Configuration.ApiKey["Authorization"] = _loginResult.Token;
this._shipcallApi.Configuration.ApiKey["Authorization"] = _loginResult.Token; this._shipcallApi.Configuration.ApiKey["Authorization"] = _loginResult.Token;
this._staticApi.Configuration.ApiKey["Authorization"] = _loginResult.Token; this._staticApi.Configuration.ApiKey["Authorization"] = _loginResult.Token;
this._shipApi.Configuration.ApiKey["Authorization"] = _loginResult.Token; this._shipApi.Configuration.ApiKey["Authorization"] = _loginResult.Token;
result = true; result = true;
} }
} }
else else
{ {
@ -237,7 +238,7 @@ namespace BreCalClient
NewWithModel(null); NewWithModel(null);
} }
private void NewWithModel(ShipcallControlModel? model) private async void NewWithModel(ShipcallControlModel? model)
{ {
EditShipcallControl esc = new() EditShipcallControl esc = new()
{ {
@ -252,7 +253,9 @@ namespace BreCalClient
// create UI & save new dialog model // create UI & save new dialog model
if (esc.ShipcallModel.Shipcall != null) if (esc.ShipcallModel.Shipcall != null)
{ {
await uiLock.WaitAsync();
this.UpdateUI(); this.UpdateUI();
uiLock.Release();
esc.ShipcallModel.Shipcall?.Participants.Clear(); esc.ShipcallModel.Shipcall?.Participants.Clear();
foreach (ParticipantAssignment pa in esc.ShipcallModel.AssignedParticipants.Values) foreach (ParticipantAssignment pa in esc.ShipcallModel.AssignedParticipants.Values)
@ -346,10 +349,12 @@ namespace BreCalClient
this.FilterShipcalls(); this.FilterShipcalls();
} }
private void SearchFilterControl_SearchFilterChanged() private async void SearchFilterControl_SearchFilterChanged()
{ {
this.FilterShipcalls(); this.FilterShipcalls();
await uiLock.WaitAsync();
this.UpdateUI(); this.UpdateUI();
uiLock.Release();
} }
private void checkboxShowCancelledCalls_Checked(object sender, RoutedEventArgs e) private void checkboxShowCancelledCalls_Checked(object sender, RoutedEventArgs e)
@ -358,11 +363,13 @@ namespace BreCalClient
this.SearchFilterControl_SearchFilterChanged(); this.SearchFilterControl_SearchFilterChanged();
} }
private void comboBoxSortOrder_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) private async void comboBoxSortOrder_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{ {
_sortOrder = (Extensions.SortOrder) this.comboBoxSortOrder.SelectedIndex; _sortOrder = (Extensions.SortOrder) this.comboBoxSortOrder.SelectedIndex;
this.FilterShipcalls(); this.FilterShipcalls();
await uiLock.WaitAsync();
this.UpdateUI(); this.UpdateUI();
uiLock.Release();
} }
private void buttonHistory_Click(object sender, RoutedEventArgs e) private void buttonHistory_Click(object sender, RoutedEventArgs e)
@ -445,7 +452,7 @@ namespace BreCalClient
this.Dispatcher.Invoke(new Action(() => this.Dispatcher.Invoke(new Action(() =>
{ {
labelGeneralStatus.Text = $"Connection {ConnectionStatus.SUCCESSFUL}"; labelGeneralStatus.Text = $"Connection {ConnectionStatus.SUCCESSFUL}";
labelLatestUpdate.Text = $"Last update: {DateTime.Now.ToLongTimeString()}"; labelLatestUpdate.Text = $"Last update: {DateTime.Now.ToLongTimeString()}";
labelStatusBar.Text = ""; labelStatusBar.Text = "";
generalProgressStatus.Value = 0; generalProgressStatus.Value = 0;
@ -502,7 +509,9 @@ namespace BreCalClient
} }
this.FilterShipcalls(); this.FilterShipcalls();
this.UpdateUI(); await uiLock.WaitAsync();
this.UpdateUI();
uiLock.Release();
} }
try try
@ -665,14 +674,14 @@ namespace BreCalClient
{ {
_ = this._visibleControlModels.RemoveAll(x => _ = this._visibleControlModels.RemoveAll(x =>
{ {
Times? t = x.GetTimesForParticipantType(ParticipantType.AGENCY); Times? t = x.GetTimesForParticipantType(ParticipantType.AGENCY);
switch (x.Shipcall?.Type) switch (x.Shipcall?.Type)
{ {
case ShipcallType.Arrival: case ShipcallType.Arrival:
{ {
if ((t != null) && t.EtaBerth.HasValue) return t.EtaBerth.Value < sfm.EtaFrom; if ((t != null) && t.EtaBerth.HasValue) return t.EtaBerth.Value < sfm.EtaFrom;
return x.Shipcall?.Eta < sfm.EtaFrom; return x.Shipcall?.Eta < sfm.EtaFrom;
} }
default: // Shifting / Departing default: // Shifting / Departing
{ {
if ((t != null) && t.EtdBerth.HasValue) return t.EtdBerth.Value < sfm.EtaFrom; if ((t != null) && t.EtdBerth.HasValue) return t.EtdBerth.Value < sfm.EtaFrom;
@ -689,7 +698,7 @@ namespace BreCalClient
Times? t = x.GetTimesForParticipantType(ParticipantType.AGENCY); Times? t = x.GetTimesForParticipantType(ParticipantType.AGENCY);
DateTime refValue = sfm.EtaTo.Value.AddMinutes(1439); // 23:59 DateTime refValue = sfm.EtaTo.Value.AddMinutes(1439); // 23:59
switch (x.Shipcall?.Type) switch (x.Shipcall?.Type)
{ {
case ShipcallType.Arrival: case ShipcallType.Arrival:
{ {
if ((t != null) && t.EtaBerth.HasValue) return t.EtaBerth.Value > refValue; if ((t != null) && t.EtaBerth.HasValue) return t.EtaBerth.Value > refValue;
@ -701,7 +710,7 @@ namespace BreCalClient
return x.Shipcall?.Etd > refValue; return x.Shipcall?.Etd > refValue;
} }
} }
}); });
} }
if(sfm.MineOnly ?? false) if(sfm.MineOnly ?? false)
@ -765,7 +774,7 @@ namespace BreCalClient
this.Dispatcher.Invoke(new Action(() => this.Dispatcher.Invoke(new Action(() =>
{ {
if (Interlocked.CompareExchange(ref _uiUpdateRunning, 1, 0) == 1) return; //if (Interlocked.CompareExchange(ref _uiUpdateRunning, 1, 0) == 1) return;
try try
{ {
@ -785,7 +794,7 @@ namespace BreCalClient
} }
finally finally
{ {
_uiUpdateRunning = 0; // _uiUpdateRunning = 0;
} }
Mouse.OverrideCursor = null; Mouse.OverrideCursor = null;
@ -991,6 +1000,6 @@ namespace BreCalClient
} }
#endregion #endregion
} }
} }