-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds a delay parameter to the job scheduler #61
Changes from 11 commits
5ad1431
566e2cd
c7fea9e
8a3eef0
743820a
bf417de
14e9dc7
071bbd4
659336f
4a72993
0eab490
472f0ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,39 +66,48 @@ public class IntervalSchedule implements Schedule { | |
SUPPORTED_UNITS = Collections.unmodifiableSet(set); | ||
} | ||
|
||
private Instant startTime; | ||
private Instant initialStartTime; | ||
private Instant startTimeWithDelay; | ||
private int interval; | ||
private ChronoUnit unit; | ||
private transient long intervalInMillis; | ||
private Clock clock; | ||
private Long scheduleDelay; | ||
|
||
public IntervalSchedule(Instant startTime, int interval, ChronoUnit unit) { | ||
if (!SUPPORTED_UNITS.contains(unit)) { | ||
throw new IllegalArgumentException( | ||
String.format(Locale.ROOT, "Interval unit %s is not supported, expects %s", | ||
unit, SUPPORTED_UNITS)); | ||
} | ||
this.startTime = startTime; | ||
this.initialStartTime = startTime; | ||
this.startTimeWithDelay = startTime; | ||
this.interval = interval; | ||
this.unit = unit; | ||
this.intervalInMillis = Duration.of(interval, this.unit).toMillis(); | ||
this.clock = Clock.system(ZoneId.systemDefault()); | ||
} | ||
|
||
public IntervalSchedule(Instant startTime, int interval, ChronoUnit unit, long scheduleDelay) { | ||
this(startTime, interval, unit); | ||
this.startTimeWithDelay = startTime.plusMillis(scheduleDelay); | ||
this.scheduleDelay = scheduleDelay; | ||
} | ||
|
||
public IntervalSchedule(StreamInput input) throws IOException { | ||
startTime = input.readInstant(); | ||
initialStartTime = input.readInstant(); | ||
interval = input.readInt(); | ||
unit = input.readEnum(ChronoUnit.class); | ||
scheduleDelay = input.readOptionalLong(); | ||
startTimeWithDelay = scheduleDelay == null ? initialStartTime : initialStartTime.plusMillis(scheduleDelay); | ||
intervalInMillis = Duration.of(interval, unit).toMillis(); | ||
clock = Clock.system(ZoneId.systemDefault()); | ||
} | ||
|
||
@VisibleForTesting | ||
Instant getStartTime() { | ||
return this.startTime; | ||
public Instant getStartTime() { | ||
return this.startTimeWithDelay; | ||
} | ||
|
||
@VisibleForTesting | ||
public int getInterval() { | ||
return this.interval; | ||
} | ||
|
@@ -107,22 +116,31 @@ public ChronoUnit getUnit() { | |
return this.unit; | ||
} | ||
|
||
public Long getDelay() { return this.scheduleDelay; } | ||
|
||
@Override | ||
public Instant getNextExecutionTime(Instant time) { | ||
Instant baseTime = time == null ? this.clock.instant() : time; | ||
long delta = (baseTime.toEpochMilli() - this.startTime.toEpochMilli()) % this.intervalInMillis; | ||
long remaining = this.intervalInMillis - delta; | ||
|
||
return baseTime.plus(remaining, ChronoUnit.MILLIS); | ||
long delta = (baseTime.toEpochMilli() - this.startTimeWithDelay.toEpochMilli()); | ||
if (delta >= 0) { | ||
long remaining = this.intervalInMillis - (delta % this.intervalInMillis); | ||
return baseTime.plus(remaining, ChronoUnit.MILLIS); | ||
} else { | ||
return this.startTimeWithDelay; | ||
} | ||
} | ||
|
||
@Override | ||
public Duration nextTimeToExecute() { | ||
long enabledTimeEpochMillis = this.startTime.toEpochMilli(); | ||
long enabledTimeEpochMillis = this.startTimeWithDelay.toEpochMilli(); | ||
Instant currentTime = this.clock.instant(); | ||
long delta = currentTime.toEpochMilli() - enabledTimeEpochMillis; | ||
long remainingScheduleTime = intervalInMillis - (delta % intervalInMillis); | ||
return Duration.of(remainingScheduleTime, ChronoUnit.MILLIS); | ||
if (delta >= 0) { | ||
long remainingScheduleTime = intervalInMillis - (delta % intervalInMillis); | ||
return Duration.of(remainingScheduleTime, ChronoUnit.MILLIS); | ||
} else { | ||
return Duration.ofMillis(enabledTimeEpochMillis - currentTime.toEpochMilli()); | ||
} | ||
Comment on lines
+138
to
+143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment on Ternary operators! Simplification of code increases its readability. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am also a big fan of ternary operators! But I try to only use them in simple cases, and I really try to not include anything that takes much thought. I agree with you on the other cases but I will leave this one as an if/else for that reason |
||
} | ||
|
||
@Override | ||
|
@@ -139,7 +157,7 @@ public Boolean runningOnTime(Instant lastExecutionTime) { | |
return true; | ||
} | ||
|
||
long enabledTimeEpochMillis = this.startTime.toEpochMilli(); | ||
long enabledTimeEpochMillis = this.startTimeWithDelay.toEpochMilli(); | ||
Instant now = this.clock.instant(); | ||
long expectedMillisSinceLastExecution = (now.toEpochMilli() - enabledTimeEpochMillis) % this.intervalInMillis; | ||
if (expectedMillisSinceLastExecution < 1000) { | ||
|
@@ -155,10 +173,11 @@ public Boolean runningOnTime(Instant lastExecutionTime) { | |
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject() | ||
.startObject(INTERVAL_FIELD) | ||
.field(START_TIME_FIELD, this.startTime.toEpochMilli()) | ||
.field(START_TIME_FIELD, this.initialStartTime.toEpochMilli()) | ||
.field(PERIOD_FIELD, this.interval) | ||
.field(UNIT_FIELD, this.unit) | ||
.endObject() | ||
.field(UNIT_FIELD, this.unit); | ||
if (this.scheduleDelay != null) { builder.field(DELAY_FIELD, this.scheduleDelay); } | ||
builder.endObject() | ||
.endObject(); | ||
return builder; | ||
} | ||
|
@@ -178,21 +197,27 @@ public boolean equals(Object o) { | |
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
IntervalSchedule intervalSchedule = (IntervalSchedule) o; | ||
return startTime.equals(intervalSchedule.startTime) && | ||
return initialStartTime.equals(intervalSchedule.initialStartTime) && | ||
interval == intervalSchedule.interval && | ||
unit == intervalSchedule.unit && | ||
intervalInMillis == intervalSchedule.intervalInMillis; | ||
intervalInMillis == intervalSchedule.intervalInMillis && | ||
Objects.equals(scheduleDelay, intervalSchedule.scheduleDelay); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(startTime, interval, unit, intervalInMillis); | ||
if (scheduleDelay == null) { | ||
return Objects.hash(initialStartTime, interval, unit, intervalInMillis); | ||
} else { | ||
return Objects.hash(initialStartTime, interval, unit, intervalInMillis, scheduleDelay); | ||
} | ||
downsrob marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeInstant(startTime); | ||
out.writeInstant(initialStartTime); | ||
out.writeInt(interval); | ||
out.writeEnum(unit); | ||
out.writeOptionalLong(scheduleDelay); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are your thoughts on adding the example path to target?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this very much, but this follows another plugin: https://github.com/opensearch-project/security-dashboards-plugin/blob/ad6e723d4710b91cbcf1f0ec543b6ec24b976c53/DEVELOPER_GUIDE.md and this change lets the lychee link checker succeed