From c522561fe6107af949b4ac3716471ddb3c801df3 Mon Sep 17 00:00:00 2001 From: luosheng Date: Sat, 15 Jul 2023 22:56:59 +0800 Subject: [PATCH] Add count to repeat --- .../Job/JobChainingJobLIstenerWithDataMapRepeated.cs | 12 ++++++++++-- Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs | 2 +- Samples/MachineJob/Worker.cs | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Modbus.Net/Modbus.Net/Job/JobChainingJobLIstenerWithDataMapRepeated.cs b/Modbus.Net/Modbus.Net/Job/JobChainingJobLIstenerWithDataMapRepeated.cs index 45833de..dcec7b2 100644 --- a/Modbus.Net/Modbus.Net/Job/JobChainingJobLIstenerWithDataMapRepeated.cs +++ b/Modbus.Net/Modbus.Net/Job/JobChainingJobLIstenerWithDataMapRepeated.cs @@ -13,13 +13,17 @@ namespace Modbus.Net /// public class JobChainingJobLIstenerWithDataMapRepeated : JobChainingJobListenerWithDataMap { + protected int RepeatCount { get; set; } + /// /// JobChaningJobListener with DataMap passing from parent job to next job /// /// Job name /// If key is overwritable, parent job will pass the value to next job event next job contains that key - public JobChainingJobLIstenerWithDataMapRepeated(string name, ICollection overwriteKeys) : base(name, overwriteKeys) + /// Repeatation count for job chain + public JobChainingJobLIstenerWithDataMapRepeated(string name, ICollection overwriteKeys, int repeatCount) : base(name, overwriteKeys) { + RepeatCount = repeatCount; } #nullable enable @@ -29,6 +33,7 @@ namespace Modbus.Net CancellationToken cancellationToken = default) { await base.JobWasExecuted(context, jobException, cancellationToken); + if (RepeatCount == 0) return; ChainLinks.TryGetValue(context.JobDetail.Key, out var sj); if (sj == null) { @@ -39,7 +44,10 @@ namespace Modbus.Net chainRoot = chainParent; chainParent = ChainLinks.FirstOrDefault(p => p.Value == chainParent).Key; } - + if (RepeatCount > 0) + { + RepeatCount--; + } var sjJobDetail = await context.Scheduler.GetJobDetail(chainRoot); await context.Scheduler.AddJob(sjJobDetail!, true, false); await context.Scheduler.TriggerJob(chainRoot, cancellationToken).ConfigureAwait(false); diff --git a/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs b/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs index 30ad145..9a93a6c 100644 --- a/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs +++ b/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs @@ -80,7 +80,7 @@ namespace Modbus.Net IJobListener listener; if (interval <= 0) { - listener = new JobChainingJobLIstenerWithDataMapRepeated("Modbus.Net.DataQuery.Chain." + triggerKey, new string[2] { "Value", "SetValue" }); + listener = new JobChainingJobLIstenerWithDataMapRepeated("Modbus.Net.DataQuery.Chain." + triggerKey, new string[2] { "Value", "SetValue" }, count); } else { diff --git a/Samples/MachineJob/Worker.cs b/Samples/MachineJob/Worker.cs index 9009b17..ca32ecb 100644 --- a/Samples/MachineJob/Worker.cs +++ b/Samples/MachineJob/Worker.cs @@ -69,7 +69,7 @@ namespace MachineJob.Service //5. 不设置固定时间,连续触发Job foreach (var machine in machines) { - var scheduler = await MachineJobSchedulerCreator.CreateScheduler(machine.Id, 0, 0); + var scheduler = await MachineJobSchedulerCreator.CreateScheduler(machine.Id, -1, 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(); }