NTP Analyzer  0.8.2
Analyze the operation of time servers
JobScheduleDescription.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 
24 namespace Ntp.Common.Process
25 {
29  public sealed class JobScheduleDescription
30  {
37  public JobScheduleDescription(bool initialRun, bool fixedRun, int frequency)
38  {
39  InitialRun = initialRun || (frequency == 0);
40  FixedRun = fixedRun;
41  Frequency = frequency;
42  initialRunDone = false;
43  }
44 
45  private bool initialRunDone;
46 
52  public bool InitialRun { get; }
53 
59  public bool FixedRun { get; }
60 
65  public int Frequency { get; }
66 
71  public bool CanMove => !(InitialRun && !initialRunDone) && !FixedRun;
72 
78  public DateTime CalculateNextRun(DateTime start)
79  {
80  if (Frequency == -1)
81  return DateTime.MaxValue;
82  if (Frequency == 0 || (InitialRun && !initialRunDone))
83  return start;
84 
85  int multiplier = Convert.ToInt32(Math.Truncate((double) (start.Hour*60 + start.Minute)/Frequency));
86  int startMinutes = multiplier*Frequency;
87 
88  int hour = (startMinutes + Frequency)/60;
89  int minute = (startMinutes + Frequency)%60;
90 
91  if (hour >= 24)
92  {
93  hour -= 24;
94  start = start.AddDays(1);
95  }
96 
97  if (minute >= 60)
98  {
99  minute -= 60;
100  start = start.AddHours(1);
101  }
102 
103  var next = new DateTime(start.Year, start.Month, start.Day, hour, minute, 0);
104 
105  return next;
106  }
107 
115  public ScheduledJob CreateNew(Job job, DateTime start, double offset)
116  {
117  if (InitialRun && !initialRunDone)
118  {
119  initialRunDone = true;
120  return new ScheduledJob(job, start);
121  }
122 
123  return new ScheduledJob(job, CalculateNextRun(start).AddMinutes(offset));
124  }
125 
132  {
133  double offset = Frequency/100.0;
134 
135  if (offset > 2)
136  {
137  offset = 2;
138  }
139  else if (offset < 0.5)
140  {
141  offset = 0.5;
142  }
143 
144  return new ScheduledJob(job, DateTime.Now.AddMinutes(offset));
145  }
146  }
147 }
ScheduledJob CreatePostponed(Job job)
Creates a postponed job schedule.
JobScheduleDescription(bool initialRun, bool fixedRun, int frequency)
Initializes a new instance of the JobScheduleDescription class.
ScheduledJob CreateNew(Job job, DateTime start, double offset)
Creates a new scheduled job based on description and parameters.
A description of job schedule rules.
DateTime CalculateNextRun(DateTime start)
Calculates the time of next run according to description parameters.
A job which have been scheduled for execution.
Definition: ScheduledJob.cs:29