Skip to content

Commit 3a3eeeb

Browse files
committed
Autowire job context values to command properties
1 parent f6384d1 commit 3a3eeeb

File tree

3 files changed

+8
-17
lines changed

3 files changed

+8
-17
lines changed

src/InEngine.Core.Test/Queue/Commands/ConsumeTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public void ShouldConsumeSecondaryQueueBasedOnJobContextData()
4949
var jobExecutionConext = new Mock<IJobExecutionContext>();
5050
var jobDataMap = new JobDataMap { { "useSecondaryQueue", true } };
5151
jobExecutionConext.SetupGet(p => p.JobDetail.JobDataMap).Returns(jobDataMap);
52-
Subject.JobExecutionContext = jobExecutionConext.Object;
5352

5453
Subject.Run();
5554
}

src/InEngine.Core/AbstractCommand.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using InEngine.Core.IO;
34
using Konsole;
45
using Quartz;
@@ -7,7 +8,6 @@ namespace InEngine.Core
78
{
89
abstract public class AbstractCommand : ICommand, IFailed, IJob, IWrite
910
{
10-
public IJobExecutionContext JobExecutionContext { get; set; }
1111
public ProgressBar ProgressBar { get; internal set; }
1212
public string Name { get; set; }
1313
public string SchedulerGroup { get; set; }
@@ -45,7 +45,13 @@ public void UpdateProgress(int tick)
4545
#region Scheduling
4646
public void Execute(IJobExecutionContext context)
4747
{
48-
JobExecutionContext = context;
48+
var properties = GetType().GetProperties();
49+
context.MergedJobDataMap.ToList().ForEach(x => {
50+
var property = properties.FirstOrDefault(y => y.Name == x.Key);
51+
if (property != null)
52+
property.SetValue(this, x.Value);
53+
});
54+
4955
try
5056
{
5157
Run();
@@ -56,19 +62,6 @@ public void Execute(IJobExecutionContext context)
5662
}
5763
}
5864

59-
public T GetJobContextData<T>(string key)
60-
{
61-
if (JobExecutionContext == null || JobExecutionContext.MergedJobDataMap == null)
62-
return default(T);
63-
var objectVal = JobExecutionContext.MergedJobDataMap.Get(key);
64-
return objectVal == null ? default(T) : (T)objectVal;
65-
}
66-
67-
public void AddJobContextData<T>(string key, T val)
68-
{
69-
JobExecutionContext.MergedJobDataMap.Add(key, val);
70-
}
71-
7265
#endregion
7366

7467
#region Console output

src/InEngine.Core/Queue/Commands/Consume.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class Consume : AbstractCommand
1414

1515
public override void Run()
1616
{
17-
UseSecondaryQueue = UseSecondaryQueue || GetJobContextData<bool>("useSecondaryQueue");
1817
var broker = Broker.Make(UseSecondaryQueue);
1918
var shouldConsume = true;
2019
while (shouldConsume)

0 commit comments

Comments
 (0)