Skip to content

Commit 4275c28

Browse files
committed
Merge branch '3.4-dev'
2 parents ea02809 + 0893123 commit 4275c28

File tree

11 files changed

+129
-29
lines changed

11 files changed

+129
-29
lines changed

src/InEngine.Core/AbstractCommand.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace InEngine.Core
99
{
1010
abstract public class AbstractCommand : ICommand, IFailed, IJob, IWrite
1111
{
12+
public LifecycleActions LifecycleActions { get; set; }
1213
public Write Write { get; set; }
1314
public ProgressBar ProgressBar { get; internal set; }
1415
public string Name { get; set; }
@@ -20,7 +21,8 @@ protected AbstractCommand()
2021
ScheduleId = Guid.NewGuid().ToString();
2122
Name = GetType().FullName;
2223
SchedulerGroup = GetType().AssemblyQualifiedName;
23-
Write = new Write();;
24+
Write = new Write();
25+
LifecycleActions = new LifecycleActions();
2426
}
2527

2628
public virtual void Run()
@@ -55,7 +57,9 @@ public void Execute(IJobExecutionContext context)
5557

5658
try
5759
{
60+
LifecycleActions.BeforeAction?.Invoke(this);
5861
Run();
62+
LifecycleActions.AfterAction?.Invoke(this);
5963
}
6064
catch (Exception exception)
6165
{
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace InEngine.Core.Exceptions
4+
{
5+
public class DuplicateScheduledCommandException : Exception
6+
{
7+
public DuplicateScheduledCommandException(string commandName, string schedulerId, string schedulerGroup)
8+
: base($"A command was scheduled multiple times with the same ID ({schedulerId}) in the same group ({schedulerGroup}). Ensure IDs are unique in a group.")
9+
{}
10+
}
11+
}

src/InEngine.Core/Queuing/Jobs.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ private void ScheduleQueueConsumerJobs(Schedule schedule, int consumers, bool us
2323
schedule.Job(new Consume() {
2424
ScheduleId = $"{(useSecondaryQueue ? "secondary" : "primary")}:{index.ToString()}",
2525
UseSecondaryQueue = useSecondaryQueue
26-
}).EverySecond();
26+
})
27+
.EverySecond()
28+
.Before(x => Console.Write("Before..."))
29+
.After(x => Console.Write("After..."));
2730
}
2831
}
2932
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
3+
namespace InEngine.Core.Scheduling
4+
{
5+
public class JobGroup
6+
{
7+
public string Name { get; set; }
8+
public IDictionary<string, JobRegistration> Registrations { get; set; } = new Dictionary<string, JobRegistration>();
9+
}
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Quartz;
2+
3+
namespace InEngine.Core.Scheduling
4+
{
5+
public class JobRegistration
6+
{
7+
public AbstractCommand Command { get; set; }
8+
public IJobDetail JobDetail { get; set; }
9+
public ITrigger Trigger { get; set; }
10+
11+
public JobRegistration()
12+
{}
13+
14+
public JobRegistration(AbstractCommand command, IJobDetail jobDetail, ITrigger trigger)
15+
: this()
16+
{
17+
Command = command;
18+
JobDetail = jobDetail;
19+
Trigger = trigger;
20+
}
21+
}
22+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
namespace InEngine.Core.Scheduling
3+
{
4+
public class LifecycleActionApi
5+
{
6+
public LifecycleActionApi()
7+
{
8+
}
9+
}
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
namespace InEngine.Core.Scheduling
3+
{
4+
public class LifecycleActions
5+
{
6+
public JobRegistration JobRegistration { get; set; }
7+
public Action<AbstractCommand> BeforeAction { get; set; }
8+
public Action<AbstractCommand> AfterAction { get; set; }
9+
10+
public LifecycleActions Before(Action<AbstractCommand> action)
11+
{
12+
JobRegistration.Command.LifecycleActions.BeforeAction = action;
13+
return this;
14+
}
15+
16+
public LifecycleActions After(Action<AbstractCommand> action)
17+
{
18+
JobRegistration.Command.LifecycleActions.AfterAction = action;
19+
return this;
20+
}
21+
}
22+
}

src/InEngine.Core/Scheduling/Occurence.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace InEngine.Core.Scheduling
66
{
77
public class Occurence
88
{
9-
public IScheduler Scheduler { get; set; } = StdSchedulerFactory.GetDefaultScheduler();
9+
public Schedule Schedule { get; set; }
1010
public AbstractCommand Command { get; set; }
1111
public IJobDetail JobDetail { get; set; }
1212

@@ -16,32 +16,36 @@ public static TriggerBuilder MakeTriggerBuilder(AbstractCommand command)
1616
throw new ArgumentNullException(command.GetType().Name, "The command to schedule cannot be null.");
1717
return TriggerBuilder
1818
.Create()
19-
.WithIdentity($"{command.Name}:job:{command.ScheduleId}", command.SchedulerGroup);
19+
.WithIdentity($"{command.Name}:job:{command.ScheduleId}", command.SchedulerGroup);
2020
}
2121

2222
public void RegisterJob(Action<DailyTimeIntervalScheduleBuilder> action)
2323
{
24-
Scheduler.ScheduleJob(JobDetail, MakeTriggerBuilder(Command).WithDailyTimeIntervalSchedule(action).Build());
24+
Schedule.RegisterJob(Command, JobDetail, MakeTriggerBuilder(Command).WithDailyTimeIntervalSchedule(action).Build());
2525
}
2626

2727
public void RegisterJob(string cronExpression)
2828
{
29-
Scheduler.ScheduleJob(JobDetail, MakeTriggerBuilder(Command).WithCronSchedule(cronExpression).Build());
29+
Schedule.RegisterJob(Command, JobDetail, MakeTriggerBuilder(Command).WithCronSchedule(cronExpression).Build());
3030
}
3131

3232
public void RegisterJob(Action<SimpleScheduleBuilder> action)
3333
{
34-
Scheduler.ScheduleJob(JobDetail, MakeTriggerBuilder(Command).WithSimpleSchedule(action).Build());
34+
Schedule.RegisterJob(Command, JobDetail, MakeTriggerBuilder(Command).WithSimpleSchedule(action).Build());
3535
}
3636

3737
public void Cron(string cronExpression)
3838
{
3939
RegisterJob(cronExpression);
4040
}
4141

42-
public void EverySecond()
42+
public LifecycleActions EverySecond()
4343
{
4444
RegisterJob(x => x.WithIntervalInSeconds(1).RepeatForever());
45+
return new LifecycleActions()
46+
{
47+
Command = Command
48+
};
4549
}
4650

4751
public void EveryMinute()

src/InEngine.Core/Scheduling/Schedule.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Linq;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using InEngine.Core.Exceptions;
25
using Quartz;
36
using Quartz.Impl;
47

@@ -7,6 +10,7 @@ namespace InEngine.Core.Scheduling
710
public class Schedule
811
{
912
public IScheduler Scheduler { get; set; } = StdSchedulerFactory.GetDefaultScheduler();
13+
public IDictionary<string, JobGroup> JobGroups { get; set; } = new Dictionary<string, JobGroup>();
1014

1115
public Occurence Job(AbstractCommand command)
1216
{
@@ -19,12 +23,40 @@ public Occurence Job(AbstractCommand command)
1923
.ToList()
2024
.ForEach(x => jobDetail.JobDataMap.Add(x.Name, x.GetValue(command)));
2125

22-
return new Occurence() {
26+
return new Occurence()
27+
{
28+
Schedule = this,
2329
JobDetail = jobDetail,
2430
Command = command
2531
};
2632
}
2733

34+
public JobRegistration RegisterJob(AbstractCommand command, IJobDetail jobDetail, ITrigger trigger)
35+
{
36+
if (!JobGroups.ContainsKey(command.SchedulerGroup))
37+
JobGroups.Add(command.SchedulerGroup, new JobGroup());
38+
39+
if (JobGroups[command.SchedulerGroup].Registrations.ContainsKey(command.ScheduleId))
40+
throw new DuplicateScheduledCommandException(command.Name, command.ScheduleId, command.SchedulerGroup);
41+
42+
var registration = new JobRegistration(command, jobDetail, trigger);
43+
JobGroups[command.SchedulerGroup].Registrations.Add(command.ScheduleId, registration);
44+
return registration;
45+
}
46+
47+
public void Initialize()
48+
{
49+
Plugin.Load<IJobs>().ForEach(x => {
50+
x.Make<IJobs>().ForEach(y => y.Schedule(this));
51+
});
52+
53+
JobGroups.AsEnumerable().ToList().ForEach(x => {
54+
x.Value.Registrations.AsEnumerable().ToList().ForEach(y => {
55+
Scheduler.ScheduleJob(y.Value.JobDetail, y.Value.Trigger);
56+
});
57+
});
58+
}
59+
2860
public void Start()
2961
{
3062
Scheduler.Start();

src/InEngine/Jobs.cs

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

0 commit comments

Comments
 (0)