Skip to content

Commit eb4fc84

Browse files
committed
code cleanup
1 parent 949eba7 commit eb4fc84

10 files changed

+57
-70
lines changed

sample/CrontabSample.ClassLibrary/Schedulers/TestScheduler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static void Task4(DateTime time, CrontabTask task, CrontabTaskExecutor ta
4646
Debug.WriteLine($"Task..............Cron_{time}_{task.Method.Name}_{taskExecutor.Tasks.Count}");
4747
}
4848

49-
[Cron("0/1 * * * * *", 100, CronStringFormat.WithSeconds)]
49+
[Cron("0/1 * * * * *", 100, CronStringFormat.WithSeconds)]
5050
public static void DeferTask1()
5151
{
5252
Debug.WriteLine($"Task..............5555_{DateTime.Now}");

src/Yisoft.Crontab/CronStringFormat.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,27 @@
1616
namespace Yisoft.Crontab
1717
{
1818
/// <summary>
19-
/// The cron string format to use during parsing
19+
/// The cron string format to use during parsing
2020
/// </summary>
2121
public enum CronStringFormat
2222
{
2323
/// <summary>
24-
/// Defined as "MINUTES HOURS DAYS MONTHS DAYS-OF-WEEK"
24+
/// Defined as "MINUTES HOURS DAYS MONTHS DAYS-OF-WEEK"
2525
/// </summary>
2626
Default = 0,
2727

2828
/// <summary>
29-
/// Defined as "MINUTES HOURS DAYS MONTHS DAYS-OF-WEEK YEARS"
29+
/// Defined as "MINUTES HOURS DAYS MONTHS DAYS-OF-WEEK YEARS"
3030
/// </summary>
3131
WithYears = 1,
3232

3333
/// <summary>
34-
/// Defined as "SECONDS MINUTES HOURS DAYS MONTHS DAYS-OF-WEEK"
34+
/// Defined as "SECONDS MINUTES HOURS DAYS MONTHS DAYS-OF-WEEK"
3535
/// </summary>
3636
WithSeconds = 2,
3737

3838
/// <summary>
39-
/// Defined as "SECONDS MINUTES HOURS DAYS MONTHS DAYS-OF-WEEK YEARS"
39+
/// Defined as "SECONDS MINUTES HOURS DAYS MONTHS DAYS-OF-WEEK YEARS"
4040
/// </summary>
4141
WithSecondsAndYears = 3
4242
}

src/Yisoft.Crontab/CrontabSchedule.cs

+3-6
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ private static List<ICronFilter> _ParseField(string field, CrontabFieldKind kind
304304
catch (Exception e)
305305
{
306306
throw new CrontabException(
307-
string.Format("There was an error parsing '{0}' for the {1} field", field,
308-
Enum.GetName(typeof(CrontabFieldKind), kind)), e);
307+
$"There was an error parsing '{field}' for the {Enum.GetName(typeof(CrontabFieldKind), kind)} field", e);
309308
}
310309
}
311310

@@ -426,8 +425,7 @@ private static int _GetValue(ref string filter, CrontabFieldKind kind)
426425
var returnValue = value;
427426

428427
if (returnValue > maxValue)
429-
throw new CrontabException(string.Format("Value for {0} filter exceeded maximum value of {1}",
430-
Enum.GetName(typeof(CrontabFieldKind), kind), maxValue));
428+
throw new CrontabException($"Value for {Enum.GetName(typeof(CrontabFieldKind), kind)} filter exceeded maximum value of {maxValue}");
431429

432430
return returnValue;
433431
}
@@ -458,8 +456,7 @@ private static int _GetValue(ref string filter, CrontabFieldKind kind)
458456
var returnValue = replaceVal.First().Value;
459457

460458
if (returnValue > maxValue)
461-
throw new CrontabException(string.Format("Value for {0} filter exceeded maximum value of {1}",
462-
Enum.GetName(typeof(CrontabFieldKind), kind), maxValue));
459+
throw new CrontabException($"Value for {Enum.GetName(typeof(CrontabFieldKind), kind)} filter exceeded maximum value of {maxValue}");
463460

464461
return returnValue;
465462
}

src/Yisoft.Crontab/CrontabTaskExecutor.cs

