Skip to content

Commit f6384d1

Browse files
committed
Refactor scheduling API
1 parent b460a97 commit f6384d1

File tree

3 files changed

+94
-123
lines changed

3 files changed

+94
-123
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using Quartz;
3+
using Quartz.Impl;
4+
5+
namespace InEngine.Core.Scheduling
6+
{
7+
public class Occurence
8+
{
9+
public IScheduler Scheduler { get; set; } = StdSchedulerFactory.GetDefaultScheduler();
10+
public AbstractCommand Command { get; set; }
11+
public IJobDetail JobDetail { get; set; }
12+
13+
public static TriggerBuilder MakeTriggerBuilder(AbstractCommand command)
14+
{
15+
if (command == null)
16+
throw new ArgumentNullException(command.GetType().Name, "The command to schedule cannot be null.");
17+
return TriggerBuilder
18+
.Create()
19+
.WithIdentity($"{command.Name}:job:{command.ScheduleId}", command.SchedulerGroup);
20+
}
21+
22+
public void RegisterJob(Action<DailyTimeIntervalScheduleBuilder> action)
23+
{
24+
Scheduler.ScheduleJob(JobDetail, MakeTriggerBuilder(Command).WithDailyTimeIntervalSchedule(action).Build());
25+
}
26+
27+
public void RegisterJob(string cronExpression)
28+
{
29+
Scheduler.ScheduleJob(JobDetail, MakeTriggerBuilder(Command).WithCronSchedule(cronExpression).Build());
30+
}
31+
32+
public void RegisterJob(Action<SimpleScheduleBuilder> action)
33+
{
34+
Scheduler.ScheduleJob(JobDetail, MakeTriggerBuilder(Command).WithSimpleSchedule(action).Build());
35+
}
36+
37+
public void Cron(string cronExpression)
38+
{
39+
RegisterJob(cronExpression);
40+
}
41+
42+
public void EverySecond()
43+
{
44+
RegisterJob(x => x.WithIntervalInSeconds(1).RepeatForever());
45+
}
46+
47+
public void EveryMinute()
48+
{
49+
RegisterJob(x => x.WithIntervalInMinutes(1).RepeatForever());
50+
}
51+
52+
public void EveryFiveMinutes()
53+
{
54+
RegisterJob(x => x.WithIntervalInMinutes(5).RepeatForever());
55+
}
56+
57+
public void EveryTenMinutes()
58+
{
59+
RegisterJob(x => x.WithIntervalInMinutes(10).RepeatForever());
60+
}
61+
62+
public void EveryFifteenMinutes()
63+
{
64+
RegisterJob(x => x.WithIntervalInMinutes(15).RepeatForever());
65+
}
66+
67+
public void EveryThirtyMinutes()
68+
{
69+
RegisterJob(x => x.WithIntervalInMinutes(30).RepeatForever());
70+
}
71+
72+
public void Hourly()
73+
{
74+
RegisterJob(x => x.WithIntervalInHours(1).RepeatForever());
75+
}
76+
77+
public void HourlyAt(int minutesAfterTheHour)
78+
{
79+
RegisterJob($"0 {minutesAfterTheHour} * * * ?");
80+
}
81+
82+
public void Daily()
83+
{
84+
RegisterJob(x => x.WithIntervalInHours(24).RepeatForever());
85+
}
86+
87+
public void DailyAt(TimeOfDay timeOfDay)
88+
{
89+
RegisterJob(x => x.StartingDailyAt(timeOfDay));
90+
}
91+
}
92+
}

src/InEngine.Core/Scheduling/Recurrence.cs

Lines changed: 0 additions & 121 deletions
This file was deleted.

src/InEngine.Core/Scheduling/Schedule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class Schedule
99
{
1010
public IScheduler Scheduler { get; set; } = StdSchedulerFactory.GetDefaultScheduler();
1111

12-
public Recurrence Job(AbstractCommand command)
12+
public Occurence Job(AbstractCommand command)
1313
{
1414
var jobDetail = MakeJobBuilder(command).Build();
1515

@@ -18,7 +18,7 @@ public Recurrence Job(AbstractCommand command)
1818
.ToList()
1919
.ForEach(x => jobDetail.JobDataMap.Add(x.Name, x.GetValue(command)));
2020

21-
return new Recurrence() {
21+
return new Occurence() {
2222
JobDetail = jobDetail,
2323
Command = command
2424
};

0 commit comments

Comments
 (0)