Skip to content

Commit

Permalink
Version bump and minor formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
stajs committed Mar 11, 2016
1 parent 87d2d26 commit 659dde8
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 68 deletions.
5 changes: 3 additions & 2 deletions LIC.Malone.Client.Desktop/ViewModels/AppViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ private void RunAuthAutoRefreshServices()
if (_autoRefreshAuthService != null)
_autoRefreshAuthService.UpdateTokens(Tokens.ToArray());
};

Tokens.PropertyChanged += (sender, args) =>
{
if (_autoRefreshAuthService != null)
Expand All @@ -478,8 +479,8 @@ private void RunAuthAutoRefreshServices()
private async void CheckForUpdates()
{
/*
Here be dragons!
Warning: Be *really* careful with changes to the updating code because you could break installed clients from
********* WARNING: Here be dragons! *********
Be *really* careful with changes to the updating code because you could break installed clients from
updating. Ensure any changes are tested thoroughly.
*/

Expand Down
38 changes: 18 additions & 20 deletions LIC.Malone.Core/Services/AutoRefreshAuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
using Caliburn.Micro;
using LIC.Malone.Core.Authentication.OAuth;
using LIC.Malone.Core.Scheduling;
using Action = System.Action;

namespace LIC.Malone.Core.Services
{
/// <summary>
/// A long-living backend service which is trying to refresh the expired tokens
/// and the soon-to-expire tokens
/// A long-living backend service which is trying to refresh expired/soon-to-expire tokens.
/// </summary>
public class AutoRefreshAuthService : IAutoRefreshAuthService
{
Expand All @@ -21,12 +19,12 @@ public class AutoRefreshAuthService : IAutoRefreshAuthService
private RepeatingTimerTask _task;

/// <summary>
/// How often does this service refresh all tokens
/// How often does this service refresh all tokens.
/// </summary>
public double RepeatingIntervalMinutes = 1;

/// <summary>
/// Refresh the tokens which is about to expire within this time frame
/// Refresh the tokens which is about to expire within this time frame.
/// </summary>
public TimeSpan RefreshTimeFrame = TimeSpan.FromMinutes(3);

Expand Down Expand Up @@ -73,28 +71,28 @@ private DateTime InternalCheckAuthExpiration()

var expirationUtc = token.AuthorizationState.AccessTokenExpirationUtc;

var expiraionOffset = expirationUtc - DateTime.UtcNow;
if (expiraionOffset < TimeSpan.Zero || expiraionOffset < RefreshTimeFrame)
{
var app =
_authApplications.FirstOrDefault(a => a.ClientIdentifier == token.AuthorizationState.GetClientIdentifier());
if (app == null) continue;
var expirationOffset = expirationUtc - DateTime.UtcNow;

var result = app.Refresh(token.Url, token.AuthorizationState);
if (!(expirationOffset < TimeSpan.Zero) && !(expirationOffset < RefreshTimeFrame))
continue;

if (result != null && !result.HasError)
{
NotifyTokeChange(new AuthRefreshEventArgs(token));
}
}
var app = _authApplications.FirstOrDefault(a => a.ClientIdentifier == token.AuthorizationState.GetClientIdentifier());

if (app == null)
continue;

var result = app.Refresh(token.Url, token.AuthorizationState);

if (result != null && !result.HasError)
NotifyTokenChange(new AuthRefreshEventArgs(token));
}

return DateTime.UtcNow.AddMinutes(RepeatingIntervalMinutes);
}

private void NotifyTokeChange(AuthRefreshEventArgs args)
private void NotifyTokenChange(AuthRefreshEventArgs args)
{
if(TokenChanged == null)
if (TokenChanged == null)
return;

Execute.OnUIThread(() =>
Expand All @@ -103,4 +101,4 @@ private void NotifyTokeChange(AuthRefreshEventArgs args)
});
}
}
}
}
5 changes: 2 additions & 3 deletions LIC.Malone.Core/Services/IAutoRefreshAuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ public interface IAutoRefreshAuthService
{
event AuthRefreshEventHandler TokenChanged;

void UpdateTokens(params NamedAuthorizationState[] tokens);

void UpdateTokens(params NamedAuthorizationState[] tokens);
void Start();
void Stop();
}
}
}
12 changes: 4 additions & 8 deletions LIC.Malone.Core/Services/ITaskSchedulerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@ namespace LIC.Malone.Core.Services
public interface ITaskSchedulerService
{
int TaskCount();

void Add(TimerTask task);
TimerTask Add(DateTime runAt, Action action);
TimerTask Add(TimeSpan runAt, Action action);
TimerTask Add(DateTime runAt, Action action);
TimerTask Add(TimeSpan runAt, Action action);
TimerTask Add(double milliseconds, Action action);
RepeatingTimerTask Add(DateTime runAt, Func<DateTime> action, TimeSpan errorDelay);
RepeatingTimerTask AddRepeatingTask(Func<DateTime> action);
RepeatingTimerTask AddRepeatingTask(Func<DateTime> action);
RepeatingTimerTask AddRepeatingTask(Func<DateTime> action, TimeSpan initialDelay);

void CheckTasksNow();

bool Remove(TimerTask timerTask);

void Start();
void Stop();
}
}
}
64 changes: 32 additions & 32 deletions LIC.Malone.Core/Services/TaskSchedulerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public virtual void Start()
{
Name = "NewTimerScheduler",
IsBackground = true,
// Important that this stays on the normal priority.
Priority = ThreadPriority.Normal
};

// important that this stays on the normal priority

_mt.Start();
}

