-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchedule.cs
132 lines (116 loc) · 5.31 KB
/
Schedule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Linq;
namespace Teto {
/// <summary>
/// A schedule for tasks to run in.
/// </summary>
public class Schedule {
/// <summary>
/// The years in which the schedule is allowed to run, or an empty array if all are allowed.
/// </summary>
public int[] AllowedYears { get; private set; }
/// <summary>
/// The months in which the schedule is allowed to run, or an empty array if all are allowed.
/// </summary>
public int[] AllowedMonths { get; private set; }
/// <summary>
/// The days of the week in which the schedule is allowed to run, or an empty array if all are allowed.
/// </summary>
public int[] AllowedDOWs { get; private set; }
/// <summary>
/// The days of the month in which the schedule is allowed to run, or an empty array if all are allowed.
/// </summary>
public int[] AllowedDays { get; private set; }
/// <summary>
/// The hours in which the schedule is allowed to run, or an empty array if all are allowed.
/// </summary>
public int[] AllowedHours { get; private set; }
/// <summary>
/// The minutes in which the schedule is allowed to run, or an empty array if all are allowed.
/// </summary>
public int[] AllowedMinutes { get; private set; }
/// <summary>
/// An action that can be run by the scheduler.
/// </summary>
public delegate void Action();
/// <summary>
/// The action to be run.
/// </summary>
private Action action;
/// <summary>
/// Construct a schedule from its string arguments and a delegate action.
/// </summary>
/// <param name="y">The string representation of the allowed years.</param>
/// <param name="mo">The string representation of the allowed months.</param>
/// <param name="dow">The string representation of the allowed days of the week.</param>
/// <param name="d">The string representation of the allowed days of the month.</param>
/// <param name="h">The string representation of the allowed hours.</param>
/// <param name="m">The string representation of the allowed minutes.</param>
/// <param name="action">The action to be run.</param>
public Schedule(string y, string mo, string dow, string d, string h, string m, Action action) {
AllowedYears = ParseArgument(y);
AllowedMonths = ParseArgument(mo);
AllowedDOWs = ParseArgument(dow);
AllowedDays = ParseArgument(d);
AllowedHours = ParseArgument(h);
AllowedMinutes = ParseArgument(m);
if (AllowedMinutes.Length == 0) {
The.Warning("The schedule's minutes parameter is set to post every minute. Double-check if that's what you intended?");
}
this.action = action;
}
/// <summary>
/// Parse a string argument to an integer array.
/// </summary>
/// <param name="argument">The string argument to be parsed (either *, a number, or comma-seperated numbers)</param>
/// <returns>The int[] of allowed values, an empty int[] if all are allowed, or an int[1] { -1 } if parsing failed.</returns>
static int[] ParseArgument(string argument) {
if (argument == "*") {
return new int[0];
}
if (argument.All(o => char.IsDigit(o))) {
return new int[1] { int.Parse(argument) };
}
if (argument.Contains(",")) {
List<int> args = new List<int>();
string[] pcs = argument.Split(',');
foreach (string pc in pcs) {
string tpc = pc.Trim();
if (!tpc.All(o => char.IsDigit(o))) {
continue;
}
args.Add(int.Parse(tpc));
}
return args.ToArray();
}
The.Failure($"Failed to parse schedule value '{ argument }'. The schedule will not run.");
return new int[] { -1 };
}
/// <summary>
/// Check whether the schedule is allowed to run now.
/// </summary>
/// <returns>True if the schedule may run.</returns>
public bool Test() {
DateTime d = DateTime.Now;
if (AllowedYears.Length > 0 && !AllowedYears.Contains(d.Year)) { return false; }
if (AllowedMonths.Length > 0 && !AllowedMonths.Contains(d.Month)) { return false; }
if (AllowedDOWs.Length > 0 && !AllowedDOWs.Contains((int)d.DayOfWeek)) { return false; }
if (AllowedDays.Length > 0 && !AllowedDays.Contains(d.Day)) { return false; }
if (AllowedHours.Length > 0 && !AllowedHours.Contains(d.Hour)) { return false; }
if (AllowedMinutes.Length > 0 && !AllowedMinutes.Contains(d.Minute)) { return false; }
return true;
}
/// <summary>
/// Attempt to run the schedule now.
/// </summary>
/// <returns>Whether the schedule has run.</returns>
public bool Run() {
if (Test()) {
action();
return true;
}
return false;
}
}
}