+34-28
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ private Action<DateTime> _CreateAction(CrontabTask task)
7676
{
7777
if (task == null) throw new ArgumentNullException(nameof(task));
7878

79-
var method = task.Method;
80-
var typeInstance = _typeInstanceCreator(method);
79+
var typeInstance = _typeInstanceCreator(task.Method);
8180

8281
return time =>
8382
{
@@ -88,34 +87,41 @@ private Action<DateTime> _CreateAction(CrontabTask task)
8887
return;
8988
}
9089

91-
try
92-
{
93-
task.LastExecuteTime = time;
94-
task.Status = CrontabTaskStatus.Running;
95-
96-
switch (task.Parameters.Length)
97-
{
98-
case 0:
99-
method.Invoke(typeInstance, null);
100-
break;
101-
case 1:
102-
method.Invoke(typeInstance, new object[] {time});
103-
break;
104-
case 2:
105-
method.Invoke(typeInstance, new object[] {time, task});
106-
break;
107-
case 3:
108-
method.Invoke(typeInstance, new object[] {time, task, this});
109-
break;
110-
default:
111-
throw new ArgumentException("the number of task parameters is incorrect.");
112-
}
113-
}
114-
catch
90+
_TryRun(time, task, typeInstance);
91+
};
92+
}
93+
94+
private void _TryRun(DateTime time, CrontabTask task, object typeInstance)
95+
{
96+
var method = task.Method;
97+
98+
task.LastExecuteTime = time;
99+
task.Status = CrontabTaskStatus.Running;
100+
101+
try
102+
{
103+
switch (task.Parameters.Length)
115104
{
116-
task.Status = CrontabTaskStatus.Failing;
105+
case 0:
106+
method.Invoke(typeInstance, null);
107+
break;
108+
case 1:
109+
method.Invoke(typeInstance, new object[] {time});
110+
break;
111+
case 2:
112+
method.Invoke(typeInstance, new object[] {time, task});
113+
break;
114+
case 3:
115+
method.Invoke(typeInstance, new object[] {time, task, this});
116+
break;
117+
default:
118+
throw new ArgumentException("the number of task parameters is incorrect.");
117119
}
118-
};
120+
}
121+
catch
122+
{
123+
task.Status = CrontabTaskStatus.Failing;
124+
}
119125
}
120126
}
121127
}

src/Yisoft.Crontab/Filters/AnyFilter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
namespace Yisoft.Crontab.Filters
1919
{
2020
/// <summary>
21-
/// Handles the filter instance where the user specifies a * (for any value)
21+
/// Handles the filter instance where the user specifies a * (for any value)
2222
/// </summary>
2323
public class AnyFilter : ICronFilter, ITimeFilter
2424
{

src/Yisoft.Crontab/Filters/RangeFilter.cs

+5-13
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,12 @@ public RangeFilter(int start, int end, int? steps, CrontabFieldKind kind)
3838
var maxValue = Constants.MaximumDateTimeValues[kind];
3939

4040
if (start < 0 || start > maxValue)
41-
throw new CrontabException(string.Format("Start = {0} is out of bounds for <{1}> field", start,
42-
Enum.GetName(typeof(CrontabFieldKind), kind)));
41+
throw new CrontabException($"Start = {start} is out of bounds for <{Enum.GetName(typeof(CrontabFieldKind), kind)}> field");
4342

44-
if (end < 0 || end > maxValue)
45-
throw new CrontabException(string.Format("End = {0} is out of bounds for <{1}> field", end,
46-
Enum.GetName(typeof(CrontabFieldKind), kind)));
43+
if (end < 0 || end > maxValue) throw new CrontabException($"End = {end} is out of bounds for <{Enum.GetName(typeof(CrontabFieldKind), kind)}> field");
4744

4845
if (steps != null && (steps <= 0 || steps > maxValue))
49-
throw new CrontabException(string.Format("Steps = {0} is out of bounds for <{1}> field", steps,
50-
Enum.GetName(typeof(CrontabFieldKind), kind)));
46+
throw new CrontabException($"Steps = {steps} is out of bounds for <{Enum.GetName(typeof(CrontabFieldKind), kind)}> field");
5147

5248
Start = start;
5349
End = end;
@@ -144,18 +140,14 @@ public int First()
144140

145141
while (newValue < max && !_IsMatch(newValue)) newValue++;
146142

147-
if (newValue > max)
148-
throw new CrontabException(string.Format("Next value for {0} on field {1} could not be found!",
149-
ToString(),
150-
Enum.GetName(typeof(CrontabFieldKind), Kind))
151-
);
143+
if (newValue > max) throw new CrontabException($"Next value for {ToString()} on field {Enum.GetName(typeof(CrontabFieldKind), Kind)} could not be found!");
152144

153145
_firstCache = newValue;
154146
return newValue;
155147
}
156148

157149
private bool _IsMatch(int evalValue) { return evalValue >= Start && evalValue <= End && (!Steps.HasValue || (evalValue - Start) % Steps == 0); }
158150

159-
public override string ToString() { return Steps.HasValue ? string.Format("{0}-{1}/{2}", Start, End, Steps) : string.Format("{0}-{1}", Start, End); }
151+
public override string ToString() { return Steps.HasValue ? string.Format("{0}-{1}/{2}", Start, End, Steps) : $"{Start}-{End}"; }
160152
}
161153
}

