Skip to content

Commit

Permalink
feat(#103): add SchedulingCompositeMonitor
Browse files Browse the repository at this point in the history
  • Loading branch information
zero88 committed Jan 15, 2024
1 parent 17cafa1 commit fba966e
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.zero88.schedulerx;

import io.github.zero88.schedulerx.impl.SchedulingCompositeMonitorImpl;

/**
* Represents for a delegated monitor that holds multiple scheduling monitors.
* <p/>
*
* @param <OUT> Type of job result data
* @apiNote The holder keeps only one monitor per java class, if try to register many instances of same class, the
* holder will keep the last one
* @since 2.0.0
*/
public interface SchedulingCompositeMonitor<OUT> extends SchedulingMonitor<OUT> {

static <O> SchedulingCompositeMonitor<O> create() {
return new SchedulingCompositeMonitorImpl<>();
}

/**
* Register a new scheduling monitor.
*
* @param monitor the scheduling monitor
* @return a reference to this for fluent API
*/
SchedulingCompositeMonitor<OUT> register(SchedulingMonitor<OUT> monitor);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.github.zero88.schedulerx.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.jetbrains.annotations.NotNull;

import io.github.zero88.schedulerx.ExecutionResult;
import io.github.zero88.schedulerx.SchedulingCompositeMonitor;
import io.github.zero88.schedulerx.SchedulingMonitor;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;

public final class SchedulingCompositeMonitorImpl<OUT> extends SchedulingMonitorAbstract<OUT>
implements SchedulingCompositeMonitor<OUT> {

@SuppressWarnings("rawtypes")
private final Map<Class<? extends SchedulingMonitor>, SchedulingMonitor<OUT>> monitors = new HashMap<>();

@Override
protected Logger logger() {
return LoggerFactory.getLogger(SchedulingCompositeMonitor.class);
}

@Override
public SchedulingCompositeMonitor<OUT> register(SchedulingMonitor<OUT> monitor) {
if (Objects.nonNull(monitor)) {
this.monitors.put(monitor.getClass(), monitor);
}
return this;
}

@Override
public void onUnableSchedule(@NotNull ExecutionResult<OUT> result) {
monitors.forEach(((aClass, monitor) -> dispatch(aClass, monitor::onUnableSchedule, result)));
}

@Override
public void onSchedule(@NotNull ExecutionResult<OUT> result) {
monitors.forEach(((aClass, monitor) -> dispatch(aClass, monitor::onSchedule, result)));
}

@Override
public void onMisfire(@NotNull ExecutionResult<OUT> result) {
monitors.forEach(((aClass, monitor) -> dispatch(aClass, monitor::onMisfire, result)));
}

@Override
public void onEach(@NotNull ExecutionResult<OUT> result) {
monitors.forEach(((aClass, monitor) -> dispatch(aClass, monitor::onEach, result)));
}

@Override
public void onCompleted(@NotNull ExecutionResult<OUT> result) {
monitors.forEach(((aClass, monitor) -> dispatch(aClass, monitor::onCompleted, result)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ void test_job_should_be_executed_in_cron_trigger(Vertx vertx, VertxTestContext t
Assertions.assertEquals(4, result.round());
Assertions.assertFalse(result.isError());
};
final SchedulingAsserter<String> asserter = SchedulingAsserter.<String>builder()
.setTestContext(testContext)
.setSchedule(onSchedule)
.setEach(onEach)
.setCompleted(onCompleted)
.build();
final SchedulingMonitor<String> asserter = SchedulingAsserter.<String>builder()
.setTestContext(testContext)
.setSchedule(onSchedule)
.setEach(onEach)
.setCompleted(onCompleted)
.build();
final CronTrigger trigger = CronTrigger.builder().expression("0/2 * * ? * * *").build();
final Job<Void, String> job = (jobData, ctx) -> {
final long round = ctx.round();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ void test_job_should_be_executed_when_receive_event(Vertx vertx, VertxTestContex
Assertions.assertEquals(String.valueOf(data), result.data());
}
};
final SchedulingAsserter<String> asserter = SchedulingAsserter.<String>builder()
.setTestContext(testContext)
.setEach(each)
.build();
final SchedulingMonitor<String> asserter = SchedulingAsserter.<String>builder()
.setTestContext(testContext)
.setEach(each)
.build();
final String address = "schedulerx.event.2";
final EventTrigger<Object> trigger = EventTrigger.builder()
.address(address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ void test_job_should_be_executed_in_interval_trigger(Vertx vertx, VertxTestConte
}
Assertions.assertNull(result.triggerContext().info());
};
final SchedulingAsserter<String> asserter = SchedulingAsserter.<String>builder()
.setTestContext(context)
.setEach(onEach)
.build();
final SchedulingMonitor<String> asserter = SchedulingAsserter.<String>builder()
.setTestContext(context)
.setEach(onEach)
.build();
final Job<Void, String> job = (jobData, ctx) -> {
final long round = ctx.round();
if (round == 1) {
Expand Down Expand Up @@ -103,11 +103,11 @@ void test_job_should_be_executed_in_interval_trigger_after_delay(Vertx vertx, Ve
Assertions.assertTrue(timeLapsed.compareTo(initialDelay) > 0);
};
final Consumer<ExecutionResult<Void>> onComplete = result -> Assertions.assertEquals(1, result.round());
final SchedulingAsserter<Void> asserter = SchedulingAsserter.<Void>builder()
.setTestContext(ctx)
.setSchedule(onSchedule)
.setCompleted(onComplete)
.build();
final SchedulingMonitor<Void> asserter = SchedulingAsserter.<Void>builder()
.setTestContext(ctx)
.setSchedule(onSchedule)
.setCompleted(onComplete)
.build();
final IntervalTrigger trigger = IntervalTrigger.builder()
.initialDelay(initialDelay)
.interval(Duration.ofSeconds(2))
Expand Down Expand Up @@ -146,13 +146,13 @@ void test_run_blocking_job_till_the_end(Vertx vertx, VertxTestContext testContex
Assertions.assertEquals(3, result.round());
flag.flag();
};
final SchedulingAsserter<Void> asserter = SchedulingAsserter.<Void>builder()
.setTestContext(testContext)
.setEach(onEach)
.setMisfire(onMisfire)
.setCompleted(onComplete)
.disableAutoCompleteTest()
.build();
final SchedulingMonitor<Void> asserter = SchedulingAsserter.<Void>builder()
.setTestContext(testContext)
.setEach(onEach)
.setMisfire(onMisfire)
.setCompleted(onComplete)
.disableAutoCompleteTest()
.build();
final IntervalTrigger trigger = IntervalTrigger.builder().interval(Duration.ofSeconds(1)).repeat(3).build();
final WorkerExecutor worker = vertx.createSharedWorkerExecutor("hello", 3, 1000);
IntervalScheduler.<Void, Void>builder()
Expand All @@ -171,10 +171,10 @@ void test_scheduler_should_be_stopped_when_reach_to_target_round(Vertx vertx, Ve
Assertions.assertTrue(result.triggerContext().isStopped());
Assertions.assertEquals("StopByTriggerConfig", result.triggerContext().condition().reasonCode());
};
final SchedulingAsserter<String> asserter = SchedulingAsserter.<String>builder()
.setTestContext(context)
.setCompleted(onCompleted)
.build();
final SchedulingMonitor<String> asserter = SchedulingAsserter.<String>builder()
.setTestContext(context)
.setCompleted(onCompleted)
.build();
final IntervalTrigger trigger = IntervalTrigger.builder().interval(Duration.ofSeconds(1)).repeat(3).build();
IntervalScheduler.<Void, String>builder()
.setVertx(vertx)
Expand Down
Loading

0 comments on commit fba966e

Please sign in to comment.