fix: Using UTC for tracking scheduled events

This commit is contained in:
Chris Cameron
2020-03-10 13:39:38 -04:00
parent 91684f25f2
commit ed2cf84a7e
4 changed files with 18 additions and 17 deletions

View File

@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed a bug where color formatted console output on Net Standard was not raising the OnConsolePrint event - Fixed a bug where color formatted console output on Net Standard was not raising the OnConsolePrint event
- Simplifying ANSI color methods, better cross-platform color support - Simplifying ANSI color methods, better cross-platform color support
- Console uses unicode for table drawing on Net Standard - Console uses unicode for table drawing on Net Standard
- Using UTC for tracking scheduled events, fixes issues with DST
## [10.3.0] - 2020-01-20 ## [10.3.0] - 2020-01-20
### Changed ### Changed

View File

@@ -7,17 +7,17 @@ namespace ICD.Common.Utils.Services.Scheduler
{ {
public event EventHandler OnScheduledRunTimeChanged; public event EventHandler OnScheduledRunTimeChanged;
private DateTime? m_NextRunTime; private DateTime? m_NextRunTimeUtc;
public DateTime? NextRunTime public DateTime? NextRunTimeUtc
{ {
get { return m_NextRunTime; } get { return m_NextRunTimeUtc; }
private set private set
{ {
if (m_NextRunTime == value) if (m_NextRunTimeUtc == value)
return; return;
m_NextRunTime = value; m_NextRunTimeUtc = value;
OnScheduledRunTimeChanged.Raise(this); OnScheduledRunTimeChanged.Raise(this);
} }
@@ -26,12 +26,12 @@ namespace ICD.Common.Utils.Services.Scheduler
public void Run() public void Run()
{ {
RunFinal(); RunFinal();
NextRunTime = GetNextRunTime(); NextRunTimeUtc = GetNextRunTimeUtc();
} }
public void UpdateNextRunTime() public void UpdateNextRunTime()
{ {
NextRunTime = GetNextRunTime(); NextRunTimeUtc = GetNextRunTimeUtc();
} }
/// <summary> /// <summary>
@@ -42,6 +42,6 @@ namespace ICD.Common.Utils.Services.Scheduler
/// <summary> /// <summary>
/// Runs after RunFinal in order to set the next run time of this action /// Runs after RunFinal in order to set the next run time of this action
/// </summary> /// </summary>
public abstract DateTime? GetNextRunTime(); public abstract DateTime? GetNextRunTimeUtc();
} }
} }

View File

@@ -39,7 +39,7 @@ namespace ICD.Common.Utils.Services.Scheduler
try try
{ {
Subscribe(action); Subscribe(action);
m_Actions.InsertSorted(action, a => a.NextRunTime); m_Actions.InsertSorted(action, a => a.NextRunTimeUtc);
} }
finally finally
{ {
@@ -96,15 +96,15 @@ namespace ICD.Common.Utils.Services.Scheduler
private void TimerCallback() private void TimerCallback()
{ {
DateTime currentTime = IcdEnvironment.GetLocalTime(); DateTime currentTime = IcdEnvironment.GetUtcTime();
IScheduledAction[] actionsToRun; IScheduledAction[] actionsToRun;
m_CriticalSection.Enter(); m_CriticalSection.Enter();
try try
{ {
actionsToRun = m_Actions actionsToRun = m_Actions
.Where(a => a.NextRunTime <= currentTime && a.NextRunTime > m_LastRunTime) .Where(a => a.NextRunTimeUtc <= currentTime && a.NextRunTimeUtc > m_LastRunTime)
.OrderBy(a => a.NextRunTime) .OrderBy(a => a.NextRunTimeUtc)
.ToArray(); .ToArray();
} }
finally finally
@@ -135,14 +135,14 @@ namespace ICD.Common.Utils.Services.Scheduler
m_CriticalSection.Enter(); m_CriticalSection.Enter();
try try
{ {
var action = m_Actions.FirstOrDefault(a => a.NextRunTime != null && a.NextRunTime > m_LastRunTime); var action = m_Actions.FirstOrDefault(a => a.NextRunTimeUtc != null && a.NextRunTimeUtc > m_LastRunTime);
if (action == null || action.NextRunTime == null) if (action == null || action.NextRunTimeUtc == null)
{ {
m_Timer.Stop(); m_Timer.Stop();
return; return;
} }
long msToNextAction = (long)(action.NextRunTime.Value - IcdEnvironment.GetLocalTime()).TotalMilliseconds; long msToNextAction = (long)(action.NextRunTimeUtc.Value - IcdEnvironment.GetUtcTime()).TotalMilliseconds;
if (msToNextAction < 0) if (msToNextAction < 0)
msToNextAction = 0; msToNextAction = 0;
m_Timer.Reset(msToNextAction); m_Timer.Reset(msToNextAction);
@@ -195,7 +195,7 @@ namespace ICD.Common.Utils.Services.Scheduler
try try
{ {
m_Actions.Remove(action); m_Actions.Remove(action);
m_Actions.InsertSorted(action, a => a.NextRunTime); m_Actions.InsertSorted(action, a => a.NextRunTimeUtc);
} }
finally finally
{ {

View File

@@ -12,7 +12,7 @@ namespace ICD.Common.Utils.Services.Scheduler
/// <summary> /// <summary>
/// Gets the next time this action should be run /// Gets the next time this action should be run
/// </summary> /// </summary>
DateTime? NextRunTime { get; } DateTime? NextRunTimeUtc { get; }
void Run(); void Run();
} }