Expand Down Expand Up @@ -99,10 +99,11 @@ public TimerTask Add(DateTime runAt, Action action)
{
if (action == null)
throw new ArgumentNullException("action");
if ((runAt.Subtract(DateTime.UtcNow)) < MaxPassed)
throw new InvalidOperationException("runAT is too far in the past.");

TimerTask to = new TimerTask(action, runAt);
if (runAt.Subtract(DateTime.UtcNow) < MaxPassed)
throw new InvalidOperationException("runAt is too far in the past.");

var to = new TimerTask(action, runAt);

Add(to);

Expand All @@ -113,8 +114,9 @@ public RepeatingTimerTask Add(DateTime runAt, Func<DateTime> action, TimeSpan er
{
if (action == null)
throw new ArgumentNullException("action");
if ((runAt.Subtract(DateTime.UtcNow)) < MaxPassed)
throw new InvalidOperationException("runAT is too far in the past.");

if (runAt.Subtract(DateTime.UtcNow) < MaxPassed)
throw new InvalidOperationException("runAt is too far in the past.");

var to = new RepeatingTimerTask(action, runAt, errorDelay);

Expand All @@ -133,6 +135,7 @@ public void Add(TimerTask task)
}
_taskListSorted.Add(task.runDate, task);
}

WaitEvent.Set();
}

Expand All @@ -153,20 +156,22 @@ public bool Remove(TimerTask timerTask)

lock (_sync)
{
int index = _taskListSorted.IndexOfValue(timerTask);
var index = _taskListSorted.IndexOfValue(timerTask);

if (index > -1)
{
_taskListSorted.RemoveAt(index);
Interlocked.Exchange(ref timerTask.completed, 1);
return true;
}

return false;
}
}

private void ReaddRepeatingTask(RepeatingTimerTask task)
private void ReAddRepeatingTask(RepeatingTimerTask task)
{
// reset the task
// Reset the task.
task.Reset();
Add(task);
}
Expand All @@ -183,80 +188,76 @@ private void Scheduler()
{
try
{
// if there are no tasks
// If there are no tasks.
while (_taskListSorted.Count == 0)
WaitEvent.WaitOne();

// No task is ready to run, so wait for timespan.
TimeSpan waitTill = RunCurrentTasks();
var waitTill = RunCurrentTasks();

if (waitTill == TimeSpan.Zero || waitTill.TotalMilliseconds <= 0)
{
continue;
}

DetectTimeShift();

_lastRunTime = DateTime.UtcNow;

// wait until the next event is scheduled
// Wait until the next event is scheduled.
WaitEvent.WaitOne((int)waitTill.TotalMilliseconds, false);
}
catch (ThreadAbortException ex)
{
throw ex;
}
catch (Exception ex)
catch
{
}
}
}

/// <summary>
/// Check if a time shift has occured, this will be due to the NTP server or manual intervention
/// Check if a time shift has occured, this will be due to the NTP server or manual intervention.
/// </summary>
private void DetectTimeShift()
{
//find the difference in time since the last run
// Find the difference in time since the last run.
var difference = DateTime.UtcNow - _lastRunTime;

if (Math.Abs(difference.TotalSeconds) > 60.0)
{
if (difference < TimeSpan.Zero)
{
//we went back in time, run all tasks once
// We went back in time, run all tasks once.

var currentTaskList = _taskListSorted.ToList();

// remove all tasks, will readded as part of the execution
// Remove all tasks, will be re-added as part of the execution.
_taskListSorted.Clear();

foreach (var task in currentTaskList)
ExecuteTimerTask(task.Value);
}
else if (difference > TimeSpan.Zero)
{
//we went forward in time
// We went forward in time.
}
}
}




/// <summary>
/// Runs any current tasks returns the time to wait until the next task
/// Runs any current tasks returns the time to wait until the next task.
/// </summary>
/// <returns></returns>
protected TimeSpan RunCurrentTasks()
{
// check we still have tasks to run
// Check we still have tasks to run.
while (_taskListSorted.Count > 0 && _taskListSorted.Values[0].RunDate <= DateTime.UtcNow)
{
TimerTask firstTask = null;

lock (_sync)
{
// if there are no tasks to run
// If there are no tasks to run.
if (_taskListSorted.Count == 0)
return TimeSpan.Zero;

Expand All @@ -268,7 +269,7 @@ protected TimeSpan RunCurrentTasks()
_taskListSorted.RemoveAt(0);
}

// if first has been canceled, remove it without running.
// If first has been canceled, remove it without running.
if (firstTask.Completed)
{
Debug.WriteLine("Cancelled task found");
Expand All @@ -287,7 +288,7 @@ protected TimeSpan RunCurrentTasks()

lock (_sync)
{
// determine the next run date
// Determine the next run date.
return _taskListSorted.Values.First().RunDate.Subtract(DateTime.UtcNow);
}
}
Expand All @@ -307,13 +308,12 @@ private void ExecuteTimerTask(TimerTask task)
}

if (task is RepeatingTimerTask)
ReaddRepeatingTask(task as RepeatingTimerTask);
ReAddRepeatingTask(task as RepeatingTimerTask);
}

public void Dispose()
{
Stop();
}
}

}
}
6 changes: 3 additions & 3 deletions Solution Items/SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
// http://blog.ploeh.dk/2013/12/10/semantic-versioning-with-continuous-deployment/
// http://docs.nuget.org/create/nuspec-reference#Replacement_Tokens

[assembly: AssemblyVersion("0.9.5")]
[assembly: AssemblyFileVersion("0.9.5")]
[assembly: AssemblyInformationalVersion("0.9.5")]
[assembly: AssemblyVersion("0.9.6")]
[assembly: AssemblyFileVersion("0.9.6")]
[assembly: AssemblyInformationalVersion("0.9.6")]

0 comments on commit 659dde8

Please sign in to comment.