Job repeat immediately after job complete
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
using Quartz;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// Repeated JobChaningJobListener
|
||||
/// </summary>
|
||||
public class JobChainingJobLIstenerWithDataMapRepeated : JobChainingJobListenerWithDataMap
|
||||
{
|
||||
/// <summary>
|
||||
/// JobChaningJobListener with DataMap passing from parent job to next job
|
||||
/// </summary>
|
||||
/// <param name="name">Job name</param>
|
||||
/// <param name="overwriteKeys">If key is overwritable, parent job will pass the value to next job event next job contains that key</param>
|
||||
public JobChainingJobLIstenerWithDataMapRepeated(string name, ICollection<string> overwriteKeys) : base(name, overwriteKeys)
|
||||
{
|
||||
}
|
||||
|
||||
#nullable enable
|
||||
/// <inheritdoc />
|
||||
public override async Task JobWasExecuted(IJobExecutionContext context,
|
||||
JobExecutionException? jobException,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
await base.JobWasExecuted(context, jobException, cancellationToken);
|
||||
ChainLinks.TryGetValue(context.JobDetail.Key, out var sj);
|
||||
if (sj == null)
|
||||
{
|
||||
var chainRoot = context.JobDetail.Key;
|
||||
var chainParent = ChainLinks.FirstOrDefault(p => p.Value == context.JobDetail.Key).Key;
|
||||
while (chainParent != null)
|
||||
{
|
||||
chainRoot = chainParent;
|
||||
chainParent = ChainLinks.FirstOrDefault(p => p.Value == chainParent).Key;
|
||||
}
|
||||
|
||||
var sjJobDetail = await context.Scheduler.GetJobDetail(chainRoot);
|
||||
await context.Scheduler.AddJob(sjJobDetail!, true, false);
|
||||
await context.Scheduler.TriggerJob(chainRoot, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
#nullable disable
|
||||
}
|
||||
}
|
||||
@@ -23,10 +23,13 @@ namespace Modbus.Net
|
||||
{
|
||||
Name = name;
|
||||
OverWriteKeys = overwriteKeys;
|
||||
chainLinks = new Dictionary<JobKey, JobKey>();
|
||||
ChainLinks = new Dictionary<JobKey, JobKey>();
|
||||
}
|
||||
|
||||
private readonly Dictionary<JobKey, JobKey> chainLinks;
|
||||
/// <summary>
|
||||
/// Job chain links
|
||||
/// </summary>
|
||||
protected readonly Dictionary<JobKey, JobKey> ChainLinks;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string Name { get; }
|
||||
@@ -44,7 +47,7 @@ namespace Modbus.Net
|
||||
/// <param name="secondJob">a JobKey with the name and group of the follow-up job</param>
|
||||
public void AddJobChainLink(JobKey firstJob, JobKey secondJob)
|
||||
{
|
||||
chainLinks.Add(firstJob, secondJob);
|
||||
ChainLinks.Add(firstJob, secondJob);
|
||||
}
|
||||
|
||||
#nullable enable
|
||||
@@ -53,7 +56,7 @@ namespace Modbus.Net
|
||||
JobExecutionException? jobException,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
chainLinks.TryGetValue(context.JobDetail.Key, out var sj);
|
||||
ChainLinks.TryGetValue(context.JobDetail.Key, out var sj);
|
||||
|
||||
if (sj == null)
|
||||
{
|
||||
|
||||
@@ -57,7 +57,14 @@ namespace Modbus.Net
|
||||
IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler();
|
||||
|
||||
ITrigger trigger;
|
||||
if (count >= 0)
|
||||
if (interval <= 0)
|
||||
{
|
||||
trigger = TriggerBuilder.Create()
|
||||
.WithIdentity(triggerKey, "Modbus.Net.DataQuery.Group." + triggerKey)
|
||||
.StartNow()
|
||||
.Build();
|
||||
}
|
||||
else if (count >= 0)
|
||||
trigger = TriggerBuilder.Create()
|
||||
.WithIdentity(triggerKey, "Modbus.Net.DataQuery.Group." + triggerKey)
|
||||
.StartNow()
|
||||
@@ -70,7 +77,15 @@ namespace Modbus.Net
|
||||
.WithSimpleSchedule(b => b.WithInterval(TimeSpan.FromSeconds(interval)).RepeatForever())
|
||||
.Build();
|
||||
|
||||
var listener = new JobChainingJobListenerWithDataMap("Modbus.Net.DataQuery.Chain." + triggerKey, new string[2] { "Value", "SetValue" });
|
||||
IJobListener listener;
|
||||
if (interval <= 0)
|
||||
{
|
||||
listener = new JobChainingJobLIstenerWithDataMapRepeated("Modbus.Net.DataQuery.Chain." + triggerKey, new string[2] { "Value", "SetValue" });
|
||||
}
|
||||
else
|
||||
{
|
||||
listener = new JobChainingJobListenerWithDataMap("Modbus.Net.DataQuery.Chain." + triggerKey, new string[2] { "Value", "SetValue" });
|
||||
}
|
||||
scheduler.ListenerManager.AddJobListener(listener, GroupMatcher<JobKey>.GroupEquals("Modbus.Net.DataQuery.Group." + triggerKey));
|
||||
|
||||
if (await scheduler.GetTrigger(new TriggerKey(triggerKey)) != null)
|
||||
@@ -344,9 +359,9 @@ namespace Modbus.Net
|
||||
/// 执行任务
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task Run()
|
||||
public Task Run()
|
||||
{
|
||||
await _scheduler.Start();
|
||||
return _scheduler.Start();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace MachineJob.Service
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
//1. 直接Coding
|
||||
//List<AddressUnit> _addresses = new List<AddressUnit>
|
||||
@@ -61,10 +61,18 @@ namespace MachineJob.Service
|
||||
//}
|
||||
|
||||
//4. 使用MultipleMachinesJobScheduler
|
||||
return Task.Run(() => MultipleMachinesJobScheduler.RunScheduler(machines, async (machine, scheduler) =>
|
||||
//return Task.Run(() => MultipleMachinesJobScheduler.RunScheduler(machines, async (machine, scheduler) =>
|
||||
//{
|
||||
// /await scheduler.From(machine.Id + ".From", machine, MachineDataType.Name).Result.Query(machine.Id + ".ConsoleQuery", QueryConsole).Result.To(machine.Id + ".To", machine).Result.Deal(machine.Id + ".Deal", OnSuccess, OnFailure).Result.Run();
|
||||
//}, -1, 10));
|
||||
|
||||
//5. 不设置固定时间,连续触发Job
|
||||
foreach (var machine in machines)
|
||||
{
|
||||
await scheduler.From(machine.Id + ".From", machine, MachineDataType.Name).Result.Query(machine.Id + ".ConsoleQuery", QueryConsole).Result.To(machine.Id + ".To", machine).Result.Deal(machine.Id + ".Deal", OnSuccess, OnFailure).Result.Run();
|
||||
}, -1, 10));
|
||||
var scheduler = await MachineJobSchedulerCreator<IMachineMethodDatas, string, double>.CreateScheduler(machine.Id, 0, 0);
|
||||
var job = scheduler.From(machine.Id + ".From", machine, MachineDataType.Name).Result.Query(machine.Id + ".ConsoleQuery", QueryConsole).Result.To(machine.Id + ".To", machine).Result.Deal(machine.Id + ".Deal", OnSuccess, OnFailure).Result;
|
||||
await job.Run();
|
||||
}
|
||||
}
|
||||
|
||||
public override Task StopAsync(CancellationToken cancellationToken)
|
||||
|
||||
Reference in New Issue
Block a user