using Serilog; using System; using System.Linq; using System.Threading; using System.Threading.Tasks; using TINK.Repository.Exception; namespace TINK.ViewModel { /// /// Object to periodically actuate an action. /// public class PollingUpdateTask { /// Object to control canelling. private CancellationTokenSource CanellationTokenSource { get; } /// Task to perform update. private Task UpdateTask { get; } /// Action which performs an update. private Action UpdateAction { get; } /// Obects which does a periodic update. /// Update action to perform. /// Holds whether to poll or not and the periode leght is polling is on. public PollingUpdateTask( Action updateAction, TimeSpan? polling) { UpdateAction = updateAction ?? throw new ArgumentException($"Can not construct {GetType().Name}- obect. Argument {nameof(updateAction)} must not be null."); if (!polling.HasValue) { // Automatic update is switched off. Log.ForContext().Debug($"Automatic update is off, context {GetType().Name} at {DateTime.Now}."); return; } var updatePeriodeSet = polling.Value; CanellationTokenSource = new CancellationTokenSource(); int cycleIndex = 2; UpdateTask = Task.Run( async () => { while (!CanellationTokenSource.IsCancellationRequested) { await Task.Delay(updatePeriodeSet, CanellationTokenSource.Token); { // N. update cycle Log.ForContext().Information($"Actuating {cycleIndex} update cycle, context {GetType().Name} at {DateTime.Now}."); UpdateAction(); cycleIndex = cycleIndex < int.MaxValue ? ++cycleIndex : 0; } } }, CanellationTokenSource.Token); } /// /// Invoked when pages is closed/ hidden. /// Stops update process. /// public async Task Terminate() { if (UpdateTask == null) { // Nothing to do if no task was created. return; } // Cancel update task; if (CanellationTokenSource == 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}."); CanellationTokenSource.Cancel(); try { await UpdateTask; } catch (TaskCanceledException) { // Polling update was canceled. // Nothing to notice/ worry about. } catch (Exception exception) { // An error occurred updating pin. var aggregateException = exception as AggregateException; if (aggregateException == null) { // Unexpected exception detected. Exception should alyways be of type AggregateException Log.Error("An/ several errors occurred on update task. {@Exceptions}.", exception); } else { if (aggregateException.InnerExceptions.Count == 1 && aggregateException.InnerExceptions[0].GetType() == typeof(TaskCanceledException)) { // Polling update was canceled. // Nothing to notice/ worry about. } else if (aggregateException.InnerExceptions.Count() > 0 && aggregateException.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}.", exception); } else { // All exceptions were caused by communication errors. Log.Error("An/ several exceptions occurred on update task. {@Exception}.", exception); } } } } } }