diff --git a/Cognite.Simulator.Tests/UtilsTests/SimulationSchedulerTest.cs b/Cognite.Simulator.Tests/UtilsTests/SimulationSchedulerTest.cs index 0b966609..2f2d3384 100644 --- a/Cognite.Simulator.Tests/UtilsTests/SimulationSchedulerTest.cs +++ b/Cognite.Simulator.Tests/UtilsTests/SimulationSchedulerTest.cs @@ -11,6 +11,7 @@ using System.Threading.Tasks; using Xunit; using CogniteSdk.Alpha; +using NCrontab; namespace Cognite.Simulator.Tests.UtilsTests { @@ -37,9 +38,27 @@ public Task Delay(TimeSpan delay, CancellationToken token) } } + [Collection(nameof(SequentialTestCollection))] public class SimulationSchedulerTest { + [Fact(DisplayName = "Test Basic Working of Scheduler")] + public void TestBasicWorkingofExternalLibrary() + { + var job = new ScheduledJob() { }; + job.SetSchedule("*/5 * * * *"); + var nextOccurence = job.Schedule.GetNextOccurrence(DateTime.Now); + Assert.True(nextOccurence > DateTime.Now); + + + Assert.Throws(() => + { + job = new ScheduledJob() { }; + job.SetSchedule("0 0 0 0 0"); + }); + + } + [Fact] public async Task TestSimulationSchedulerBase() { diff --git a/Cognite.Simulator.Utils/SimulationScheduler.cs b/Cognite.Simulator.Utils/SimulationScheduler.cs index d24b1053..4149945d 100644 --- a/Cognite.Simulator.Utils/SimulationScheduler.cs +++ b/Cognite.Simulator.Utils/SimulationScheduler.cs @@ -73,6 +73,19 @@ public class ScheduledJob where V : SimulatorRoutineRevision /// Routine revision. /// public V RoutineRevision { get; set; } + + /// + /// Set schedule + /// + /// Crontab schedule + /// Self + /// If the schedule is invalid + /// + public ScheduledJob SetSchedule(string CronExpression) + { + Schedule = CrontabSchedule.Parse(CronExpression); + return this; + } } /// /// This class implements a basic simulation scheduler. It runs a loop on a configurable interval. @@ -171,14 +184,13 @@ await Task.Run(async () => { continue; } - var schedule = CrontabSchedule.Parse(routineRev.Configuration.Schedule.CronExpression); var newJob = new ScheduledJob { - Schedule = schedule, TokenSource = new CancellationTokenSource(), CreatedTime = routineRev.CreatedTime, RoutineRevision = routineRev, }; + newJob.SetSchedule(routineRev.Configuration.Schedule.CronExpression); _logger.LogDebug("Created new job for schedule: {0} with id {1}", routineRev.Configuration.Schedule.CronExpression, routineRev.ExternalId); scheduledJobs.Add(routineRev.RoutineExternalId, newJob); }