Skip to content

Commit

Permalink
Updated scheduler to support removing jobs.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlalis committed Mar 8, 2022
1 parent f714cea commit 6db233f
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 64 deletions.
6 changes: 3 additions & 3 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"cronexp": ">=0.1.0-beta3 <0.2.0-0"
},
"description": "Simple CRON-style scheduling library.",
"homepage": "https://github.com/andrewlalis/scheduled",
"license": "MIT",
"name": "scheduled",
"homepage": "https://github.com/andrewlalis/scheduled",
"targetType": "library",
"targetName": "scheduled"
"targetName": "scheduled",
"targetType": "library"
}
39 changes: 30 additions & 9 deletions source/scheduled/job.d
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ public class FunctionJob : Job {
}

/**
* Represents a pairing of a Job with a schedule.
* Represents a pairing of a Job with a schedule. This is a component that
* is utilized internally by Schedulers.
*/
public final class ScheduledJob {
package final class ScheduledJob {
/**
* The component which is used to obtain current timestamps.
*/
Expand All @@ -62,17 +63,27 @@ public final class ScheduledJob {
*/
private Job job;

/**
* The unique id for this scheduled job, within the context of the
* scheduler that's responsible for it. This id should be unique among all
* jobs currently managed by the scheduler, but ids may be reused once old
* jobs are removed.
*/
private immutable long id;

/**
* Constructs a new pairing of a job and a schedule.
* Params:
* job = The job which is scheduled.
* schedule = The schedule that defines when the job will run.
* id = The unique id for this job.
* timeProvider = Provider of current timestamps.
*/
public this(Job job, JobSchedule schedule, CurrentTimeProvider timeProvider) {
package this(Job job, JobSchedule schedule, long id, CurrentTimeProvider timeProvider) {
this.job = job;
this.schedule = schedule;
this.timeProvider = timeProvider;
this.id = id;
}

/**
Expand All @@ -81,9 +92,10 @@ public final class ScheduledJob {
* Params:
* job = The job which is scheduled.
* schedule = The schedule that defines when the job will run.
* id = The unique id for this job.
*/
public this(Job job, JobSchedule schedule) {
this(job, schedule, new SysTimeProvider);
package this(Job job, JobSchedule schedule, long id) {
this(job, schedule, id, new SysTimeProvider);
}

/**
Expand All @@ -102,6 +114,14 @@ public final class ScheduledJob {
return this.job;
}

/**
* Gets the id for this scheduled job.
* Returns: The id.
*/
public long getId() {
return this.id;
}

/**
* Compares two scheduled jobs, such that jobs whose next execution time
* is earlier, are considered greater than others.
Expand All @@ -112,6 +132,7 @@ public final class ScheduledJob {
*/
override int opCmp(Object other) {
if (auto otherJob = cast(ScheduledJob) other) {
if (this.getId() == otherJob.getId()) return 0; // Exit right away if we're comparing to the same scheduled job.
SysTime now = timeProvider.now;
auto t1 = this.getSchedule().getNextExecutionTime(now);
auto t2 = otherJob.getSchedule().getNextExecutionTime(now);
Expand Down Expand Up @@ -145,10 +166,10 @@ unittest {
}
auto j = new IncrementJob;

ScheduledJob jobA = new ScheduledJob(j, s1);
ScheduledJob jobA2 = new ScheduledJob(j, s1);
ScheduledJob jobB = new ScheduledJob(j, s2);
ScheduledJob jobC = new ScheduledJob(j, s3);
ScheduledJob jobA = new ScheduledJob(j, s1, 1);
ScheduledJob jobA2 = new ScheduledJob(j, s1, 2);
ScheduledJob jobB = new ScheduledJob(j, s2, 3);
ScheduledJob jobC = new ScheduledJob(j, s3, 4);
assert(jobA > jobB);
assert(jobA >= jobA2 && jobA <= jobA2);
assert(jobB > jobC);
Expand Down
Loading

0 comments on commit 6db233f

Please sign in to comment.