Changeset 151
- Timestamp:
- 3/8/2008 1:10:39 PM (5 years ago)
- File:
-
- 1 edited
-
branches/eraser6/Manager/Schedule.cs (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/eraser6/Manager/Schedule.cs
r147 r151 47 47 } 48 48 49 /// <summary> 50 /// Recurring runs schedule type. 51 /// </summary> 49 52 public class RecurringSchedule : Schedule 50 53 { … … 53 56 get { return "Recurring schedule, not implemented."; } 54 57 } 58 59 /// <summary> 60 /// The types of schedule 61 /// </summary> 62 public enum ScheduleUnit 63 { 64 /// <summary> 65 /// Daily schedule type 66 /// </summary> 67 DAILY, 68 69 /// <summary> 70 /// Weekdays-only schedule type 71 /// </summary> 72 WEEKDAYS, 73 74 /// <summary> 75 /// Weekly schedule type 76 /// </summary> 77 WEEKLY, 78 79 /// <summary> 80 /// Monthly schedule type 81 /// </summary> 82 MONTHLY 83 } 84 85 /// <summary> 86 /// The days of the week, with values usable in a bitfield. 87 /// </summary> 88 public enum DaysOfWeek 89 { 90 SUNDAY = 1, 91 MONDAY = 1 << 1, 92 TUESDAY = 1 << 2, 93 WEDNESDAY = 1 << 3, 94 THURSDAY = 1 << 4, 95 FRIDAY = 1 << 5, 96 SATURDAY = 1 << 6 97 } 98 99 /// <summary> 100 /// The type of schedule. 101 /// </summary> 102 public ScheduleUnit Type 103 { 104 get { return type; } 105 set { type = value; } 106 } 107 108 /// <summary> 109 /// The frequency of the event. This value is valid only with Daily, 110 /// Weekly and Monthly schedules. 111 /// </summary> 112 public uint Frequency 113 { 114 get 115 { 116 if (Type != ScheduleUnit.DAILY && Type != ScheduleUnit.WEEKLY && 117 Type != ScheduleUnit.MONTHLY) 118 throw new ArgumentException("The ScheduleUnit of the schedule does " + 119 "not require a frequency value, this field would contain garbage."); 120 121 return frequency; 122 } 123 set 124 { 125 if (value == 0) 126 throw new ArgumentException("The frequency of the recurrance should " + 127 "be greater than one"); 128 129 frequency = value; 130 } 131 } 132 133 /// <summary> 134 /// The days of the week which this task should be run. This is valid only 135 /// with Weekly schedules. This field is the DaysOfWeek enumerations 136 /// ORed together. 137 /// </summary> 138 public DaysOfWeek WeeklySchedule 139 { 140 get 141 { 142 if (Type != ScheduleUnit.WEEKLY) 143 throw new ArgumentException("The ScheduleUnit of the schedule does " + 144 "not require the WeeklySchedule value, this field would contain garbage"); 145 146 return weeklySchedule; 147 } 148 set 149 { 150 if (value == 0) 151 throw new ArgumentException("The WeeklySchedule should have at " + 152 "least one day where the task should be run."); 153 154 weeklySchedule = value; 155 } 156 } 157 158 /// <summary> 159 /// The nth day of the month on which this task will run. This is valid 160 /// only with Monthly schedules 161 /// </summary> 162 public uint MonthlySchedule 163 { 164 get 165 { 166 if (Type != ScheduleUnit.MONTHLY) 167 throw new ArgumentException("The ScheduleUnit of the schedule does " + 168 "not require the MonthlySchedule value, this field would contain garbage"); 169 170 return monthlySchedule; 171 } 172 set { monthlySchedule = value; } 173 } 174 175 /// <summary> 176 /// The last time this task was executed. This value is used for computing 177 /// the next time the task should be run. 178 /// </summary> 179 public DateTime LastRun 180 { 181 get { return lastRun; } 182 set { LastRun = value; } 183 } 184 185 /// <summary> 186 /// Based on the last run time and the current schedule, the next run time 187 /// will be computed. 188 /// </summary> 189 public DateTime NextRun 190 { 191 get 192 { 193 DateTime result = LastRun; 194 switch (Type) 195 { 196 case ScheduleUnit.DAILY: 197 while (result < DateTime.Now) 198 result = result.AddDays(frequency); 199 break; 200 case ScheduleUnit.WEEKDAYS: 201 while (result < DateTime.Now || 202 lastRun.DayOfWeek == DayOfWeek.Saturday || 203 lastRun.DayOfWeek == DayOfWeek.Sunday) 204 result = result.AddDays(1.0); 205 break; 206 case ScheduleUnit.WEEKLY: 207 //First find the next day of the week within this week. 208 for (DayOfWeek day = lastRun.DayOfWeek; day <= DayOfWeek.Sunday; ++day) 209 if (((int)weeklySchedule & (1 << (int)day)) != 0) 210 //Bullseye! Run the task next day in the week. 211 result = result.AddDays(day - lastRun.DayOfWeek); 212 213 //Ok, we now need to find the earliest day of the week where 214 //the task will run. 215 throw new NotImplementedException("Weekly schedule calculation not implemented"); 216 break; 217 case ScheduleUnit.MONTHLY: 218 //Increment the month until we are past our current date. 219 while (lastRun < DateTime.Now) 220 result = result.AddMonths((int)frequency); 221 result = result.AddDays(-((int)monthlySchedule - result.Day)); 222 break; 223 } 224 225 return result; 226 } 227 } 228 229 private ScheduleUnit type; 230 private uint frequency; 231 private DaysOfWeek weeklySchedule; 232 private uint monthlySchedule; 233 234 private DateTime lastRun; 55 235 } 56 236 }
Note: See TracChangeset
for help on using the changeset viewer.
