Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Making Schedule interface Writeable #72

Merged
merged 3 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.cronutils.utils.VisibleForTesting;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
Expand Down Expand Up @@ -56,6 +58,13 @@ public CronSchedule(String expression, ZoneId timezone) {
clock = Clock.system(timezone);
}

public CronSchedule(StreamInput input) throws IOException {
timezone = input.readZoneId();
expression = input.readString();
executionTime = ExecutionTime.forCron(cronParser.parse(expression));
clock = Clock.system(timezone);
}

@VisibleForTesting
void setClock(Clock clock) {
this.clock = clock;
Expand Down Expand Up @@ -159,4 +168,10 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(timezone, expression);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeZoneId(timezone);
out.writeString(expression);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
Expand Down Expand Up @@ -72,6 +74,14 @@ public IntervalSchedule(Instant startTime, int interval, ChronoUnit unit) {
this.clock = Clock.system(ZoneId.systemDefault());
}

public IntervalSchedule(StreamInput input) throws IOException {
startTime = input.readInstant();
interval = input.readInt();
unit = input.readEnum(ChronoUnit.class);
intervalInMillis = input.readLong();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why write/read this instead of constructing based off interval and unit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great question, this constructor is needed when the input is a stream. This stream is the TCP communication between nodes and helps us serialize and de-serialize information.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had an offline chat with @dbbaughe. Sure makes sense. I'll make the change.

clock = Clock.system(ZoneId.systemDefault());
}

@VisibleForTesting
Instant getStartTime() {
return this.startTime;
Expand Down Expand Up @@ -167,4 +177,12 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(startTime, interval, unit, intervalInMillis);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeInstant(startTime);
out.writeInt(interval);
out.writeEnum(unit);
out.writeLong(intervalInMillis);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
package com.amazon.opendistroforelasticsearch.jobscheduler.spi.schedule;

import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ToXContentObject;

import java.time.Duration;
import java.time.Instant;

public interface Schedule extends ToXContentObject {
public interface Schedule extends Writeable, ToXContentObject {

/**
* Gets next job execution time of give time parameter.
Expand Down Expand Up @@ -53,4 +54,4 @@ public interface Schedule extends ToXContentObject {
* @return true if the job executes on time, otherwise false.
*/
Boolean runningOnTime(Instant lastExecutionTime);
}
}