src/Yisoft.Crontab/Filters/SpecificDayOfWeekInMonthFilter.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ public class SpecificDayOfWeekInMonthFilter : ICronFilter
3333
/// <param name="kind">The crontab field kind to associate with this filter</param>
3434
public SpecificDayOfWeekInMonthFilter(int dayOfWeek, int weekNumber, CrontabFieldKind kind)
3535
{
36-
if (weekNumber <= 0 || weekNumber > 5) throw new CrontabException(string.Format("Week number = {0} is out of bounds.", weekNumber));
36+
if (weekNumber <= 0 || weekNumber > 5) throw new CrontabException($"Week number = {weekNumber} is out of bounds.");
3737

38-
if (kind != CrontabFieldKind.DayOfWeek)
39-
throw new CrontabException(string.Format("<{0}#{1}> can only be used in the Day of Week field.", dayOfWeek,
40-
weekNumber));
38+
if (kind != CrontabFieldKind.DayOfWeek) throw new CrontabException($"<{dayOfWeek}#{weekNumber}> can only be used in the Day of Week field.");
4139

4240
DayOfWeek = dayOfWeek;
4341
_dateTimeDayOfWeek = dayOfWeek.ToDayOfWeek();
@@ -80,6 +78,6 @@ public bool IsMatch(DateTime value)
8078
return value.Day == currentDay.Day;
8179
}
8280

83-
public override string ToString() { return string.Format("{0}#{1}", DayOfWeek, WeekNumber); }
81+
public override string ToString() { return $"{DayOfWeek}#{WeekNumber}"; }
8482
}
8583
}

src/Yisoft.Crontab/Filters/SpecificYearFilter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace Yisoft.Crontab.Filters
1717
{
1818
/// <summary>
19-
/// Handles filtering for a specific value
19+
/// Handles filtering for a specific value
2020
/// </summary>
2121
public class SpecificYearFilter : SpecificFilter
2222
{

src/Yisoft.Crontab/Filters/StepFilter.cs

+3-9
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ public StepFilter(int start, int step, CrontabFieldKind kind)
3939
{
4040
var maxValue = Constants.MaximumDateTimeValues[kind];
4141

42-
if (step <= 0 || step > maxValue)
43-
throw new CrontabException(string.Format("Steps = {0} is out of bounds for <{1}> field", step,
44-
Enum.GetName(typeof(CrontabFieldKind), kind)));
42+
if (step <= 0 || step > maxValue) throw new CrontabException($"Steps = {step} is out of bounds for <{Enum.GetName(typeof(CrontabFieldKind), kind)}> field");
4543

4644
Start = start;
4745
Step = step;
@@ -134,11 +132,7 @@ public int First()
134132

135133
while (newValue < max && !_IsMatch(newValue)) newValue++;
136134

137-
if (newValue > max)
138-
throw new CrontabException(string.Format("Next value for {0} on field {1} could not be found!",
139-
ToString(),
140-
Enum.GetName(typeof(CrontabFieldKind), Kind))
141-
);
135+
if (newValue > max) throw new CrontabException($"Next value for {ToString()} on field {Enum.GetName(typeof(CrontabFieldKind), Kind)} could not be found!");
142136

143137
_firstCache = newValue;
144138

@@ -147,6 +141,6 @@ public int First()
147141

148142
private bool _IsMatch(int evalValue) { return evalValue >= Start && (evalValue - Start) % Step == 0; }
149143

150-
public override string ToString() { return string.Format("{0}/{1}", Start == 0 ? "*" : Start.ToString(), Step); }
144+
public override string ToString() { return $"{(Start == 0 ? "*" : Start.ToString())}/{Step}"; }
151145
}
152146
}

src/Yisoft.Crontab/ICronFilter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public interface ICronFilter
2222
CrontabFieldKind Kind { get; }
2323

2424
/// <summary>
25-
/// Checks if the value is accepted by the filter
25+
/// Checks if the value is accepted by the filter
2626
/// </summary>
2727
/// <param name="value">The value to check</param>
2828
/// <returns>True if the value matches the condition, False if it does not match.</returns>

0 commit comments

Comments
 (0)