mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-12-23 15:46:29 +01:00
126 lines
5.3 KiB
C#
126 lines
5.3 KiB
C#
using Serilog;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using TINK.Repository.Exception;
|
|
|
|
namespace TINK.ViewModel
|
|
{
|
|
public class PollingUpdateTask
|
|
{
|
|
/// <summary>Name of child class provider. </summary>
|
|
private readonly Func<String> m_oGetChildName;
|
|
|
|
/// <summary> Object to control canelling. </summary>
|
|
private readonly CancellationTokenSource m_oCanellationTokenSource;
|
|
|
|
/// <summary> Task to perform update. </summary>
|
|
private readonly Task m_oUpdateTask;
|
|
|
|
/// <summary> Action which performs an update.</summary>
|
|
private readonly Action m_oUpdateAction;
|
|
|
|
/// <summary> Obects which does a periodic update. </summary>
|
|
/// <param name="p_oGetChildName">Name of the child class. For logging purposes.</param>
|
|
/// <param name="p_oUpdateAction">Update action to perform.</param>
|
|
/// <param name="p_oPolling"> Holds whether to poll or not and the periode leght is polling is on. </param>
|
|
public PollingUpdateTask(
|
|
Func<String> p_oGetChildName,
|
|
Action p_oUpdateAction,
|
|
TimeSpan? p_oPolling)
|
|
{
|
|
m_oGetChildName = p_oGetChildName
|
|
?? throw new ArgumentException($"Can not construct {GetType().Name}- obect. Argument {nameof(p_oGetChildName)} must not be null.");
|
|
|
|
m_oUpdateAction = p_oUpdateAction
|
|
?? throw new ArgumentException($"Can not construct {GetType().Name}- obect. Argument {nameof(p_oUpdateAction)} must not be null.");
|
|
|
|
if (!p_oPolling.HasValue)
|
|
{
|
|
// Automatic update is switched off.
|
|
Log.ForContext<PollingUpdateTask>().Debug($"Automatic update is off, context {GetType().Name} at {DateTime.Now}.");
|
|
return;
|
|
}
|
|
|
|
var l_iUpdatePeriodeSet = p_oPolling.Value;
|
|
|
|
m_oCanellationTokenSource = new CancellationTokenSource();
|
|
|
|
int l_iCycleIndex = 2;
|
|
m_oUpdateTask = Task.Run(
|
|
async () =>
|
|
{
|
|
while (!m_oCanellationTokenSource.IsCancellationRequested)
|
|
{
|
|
await Task.Delay(l_iUpdatePeriodeSet, m_oCanellationTokenSource.Token);
|
|
{
|
|
// N. update cycle
|
|
Log.ForContext<PollingUpdateTask>().Information($"Actuating {l_iCycleIndex} update cycle, context {GetType().Name} at {DateTime.Now}.");
|
|
|
|
m_oUpdateAction();
|
|
|
|
l_iCycleIndex = l_iCycleIndex < int.MaxValue ? ++l_iCycleIndex : 0;
|
|
}
|
|
}
|
|
},
|
|
m_oCanellationTokenSource.Token);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invoked when pages is closed/ hidden.
|
|
/// Stops update process.
|
|
/// </summary>
|
|
public async Task Terminate()
|
|
{
|
|
// Cancel update task;
|
|
if (m_oCanellationTokenSource == null)
|
|
{
|
|
throw new Exception($"Can not terminate periodical update task, context {GetType().Name} at {DateTime.Now}. No task running.");
|
|
}
|
|
|
|
Log.Information($"Request to terminate update cycle, context {GetType().Name} at {DateTime.Now}.");
|
|
m_oCanellationTokenSource.Cancel();
|
|
|
|
try
|
|
{
|
|
await m_oUpdateTask;
|
|
}
|
|
catch (TaskCanceledException)
|
|
{
|
|
// Polling update was canceled.
|
|
// Nothing to notice/ worry about.
|
|
}
|
|
catch (Exception l_oException)
|
|
{
|
|
// An error occurred updating pin.
|
|
var l_oAggregateException = l_oException as AggregateException;
|
|
if (l_oAggregateException == null)
|
|
{
|
|
// Unexpected exception detected. Exception should alyways be of type AggregateException
|
|
Log.Error("An/ several errors occurred on update task. {@Exceptions}.", l_oException);
|
|
}
|
|
else
|
|
{
|
|
if (l_oAggregateException.InnerExceptions.Count == 1
|
|
&& l_oAggregateException.InnerExceptions[0].GetType() == typeof(TaskCanceledException))
|
|
{
|
|
// Polling update was canceled.
|
|
// Nothing to notice/ worry about.
|
|
}
|
|
else if (l_oAggregateException.InnerExceptions.Count() > 0 &&
|
|
l_oAggregateException.InnerExceptions.First(x => !x.GetIsConnectFailureException()) == null) // There is no exception which is not of type connect failure.
|
|
{
|
|
// All exceptions were caused by communication error
|
|
Log.Information("An/ several web related connect failure exceptions occurred on update task. {@Exceptions}.", l_oException);
|
|
}
|
|
else
|
|
{
|
|
// All exceptions were caused by communication errors.
|
|
Log.Error("An/ several exceptions occurred on update task. {@Exception}.", l_oException);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|