Skip to content
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

Feature/trigger evaluation #95

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -12,7 +12,6 @@
* @param <T> Type of data
* @since 1.0.0
*/
@FunctionalInterface
public interface JobData<T> {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @since 1.0.0
*/
@Internal
@FunctionalInterface
public interface JobExecutor<OUT> {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.jetbrains.annotations.NotNull;

/**
* Shared immutable fields between JobExecutor and its builder.
* Shared immutable fields between {@code JobExecutor} and its builder.
* <p/>
* This class is designed to internal usage, don't refer it in your code.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jetbrains.annotations.NotNull;

import io.github.zero88.schedulerx.trigger.Trigger;
import io.github.zero88.schedulerx.trigger.TriggerEvaluator;
import io.vertx.core.Vertx;

/**
Expand All @@ -21,10 +22,14 @@ public interface SchedulerBuilder<IN, OUT, TRIGGER extends Trigger, SCHEDULER ex
SELF extends SchedulerBuilder<IN, OUT, TRIGGER, SCHEDULER, SELF>>
extends JobExecutorContext<IN, OUT>, SchedulerContext<TRIGGER, OUT> {

@NotNull TriggerEvaluator triggerEvaluator();

@NotNull SELF setVertx(@NotNull Vertx vertx);

@NotNull SELF setTrigger(@NotNull TRIGGER trigger);

@NotNull SELF setTriggerEvaluator(@NotNull TriggerEvaluator evaluator);

@NotNull SELF setMonitor(@NotNull SchedulingMonitor<OUT> monitor);

@NotNull SELF setJob(@NotNull Job<IN, OUT> job);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
import io.github.zero88.schedulerx.trigger.Trigger;
import io.vertx.core.Vertx;

/**
* Shared immutable fields between {@code Scheduler} and its builder.
* <p/>
* This class is designed to internal usage, don't refer it in your code.
*
* @param <TRIGGER> Type of trigger
* @param <OUT> Type of job result data
* @since 2.0.0
*/
@Internal
interface SchedulerContext<TRIGGER extends Trigger, OUT> {

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.github.zero88.schedulerx.SchedulingMonitor;
import io.github.zero88.schedulerx.TimeoutPolicy;
import io.github.zero88.schedulerx.trigger.Trigger;
import io.github.zero88.schedulerx.trigger.TriggerEvaluator;
import io.vertx.core.Vertx;

/**
Expand All @@ -30,6 +31,7 @@ public abstract class AbstractSchedulerBuilder<IN, OUT, T extends Trigger, S ext
private JobData<IN> jobData;
private Job<IN, OUT> job;
private T trigger;
private TriggerEvaluator evaluator;
private TimeoutPolicy timeoutPolicy;

@Override
Expand All @@ -45,6 +47,11 @@ public abstract class AbstractSchedulerBuilder<IN, OUT, T extends Trigger, S ext
@Override
public @NotNull T trigger() { return Objects.requireNonNull(trigger, "Trigger is required"); }

@Override
public @NotNull TriggerEvaluator triggerEvaluator() {
return Optional.ofNullable(evaluator).orElseGet(DefaultTriggerEvaluator::new);
}

@Override
public @NotNull Job<IN, OUT> job() { return Objects.requireNonNull(job, "Job is required"); }

Expand All @@ -71,6 +78,12 @@ public abstract class AbstractSchedulerBuilder<IN, OUT, T extends Trigger, S ext
return (B) this;
}

@Override
public @NotNull B setTriggerEvaluator(@NotNull TriggerEvaluator evaluator) {
this.evaluator = evaluator;
return (B) this;
}

public @NotNull B setMonitor(@NotNull SchedulingMonitor<OUT> monitor) {
this.monitor = monitor;
return (B) this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.github.zero88.schedulerx.impl;

import java.util.Optional;

import org.jetbrains.annotations.ApiStatus.Internal;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import io.github.zero88.schedulerx.trigger.AfterTriggerEvaluator;
import io.github.zero88.schedulerx.trigger.BeforeTriggerEvaluator;
import io.github.zero88.schedulerx.trigger.Trigger;
import io.github.zero88.schedulerx.trigger.TriggerContext;
import io.github.zero88.schedulerx.trigger.TriggerEvaluator;
import io.vertx.core.Future;

@Internal
public class DefaultTriggerEvaluator implements TriggerEvaluator {

private BeforeTriggerEvaluator before = (trigger, ctx, externalId) -> Future.succeededFuture(ctx);
private AfterTriggerEvaluator after = (trigger, ctx, externalId, round) -> Future.succeededFuture(ctx);
private TriggerEvaluator next;

public DefaultTriggerEvaluator() { }

DefaultTriggerEvaluator(BeforeTriggerEvaluator beforeEvaluator, AfterTriggerEvaluator afterEvaluator) {
before = Optional.ofNullable(beforeEvaluator).orElse(before);
after = Optional.ofNullable(afterEvaluator).orElse(after);
}

public static TriggerEvaluator init(BeforeTriggerEvaluator beforeEvaluator, AfterTriggerEvaluator afterEvaluator) {
return new DefaultTriggerEvaluator(beforeEvaluator, afterEvaluator);
}

@Override
public final @NotNull Future<TriggerContext> beforeTrigger(@NotNull Trigger trigger,
@NotNull TriggerContext triggerContext,
@Nullable Object externalId) {
return this.internalBeforeTrigger(trigger, triggerContext, externalId)
.flatMap(c -> next == null ? Future.succeededFuture(c) : next.beforeTrigger(trigger, c, externalId));
}

@Override
public final @NotNull Future<TriggerContext> afterTrigger(@NotNull Trigger trigger,
@NotNull TriggerContext triggerContext,
@Nullable Object externalId, long round) {
// @formatter:off
return this.internalAfterTrigger(trigger, triggerContext, externalId, round )
.flatMap(c -> next == null ? Future.succeededFuture(c) : next.afterTrigger(trigger, c, externalId, round));
// @formatter:on
}

@Override
public final @NotNull TriggerEvaluator andThen(@Nullable TriggerEvaluator another) {
this.next = another;
return this;
}

protected Future<TriggerContext> internalBeforeTrigger(@NotNull Trigger trigger,
@NotNull TriggerContext triggerContext,
@Nullable Object externalId) {
return before.beforeTrigger(trigger, triggerContext, externalId);
}

protected Future<TriggerContext> internalAfterTrigger(@NotNull Trigger trigger,
@NotNull TriggerContext triggerContext,
@Nullable Object externalId, long round) {
return after.afterTrigger(trigger, triggerContext, externalId, round);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@

import org.jetbrains.annotations.NotNull;

import io.github.zero88.schedulerx.trigger.TriggerContext;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;

final class ExecutionContextImpl<OUTPUT> implements ExecutionContextInternal<OUTPUT> {

private final Vertx vertx;
private final long round;
private final TriggerTransitionContext triggerContext;
private final TriggerContext triggerContext;
private final Instant triggeredAt;
private Instant executedAt;
private Promise<Object> promise;
private OUTPUT data;
private Throwable error;
private boolean forceStop = false;

ExecutionContextImpl(Vertx vertx, TriggerTransitionContext triggerContext, long round) {
ExecutionContextImpl(Vertx vertx, TriggerContext triggerContext, long round) {
this.vertx = vertx;
this.round = round;
this.triggerContext = triggerContext;
Expand All @@ -37,9 +38,11 @@ final class ExecutionContextImpl<OUTPUT> implements ExecutionContextInternal<OUT
return this;
}

public @NotNull Vertx vertx() { return this.vertx; }
@Override
public @NotNull Vertx vertx() { return this.vertx; }

public @NotNull TriggerTransitionContext triggerContext() { return this.triggerContext; }
@Override
public @NotNull TriggerContext triggerContext() { return this.triggerContext; }

@Override
public @NotNull Instant triggeredAt() { return this.triggeredAt; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

interface ExecutionContextInternal<OUTPUT> extends ExecutionContext<OUTPUT> {

@NotNull TriggerTransitionContext triggerContext();

/**
* Prepare to execute job
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public ExecutionResultBuilder<OUTPUT> setRound(long round) {
}

public ExecutionResultBuilder<OUTPUT> setTriggerContext(TriggerContext triggerContext) {
this.triggerContext = TriggerContextFactory.convert(triggerContext);
this.triggerContext = triggerContext;
return this;
}

Expand Down
Loading
Loading