Mercurial > servermonitor
annotate ServerMonitor/Objects/Schedule.cs @ 39:7645122aa7a9
Get it working under Mono
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Tue, 09 Jun 2020 20:59:00 -0400 |
parents | 2342e9459444 |
children |
rev | line source |
---|---|
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
1 using System; |
32
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
2 using System.ComponentModel; |
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
3 using System.Xml.Serialization; |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
4 |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
5 namespace ServerMonitorApp |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
6 { |
17 | 7 /// <summary>Schedule to control when a check is automatically executed.<summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
8 public class Schedule |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
9 { |
17 | 10 // Required empty constructor for XML serialization. |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
11 public Schedule() { } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
12 |
17 | 13 /// <summary>Schedule constructor</summary> |
14 /// <param name="units">The time units to use.</param> | |
15 /// <param name="frequency">How frequently to run the check.</param> | |
16 /// <param name="startTime">Time of day the check should begin running.</param> | |
17 /// <param name="endTime">Time of day the check should stop running.</param> | |
18 /// <remarks> | |
19 /// If endTime is before startTime, then the endTime is interpreted as being on the next day. | |
20 /// For example, if startTime = 17:00 and endTime is 8:00, then the check will start running | |
21 /// at 17:00 and stop running at 8:00 the next day. | |
22 /// </remarks> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
23 public Schedule(FrequencyUnits units, int frequency, TimeSpan startTime, TimeSpan endTime) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
24 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
25 Units = units; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
26 Frequency = frequency; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
27 StartTime = startTime; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
28 EndTime = endTime; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
29 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
30 |
17 | 31 /// <summary>How often the check should be executed.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
32 public int Frequency { get; set; } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
33 |
17 | 34 /// <summary>The time units used to interpret the frequency.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
35 public FrequencyUnits Units { get; set; } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
36 |
17 | 37 /// <summary>Time of day the check should begin running.</summary> |
32
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
38 [XmlIgnore] |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
39 public TimeSpan StartTime { get; set; } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
40 |
17 | 41 /// <summary>Time of day the check should stop running.</summary> |
32
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
42 [XmlIgnore] |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
43 public TimeSpan EndTime { get; set; } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
44 |
32
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
45 /// <summary>Property to serialize StartTime since serializing a TimeSpan does not work by default.</summary> |
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
46 [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] |
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
47 public long StartTimeTicks |
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
48 { |
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
49 get { return StartTime.Ticks; } |
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
50 set { StartTime = new TimeSpan(value); } |
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
51 } |
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
52 |
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
53 /// <summary>Property to serialize EndTime since serializing a TimeSpan does not work by default.</summary> |
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
54 [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] |
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
55 public long EndTimeTicks |
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
56 { |
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
57 get { return EndTime.Ticks; } |
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
58 set { EndTime = new TimeSpan(value); } |
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
59 } |
2342e9459444
Fix schedule times not being saved.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
60 |
17 | 61 /// <summary>Given the last time a check was scheduled to run, calculates the next time in the future it should run.</summary> |
62 /// <param name="lastScheduledTime">The last time a check was scheduled to run.</param> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
63 public DateTime GetNextTime(DateTime lastScheduledTime) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
64 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
65 return GetNextTime(lastScheduledTime, DateTime.Now); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
66 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
67 |
17 | 68 /// <summary>Given the last time a check was scheduled to run, calculates the next time after the given date it should run.</summary> |
69 /// <param name="lastScheduledTime">The last time a check was scheduled to run.</param> | |
70 /// <param name="minStartTime">The earliest allowed time to return.</param> | |
71 /// <remarks> | |
72 /// The next execution time of a check cannot necessarily be determined by taking the | |
73 /// last execution time and adding the configured number of time units. The computer might | |
74 /// have been asleep or the program might have not been running, so the next date might | |
75 /// be in the past. | |
39 | 76 /// |
17 | 77 /// To best follow the schedule, we take the last execution time and fast-forward time |
78 /// by adding the configured time interval until we get a resulting time that is in the future. | |
79 /// For example, suppose a check is scheduled to run every 5 minutes starting at 7:00. | |
80 /// The check last ran at 7:40, and the computer was suspended shortly thereafter and resumed | |
81 /// at 8:28. The next execution time is determined by adding 5 minutes to 7:40 until we obtain | |
82 /// a time after 8:28, in this case, 8:30. | |
83 /// </remarks> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
84 public DateTime GetNextTime(DateTime lastScheduledTime, DateTime minStartTime) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
85 { |
17 | 86 // Start by setting the next time to the last time the check was run. |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
87 DateTime nextTime = lastScheduledTime; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
88 if (Units == FrequencyUnits.Day) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
89 { |
17 | 90 // If the check is scheduled only once a day, simply add days until we find a time in the future. |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
91 while (nextTime < minStartTime) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
92 nextTime = nextTime.AddDays(Frequency).Date.Add(StartTime); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
93 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
94 else |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
95 { |
17 | 96 // If the last run time was more than a day ago, fast-forward a day at a time to reduce the number of loops. |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
97 if (nextTime < minStartTime.AddHours(-24)) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
98 nextTime = minStartTime.Date.Add(StartTime).AddHours(-24); |
17 | 99 // Add the configured time interval to the last run time until we obtain a time in the future. |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
100 while (nextTime < minStartTime) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
101 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
102 switch (Units) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
103 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
104 case FrequencyUnits.Second: nextTime = nextTime.AddSeconds(Frequency); break; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
105 case FrequencyUnits.Minute: nextTime = nextTime.AddMinutes(Frequency); break; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
106 case FrequencyUnits.Hour: nextTime = nextTime.AddHours(Frequency); break; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
107 default: throw new InvalidOperationException("Unexpected frequency units: " + Units); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
108 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
109 } |
17 | 110 // Now we have the next date and time, but we don't know yet if it is within |
111 // the active times of day the check is allowed to run between. | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
112 if (StartTime < EndTime) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
113 { |
17 | 114 // The allowed start and end times are on the same day. |
115 // If the next scheduled time is too early (before the allowed start time), | |
116 // wait to run it until the start time happens. | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
117 if (nextTime.TimeOfDay < StartTime) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
118 nextTime = nextTime.Date + StartTime; |
17 | 119 // If the next scheduled time is too late (after the allowed end time), |
120 // wait to run it until the start time happens on the following day. | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
121 else if (nextTime.TimeOfDay > EndTime) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
122 nextTime = nextTime.Date.AddDays(1) + StartTime; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
123 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
124 else if (nextTime.TimeOfDay > EndTime && nextTime.TimeOfDay < StartTime) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
125 { |
17 | 126 // The allowed start time is on the day after the allowed end time, |
127 // and the next scheduled time is too early (before the allowed start time). | |
128 // Wait to run the check until the allowed start time happens. | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
129 nextTime = nextTime.Date + StartTime; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
130 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
131 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
132 return nextTime; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
133 } |
12
d92176c5398a
Better display of schedule name.
Brad Greco <brad@bgreco.net>
parents:
8
diff
changeset
|
134 |
d92176c5398a
Better display of schedule name.
Brad Greco <brad@bgreco.net>
parents:
8
diff
changeset
|
135 public override string ToString() |
d92176c5398a
Better display of schedule name.
Brad Greco <brad@bgreco.net>
parents:
8
diff
changeset
|
136 { |
d92176c5398a
Better display of schedule name.
Brad Greco <brad@bgreco.net>
parents:
8
diff
changeset
|
137 return string.Format("Every {0} {1}", Frequency, Units.ToString().ToLower() + (Frequency == 1 ? "" : "s")); |
d92176c5398a
Better display of schedule name.
Brad Greco <brad@bgreco.net>
parents:
8
diff
changeset
|
138 } |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
139 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
140 |
17 | 141 /// <summary>Units of time that a check can be scheduled to run in intervals of.</summary> |
39 | 142 public enum FrequencyUnits { Second = 0, Minute = 1, Hour = 2, Day = 3 } |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
143 } |