annotate ServerMonitor/Objects/Schedule.cs @ 17:68d7834dc28e

More comments.
author Brad Greco <brad@bgreco.net>
date Sat, 25 May 2019 15:14:26 -0400
parents d92176c5398a
children 2342e9459444
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
2
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
3 namespace ServerMonitorApp
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
4 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
5 /// <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
6 public class Schedule
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
7 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
8 // 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
9 public Schedule() { }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
10
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
11 /// <summary>Schedule constructor</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
12 /// <param name="units">The time units to use.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
13 /// <param name="frequency">How frequently to run the check.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
14 /// <param name="startTime">Time of day the check should begin running.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
15 /// <param name="endTime">Time of day the check should stop running.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
16 /// <remarks>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
17 /// If endTime is before startTime, then the endTime is interpreted as being on the next day.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
18 /// For example, if startTime = 17:00 and endTime is 8:00, then the check will start running
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
19 /// at 17:00 and stop running at 8:00 the next day.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
20 /// </remarks>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
21 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
22 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
23 Units = units;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
24 Frequency = frequency;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
25 StartTime = startTime;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
26 EndTime = endTime;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
27 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
28
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
29 /// <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
30 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
31
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
32 /// <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
33 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
34
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
35 /// <summary>Time of day the check should begin running.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
36 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
37
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
38 /// <summary>Time of day the check should stop running.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
39 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
40
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
41 /// <summary>Given the last time a check was scheduled to run, calculates the next time in the future it should run.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
42 /// <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
43 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
44 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
45 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
46 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
47
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
48 /// <summary>Given the last time a check was scheduled to run, calculates the next time after the given date it should run.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
49 /// <param name="lastScheduledTime">The last time a check was scheduled to run.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
50 /// <param name="minStartTime">The earliest allowed time to return.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
51 /// <remarks>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
52 /// The next execution time of a check cannot necessarily be determined by taking the
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
53 /// last execution time and adding the configured number of time units. The computer might
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
54 /// have been asleep or the program might have not been running, so the next date might
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
55 /// be in the past.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
56 ///
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
57 /// To best follow the schedule, we take the last execution time and fast-forward time
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
58 /// by adding the configured time interval until we get a resulting time that is in the future.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
59 /// For example, suppose a check is scheduled to run every 5 minutes starting at 7:00.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
60 /// The check last ran at 7:40, and the computer was suspended shortly thereafter and resumed
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
61 /// at 8:28. The next execution time is determined by adding 5 minutes to 7:40 until we obtain
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
62 /// a time after 8:28, in this case, 8:30.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
63 /// </remarks>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
64 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
65 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
66 // 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
67 DateTime nextTime = lastScheduledTime;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
68 if (Units == FrequencyUnits.Day)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
69 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
70 // 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
71 while (nextTime < minStartTime)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
72 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
73 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
74 else
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
75 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
76 // 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
77 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
78 nextTime = minStartTime.Date.Add(StartTime).AddHours(-24);
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
79 // 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
80 while (nextTime < minStartTime)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
81 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
82 switch (Units)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
83 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
84 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
85 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
86 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
87 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
88 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
89 }
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
90 // Now we have the next date and time, but we don't know yet if it is within
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
91 // 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
92 if (StartTime < EndTime)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
93 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
94 // The allowed start and end times are on the same day.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
95 // If the next scheduled time is too early (before the allowed start time),
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
96 // 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
97 if (nextTime.TimeOfDay < StartTime)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
98 nextTime = nextTime.Date + StartTime;
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
99 // If the next scheduled time is too late (after the allowed end time),
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
100 // 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
101 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
102 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
103 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
104 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
105 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
106 // The allowed start time is on the day after the allowed end time,
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
107 // and the next scheduled time is too early (before the allowed start time).
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
108 // 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
109 nextTime = nextTime.Date + StartTime;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
110 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
111 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
112 return nextTime;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
113 }
12
d92176c5398a Better display of schedule name.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
114
d92176c5398a Better display of schedule name.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
115 public override string ToString()
d92176c5398a Better display of schedule name.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
116 {
d92176c5398a Better display of schedule name.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
117 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
118 }
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
119 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
120
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 12
diff changeset
121 /// <summary>Units of time that a check can be scheduled to run in intervals of.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
122 public enum FrequencyUnits { Second, Minute, Hour, Day }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
123 }