From f51680c4f2e1800f2dcd69356764c691df2fdbbe Mon Sep 17 00:00:00 2001 From: Andrus Adamchik Date: Sat, 4 May 2024 09:03:19 -0400 Subject: [PATCH] Job to provide default metadata #123 --- RELEASE-NOTES.md | 4 + .../job/consul/lock/ConsulLockHandler.java | 2 +- .../bootique/job/consul/it/job/LockJob.java | 4 +- .../instrumented/InstrumentedJobLogger.java | 14 +- .../instrumented/InstrumentedJobMDCIT.java | 8 +- .../JobInstrumentedModule_MetricsIT.java | 2 +- .../instrumented/JobMetricsManagerTest.java | 8 +- .../zookeeper/lock/ZkClusterLockHandler.java | 2 +- .../job/zookeeper/it/job/LockJob.java | 4 +- .../main/java/io/bootique/job/BaseJob.java | 3 + .../src/main/java/io/bootique/job/Job.java | 7 +- .../main/java/io/bootique/job/JobResult.java | 141 ++++++++++++++++++ .../java/io/bootique/job/JobRunBuilder.java | 2 +- .../io/bootique/job/command/ExecCommand.java | 19 +-- .../bootique/job/lock/LocalLockHandler.java | 2 +- .../runtime/ExceptionsHandlerDecorator.java | 2 +- .../io/bootique/job/runtime/GraphJob.java | 2 +- .../io/bootique/job/runtime/GraphJobStep.java | 6 +- .../io/bootique/job/runtime/JobLogger.java | 10 +- .../job/runtime/ParallelJobsStep.java | 18 +-- .../bootique/job/runtime/SingleJobStep.java | 2 +- .../bootique/job/ExecCommand_DeepGroups.java | 12 +- .../java/io/bootique/job/JobDecoratorIT.java | 2 +- .../bootique/job/JobDecorator_FailuresIT.java | 2 +- .../java/io/bootique/job/JobRegistryIT.java | 4 +- .../test/java/io/bootique/job/JobTest.java | 48 ++++++ .../test/java/io/bootique/job/ListenerIT.java | 2 +- .../io/bootique/job/Listener_FailuresIT.java | 2 +- .../io/bootique/job/fixture/BaseTestJob.java | 2 +- .../job/fixture/ExecutableAtMostOnceJob.java | 2 +- .../job/fixture/ParameterizedJob3.java | 2 +- .../job/fixture/ParameterizedJob4.java | 2 +- .../job/fixture/ParameterizedJob5.java | 2 +- .../bootique/job/fixture/ScheduledJob1.java | 2 +- .../bootique/job/fixture/ScheduledJob2.java | 2 +- .../io/bootique/job/graph/GraphJobIT.java | 8 +- .../job/scheduler/SchedulerParamsIT.java | 2 +- .../job/scheduler/Scheduler_NoDeadlockIT.java | 10 +- .../bootique/job/trigger/JobExecParserIT.java | 4 +- 39 files changed, 286 insertions(+), 86 deletions(-) create mode 100644 bootique-job/src/test/java/io/bootique/job/JobTest.java diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index b13cc595..ddbb7784 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -1,3 +1,7 @@ +## 3.0-M5 + +* #123 Jobs as lambdas + ## 3.0-M4 * #122 Upgrade Spring to 5.3.34 diff --git a/bootique-job-consul/src/main/java/io/bootique/job/consul/lock/ConsulLockHandler.java b/bootique-job-consul/src/main/java/io/bootique/job/consul/lock/ConsulLockHandler.java index ac6639ec..03b2ee7e 100644 --- a/bootique-job-consul/src/main/java/io/bootique/job/consul/lock/ConsulLockHandler.java +++ b/bootique-job-consul/src/main/java/io/bootique/job/consul/lock/ConsulLockHandler.java @@ -57,7 +57,7 @@ public JobResult run(Job delegate, Map params) { boolean acquired = kvClient.acquireLock(lockName, sessionId); if (!acquired) { LOGGER.info("** Another job instance owns the lock. Skipping execution of '{}'", lockName); - return JobResult.skipped(metadata, "Another job instance owns the lock. Skipping execution"); + return JobResult.skipped("Another job instance owns the lock. Skipping execution"); } try { diff --git a/bootique-job-consul/src/test/java/io/bootique/job/consul/it/job/LockJob.java b/bootique-job-consul/src/test/java/io/bootique/job/consul/it/job/LockJob.java index d8734c99..0636aadb 100644 --- a/bootique-job-consul/src/test/java/io/bootique/job/consul/it/job/LockJob.java +++ b/bootique-job-consul/src/test/java/io/bootique/job/consul/it/job/LockJob.java @@ -24,8 +24,8 @@ public JobResult run(Map params) { try { Thread.sleep(DELAY); } catch (InterruptedException e) { - return JobResult.failure(getMetadata()); + return JobResult.failed(); } - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } diff --git a/bootique-job-instrumented/src/main/java/io/bootique/job/instrumented/InstrumentedJobLogger.java b/bootique-job-instrumented/src/main/java/io/bootique/job/instrumented/InstrumentedJobLogger.java index 9830546e..642c13fd 100644 --- a/bootique-job-instrumented/src/main/java/io/bootique/job/instrumented/InstrumentedJobLogger.java +++ b/bootique-job-instrumented/src/main/java/io/bootique/job/instrumented/InstrumentedJobLogger.java @@ -46,9 +46,9 @@ public JobResult run(Job delegate, Map params) { try { JobResult result = delegate.run(params); - return onMeteredJobFinished(result, meter); + return onMeteredJobFinished(metadata, result, meter); } catch (Throwable th) { - return onMeteredJobFinished(JobResult.failure(metadata, th), meter); + return onMeteredJobFinished(metadata, JobResult.failure(metadata, th), meter); } } @@ -62,17 +62,17 @@ protected JobMeter onMeteredJobStarted(JobMetadata metadata, Map return meter; } - private JobResult onMeteredJobFinished(JobResult result, JobMeter meter) { + private JobResult onMeteredJobFinished(JobMetadata metadata, JobResult result, JobMeter meter) { long timeMs = meter.stop(result); - logJobFinished(result, timeMs); + logJobFinished(metadata, result, timeMs); mdcManager.onJobFinished(); return result; } - private void logJobFinished(JobResult result, long timeMs) { + private void logJobFinished(JobMetadata metadata, JobResult result, long timeMs) { - String label = result.getMetadata().isGroup() ? "group" : "job"; - String name = result.getMetadata().getName(); + String label = metadata.isGroup() ? "group" : "job"; + String name = metadata.getName(); switch (result.getOutcome()) { case SUCCESS: diff --git a/bootique-job-instrumented/src/test/java/io/bootique/job/instrumented/InstrumentedJobMDCIT.java b/bootique-job-instrumented/src/test/java/io/bootique/job/instrumented/InstrumentedJobMDCIT.java index a8d2ebab..5236f2ee 100644 --- a/bootique-job-instrumented/src/test/java/io/bootique/job/instrumented/InstrumentedJobMDCIT.java +++ b/bootique-job-instrumented/src/test/java/io/bootique/job/instrumented/InstrumentedJobMDCIT.java @@ -132,7 +132,7 @@ public JobResult run(Map params) { int next = counter.getAndIncrement(); String id = MDC.get(TransactionIdMDC.MDC_KEY); tx.put(next, id != null ? id : NULL_PLACEHOLDER); - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -152,7 +152,7 @@ public JobResult run(Map params) { int next = counter.getAndIncrement(); String id = MDC.get(TransactionIdMDC.MDC_KEY); tx.put(next, id != null ? id : NULL_PLACEHOLDER); - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -164,7 +164,7 @@ public Job3() { @Override public JobResult run(Map params) { - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -176,7 +176,7 @@ public Job4() { @Override public JobResult run(Map params) { - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } } diff --git a/bootique-job-instrumented/src/test/java/io/bootique/job/instrumented/JobInstrumentedModule_MetricsIT.java b/bootique-job-instrumented/src/test/java/io/bootique/job/instrumented/JobInstrumentedModule_MetricsIT.java index e68fe780..f66d1b71 100644 --- a/bootique-job-instrumented/src/test/java/io/bootique/job/instrumented/JobInstrumentedModule_MetricsIT.java +++ b/bootique-job-instrumented/src/test/java/io/bootique/job/instrumented/JobInstrumentedModule_MetricsIT.java @@ -78,7 +78,7 @@ public JobMetadata getMetadata() { @Override public JobResult run(Map parameters) { - return JobResult.success(metadata); + return JobResult.succeeded(); } } } diff --git a/bootique-job-instrumented/src/test/java/io/bootique/job/instrumented/JobMetricsManagerTest.java b/bootique-job-instrumented/src/test/java/io/bootique/job/instrumented/JobMetricsManagerTest.java index 4828b740..98048776 100644 --- a/bootique-job-instrumented/src/test/java/io/bootique/job/instrumented/JobMetricsManagerTest.java +++ b/bootique-job-instrumented/src/test/java/io/bootique/job/instrumented/JobMetricsManagerTest.java @@ -48,10 +48,10 @@ public void jobsInstrumentation_ActiveCount_SuccessAndFailureResults() { JobMeter m2 = manager.onJobStarted("j1"); assertHasMetrics("j1", metricRegistry, 2, 0, 0, 0); - m1.stop(JobResult.success(null)); + m1.stop(JobResult.succeeded()); assertHasMetrics("j1", metricRegistry, 1, 1, 1, 0); - m2.stop(JobResult.failure(null)); + m2.stop(JobResult.failed()); assertHasMetrics("j1", metricRegistry, 0, 2, 1, 1); } @@ -62,7 +62,7 @@ public void jobsInstrumentation_UnknownResult() { JobMeter m1 = manager.onJobStarted("j1"); assertHasMetrics("j1", metricRegistry, 1, 0, 0, 0); - m1.stop(JobResult.unknown(null)); + m1.stop(JobResult.unknown()); assertHasMetrics("j1", metricRegistry, 0, 1, 0, 0); } @@ -71,7 +71,7 @@ public void jobsInstrumentation_SuccessResult() { JobMetricsManager manager = new JobMetricsManager(metricRegistry); JobMeter m1 = manager.onJobStarted("j1"); - m1.stop(JobResult.unknown(null)); + m1.stop(JobResult.unknown()); assertHasMetrics("j1", metricRegistry, 0, 1, 0, 0); } diff --git a/bootique-job-zookeeper/src/main/java/io/bootique/job/zookeeper/lock/ZkClusterLockHandler.java b/bootique-job-zookeeper/src/main/java/io/bootique/job/zookeeper/lock/ZkClusterLockHandler.java index 2b4a26f0..d2643a1b 100644 --- a/bootique-job-zookeeper/src/main/java/io/bootique/job/zookeeper/lock/ZkClusterLockHandler.java +++ b/bootique-job-zookeeper/src/main/java/io/bootique/job/zookeeper/lock/ZkClusterLockHandler.java @@ -56,7 +56,7 @@ public JobResult run(Job delegate, Map params) { ZkMutex lock = ZkMutex.acquire(curator.get(), lockName); if (lock == null) { LOGGER.info("** Another job instance owns the lock. Skipping execution of '{}'", lockName); - return JobResult.skipped(metadata, "Another job instance owns the lock. Skipping execution"); + return JobResult.skipped("Another job instance owns the lock. Skipping execution"); } try { diff --git a/bootique-job-zookeeper/src/test/java/io/bootique/job/zookeeper/it/job/LockJob.java b/bootique-job-zookeeper/src/test/java/io/bootique/job/zookeeper/it/job/LockJob.java index fa41abd9..99124b89 100644 --- a/bootique-job-zookeeper/src/test/java/io/bootique/job/zookeeper/it/job/LockJob.java +++ b/bootique-job-zookeeper/src/test/java/io/bootique/job/zookeeper/it/job/LockJob.java @@ -42,9 +42,9 @@ public JobResult run(Map params) { try { Thread.sleep(DELAY); } catch (InterruptedException e) { - return JobResult.failure(JobMetadata.build(LockJob.class)); + return JobResult.failed(); } - return JobResult.success(JobMetadata.build(LockJob.class)); + return JobResult.succeeded(); } } diff --git a/bootique-job/src/main/java/io/bootique/job/BaseJob.java b/bootique-job/src/main/java/io/bootique/job/BaseJob.java index ed373682..82340d69 100644 --- a/bootique-job/src/main/java/io/bootique/job/BaseJob.java +++ b/bootique-job/src/main/java/io/bootique/job/BaseJob.java @@ -21,6 +21,9 @@ import java.util.Map; +/** + * An abstract superclass of Jobs that provide their own metadata. + */ public abstract class BaseJob implements Job { private final JobMetadata metadata; diff --git a/bootique-job/src/main/java/io/bootique/job/Job.java b/bootique-job/src/main/java/io/bootique/job/Job.java index b190a675..fd758b6c 100644 --- a/bootique-job/src/main/java/io/bootique/job/Job.java +++ b/bootique-job/src/main/java/io/bootique/job/Job.java @@ -24,9 +24,12 @@ /** * Represents a runnable job with metadata. */ +@FunctionalInterface public interface Job { - JobMetadata getMetadata(); + JobResult run(Map params); - JobResult run(Map params); + default JobMetadata getMetadata() { + return JobMetadata.build(getClass()); + } } diff --git a/bootique-job/src/main/java/io/bootique/job/JobResult.java b/bootique-job/src/main/java/io/bootique/job/JobResult.java index 216d1a5c..5c861a12 100644 --- a/bootique-job/src/main/java/io/bootique/job/JobResult.java +++ b/bootique-job/src/main/java/io/bootique/job/JobResult.java @@ -27,6 +27,17 @@ public class JobResult { private final String message; private final JobFuture yieldedTo; + /** + * @since 3.0 + */ + public static JobResult succeeded() { + return new JobResult(JobOutcome.SUCCESS, null, null, null); + } + + /** + * @deprecated in favor of {@link #succeeded()} + */ + @Deprecated(since = "3.0", forRemoval = true) public static JobResult success(JobMetadata metadata) { return new JobResult(metadata, JobOutcome.SUCCESS, null, null, null); } @@ -34,18 +45,60 @@ public static JobResult success(JobMetadata metadata) { /** * @since 3.0 */ + public static JobResult succeeded(String message) { + return new JobResult(JobOutcome.SUCCESS, null, message, null); + } + + /** + * @since 3.0 + * @deprecated in favor of {@link #succeeded(String)} + */ + @Deprecated(since = "3.0", forRemoval = true) public static JobResult success(JobMetadata metadata, String message) { return new JobResult(metadata, JobOutcome.SUCCESS, null, message, null); } + /** + * @since 3.0 + */ + public static JobResult failed() { + return new JobResult(JobOutcome.FAILURE, null, null, null); + } + + /** + * @deprecated in favor of {@link #failed()} + */ + @Deprecated(since = "3.0", forRemoval = true) public static JobResult failure(JobMetadata metadata) { return new JobResult(metadata, JobOutcome.FAILURE, null, null, null); } + /** + * @since 3.0 + */ + public static JobResult failed(String message) { + return new JobResult(JobOutcome.FAILURE, null, message, null); + } + + /** + * @deprecated in favor of {@link #failed(String)} + */ + @Deprecated(since = "3.0", forRemoval = true) public static JobResult failure(JobMetadata metadata, String message) { return new JobResult(metadata, JobOutcome.FAILURE, null, message, null); } + /** + * @since 3.0 + */ + public static JobResult failed(Throwable th) { + return new JobResult(JobOutcome.FAILURE, th, null, null); + } + + /** + * @deprecated in favor of {@link #failed(Throwable)} + */ + @Deprecated(since = "3.0", forRemoval = true) public static JobResult failure(JobMetadata metadata, Throwable th) { return new JobResult(metadata, JobOutcome.FAILURE, th, null, null); } @@ -53,6 +106,14 @@ public static JobResult failure(JobMetadata metadata, Throwable th) { /** * @since 3.0 */ + public static JobResult failed(String message, Throwable th) { + return new JobResult(JobOutcome.FAILURE, th, message, null); + } + + /** + * @deprecated in favor of {@link #failed(String, Throwable)} + */ + @Deprecated(since = "3.0", forRemoval = true) public static JobResult failure(JobMetadata metadata, String message, Throwable th) { return new JobResult(metadata, JobOutcome.FAILURE, th, message, null); } @@ -60,6 +121,14 @@ public static JobResult failure(JobMetadata metadata, String message, Throwable /** * @since 3.0 */ + public static JobResult succeededPartially() { + return new JobResult(JobOutcome.PARTIAL_SUCCESS, null, null, null); + } + + /** + * @deprecated in favor of {@link #succeededPartially()} + */ + @Deprecated(since = "3.0", forRemoval = true) public static JobResult partialSuccess(JobMetadata metadata) { return new JobResult(metadata, JobOutcome.PARTIAL_SUCCESS, null, null, null); } @@ -67,14 +136,45 @@ public static JobResult partialSuccess(JobMetadata metadata) { /** * @since 3.0 */ + public static JobResult succeededPartially(String message) { + return new JobResult(JobOutcome.PARTIAL_SUCCESS, null, message, null); + } + + /** + * @deprecated in favor of {@link #succeededPartially(String)} + */ + @Deprecated(since = "3.0", forRemoval = true) public static JobResult partialSuccess(JobMetadata metadata, String message) { return new JobResult(metadata, JobOutcome.PARTIAL_SUCCESS, null, message, null); } + /** + * @since 3.0 + */ + public static JobResult unknown() { + return new JobResult(JobOutcome.UNKNOWN, null, null, null); + } + + /** + * @deprecated in favor of {@link #unknown()} + */ + @Deprecated(since = "3.0", forRemoval = true) public static JobResult unknown(JobMetadata metadata) { return new JobResult(metadata, JobOutcome.UNKNOWN, null, null, null); } + /** + * @since 3.0 + */ + public static JobResult unknown(Throwable th) { + return new JobResult(JobOutcome.UNKNOWN, th, null, null); + } + + + /** + * @deprecated in favor of {@link #unknown(Throwable)} + */ + @Deprecated(since = "3.0", forRemoval = true) public static JobResult unknown(JobMetadata metadata, Throwable th) { return new JobResult(metadata, JobOutcome.UNKNOWN, th, null, null); } @@ -82,6 +182,14 @@ public static JobResult unknown(JobMetadata metadata, Throwable th) { /** * @since 3.0 */ + public static JobResult unknown(String message) { + return new JobResult(JobOutcome.UNKNOWN, null, message, null); + } + + /** + * @deprecated in favor of {@link #unknown(String)} + */ + @Deprecated(since = "3.0", forRemoval = true) public static JobResult unknown(JobMetadata metadata, String message) { return new JobResult(metadata, JobOutcome.UNKNOWN, null, message, null); } @@ -89,6 +197,14 @@ public static JobResult unknown(JobMetadata metadata, String message) { /** * @since 3.0 */ + public static JobResult skipped() { + return new JobResult(JobOutcome.SKIPPED, null, null, null); + } + + /** + * @deprecated in favor of {@link #skipped()} + */ + @Deprecated(since = "3.0", forRemoval = true) public static JobResult skipped(JobMetadata metadata) { return new JobResult(metadata, JobOutcome.SKIPPED, null, null, null); } @@ -96,13 +212,23 @@ public static JobResult skipped(JobMetadata metadata) { /** * @since 3.0 */ + public static JobResult skipped(String message) { + return new JobResult(JobOutcome.SKIPPED, null, message, null); + } + + /** + * @deprecated in favor of {@link #skipped(String)} + */ + @Deprecated(since = "3.0", forRemoval = true) public static JobResult skipped(JobMetadata metadata, String message) { return new JobResult(metadata, JobOutcome.SKIPPED, null, message, null); } /** * @since 3.0 + * @deprecated as JobMetadata is no longer a part of JobResult */ + @Deprecated(since = "3.0", forRemoval = true) protected JobResult(JobMetadata metadata, JobOutcome outcome, Throwable throwable, String message, JobFuture yieldedTo) { this.metadata = metadata; this.outcome = outcome; @@ -111,6 +237,21 @@ protected JobResult(JobMetadata metadata, JobOutcome outcome, Throwable throwabl this.yieldedTo = yieldedTo; } + /** + * @since 3.0 + */ + protected JobResult(JobOutcome outcome, Throwable throwable, String message, JobFuture yieldedTo) { + this.outcome = outcome; + this.throwable = throwable; + this.message = message; + this.yieldedTo = yieldedTo; + this.metadata = null; + } + + /** + * @deprecatedto make possible job lambdas. Current callers must use {@link Job#getMetadata()} instead. + */ + @Deprecated(since = "3.0", forRemoval = true) public JobMetadata getMetadata() { return metadata; } diff --git a/bootique-job/src/main/java/io/bootique/job/JobRunBuilder.java b/bootique-job/src/main/java/io/bootique/job/JobRunBuilder.java index 605579a7..510f0b02 100644 --- a/bootique-job/src/main/java/io/bootique/job/JobRunBuilder.java +++ b/bootique-job/src/main/java/io/bootique/job/JobRunBuilder.java @@ -92,7 +92,7 @@ public JobFuture runNonBlocking() { return new SimpleJobFuture( job.getMetadata().getName(), future, - () -> result[0] != null ? result[0] : JobResult.unknown(job.getMetadata())); + () -> result[0] != null ? result[0] : JobResult.unknown()); } protected Job resolveJob() { diff --git a/bootique-job/src/main/java/io/bootique/job/command/ExecCommand.java b/bootique-job/src/main/java/io/bootique/job/command/ExecCommand.java index 38b37a07..6d7f58b7 100644 --- a/bootique-job/src/main/java/io/bootique/job/command/ExecCommand.java +++ b/bootique-job/src/main/java/io/bootique/job/command/ExecCommand.java @@ -33,6 +33,7 @@ import javax.inject.Inject; import javax.inject.Provider; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class ExecCommand extends CommandWithMetadata { @@ -46,7 +47,7 @@ public class ExecCommand extends CommandWithMetadata { private static OptionMetadata createSerialOption() { return OptionMetadata.builder(SERIAL_OPTION).description("Enforces sequential execution of the jobs, " + - "specified with '--job' options.") + "specified with '--job' options.") .build(); } @@ -93,10 +94,10 @@ private CommandOutcome runParallel(List execs, Scheduler scheduler) { .collect(Collectors.toList()); String failed = futures.stream() - .map(JobFuture::get) - .peek(this::processResult) - .filter(result -> !result.isSuccess()) - .map(r -> r.getMetadata().getName()) + .map(f -> Map.entry(f.getJobName(), f.get())) + .peek(t -> processResult(t.getKey(), t.getValue())) + .filter(t -> !t.getValue().isSuccess()) + .map(t -> t.getKey()) .collect(Collectors.joining(", ")); return failed.isEmpty() ? CommandOutcome.succeeded() : CommandOutcome.failed(1, "Some of the jobs failed: " + failed); @@ -105,7 +106,7 @@ private CommandOutcome runParallel(List execs, Scheduler scheduler) { private CommandOutcome runSerial(List execs, Scheduler scheduler) { for (JobExec e : execs) { JobResult result = scheduler.runBuilder().jobName(e.getJobName()).params(e.getParams()).runBlocking(); - processResult(result); + processResult(e.getJobName(), result); if (!result.isSuccess()) { return CommandOutcome.failed(1, "One of the jobs failed: " + e.getJobName()); } @@ -113,16 +114,16 @@ private CommandOutcome runSerial(List execs, Scheduler scheduler) { return CommandOutcome.succeeded(); } - private void processResult(JobResult result) { + private void processResult(String jobName, JobResult result) { String message = result.getMessage() != null ? String.format("Finished job '%s', result: %s, message: %s", - result.getMetadata().getName(), + jobName, result.getOutcome(), result.getMessage()) : String.format("Finished job '%s', result: %s", - result.getMetadata().getName(), + jobName, result.getOutcome()); if (result.getThrowable() == null) { diff --git a/bootique-job/src/main/java/io/bootique/job/lock/LocalLockHandler.java b/bootique-job/src/main/java/io/bootique/job/lock/LocalLockHandler.java index ad4e65fd..8e7215bc 100644 --- a/bootique-job/src/main/java/io/bootique/job/lock/LocalLockHandler.java +++ b/bootique-job/src/main/java/io/bootique/job/lock/LocalLockHandler.java @@ -53,7 +53,7 @@ public JobResult run(Job delegate, Map params) { if (!lock.tryLock()) { LOGGER.info("Skipping execution of '{}', another job instance owns the lock.", metadata.getName()); - return JobResult.skipped(metadata, "Skipping execution, another job instance owns the lock"); + return JobResult.skipped("Skipping execution, another job instance owns the lock"); } LOGGER.info("Locked '{}'", metadata.getName()); diff --git a/bootique-job/src/main/java/io/bootique/job/runtime/ExceptionsHandlerDecorator.java b/bootique-job/src/main/java/io/bootique/job/runtime/ExceptionsHandlerDecorator.java index b9d182a1..96ccb6d0 100644 --- a/bootique-job/src/main/java/io/bootique/job/runtime/ExceptionsHandlerDecorator.java +++ b/bootique-job/src/main/java/io/bootique/job/runtime/ExceptionsHandlerDecorator.java @@ -40,7 +40,7 @@ public JobResult run(Job delegate, Map params) { static JobResult runWithExceptionHandling(JobMetadata metadata, Job delegate, Map params) { try { JobResult result = delegate.run(params); - return result != null ? result : JobResult.unknown(metadata, "Job returned null result"); + return result != null ? result : JobResult.unknown("Job returned null result"); } catch (Exception e) { // not logging the failure here.. JobLogDecorator will do the logging return JobResult.failure(metadata, e); diff --git a/bootique-job/src/main/java/io/bootique/job/runtime/GraphJob.java b/bootique-job/src/main/java/io/bootique/job/runtime/GraphJob.java index 48fcafea..d912188e 100644 --- a/bootique-job/src/main/java/io/bootique/job/runtime/GraphJob.java +++ b/bootique-job/src/main/java/io/bootique/job/runtime/GraphJob.java @@ -51,6 +51,6 @@ public JobResult run(Map params) { } } - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } diff --git a/bootique-job/src/main/java/io/bootique/job/runtime/GraphJobStep.java b/bootique-job/src/main/java/io/bootique/job/runtime/GraphJobStep.java index 36327111..8f36ccff 100644 --- a/bootique-job/src/main/java/io/bootique/job/runtime/GraphJobStep.java +++ b/bootique-job/src/main/java/io/bootique/job/runtime/GraphJobStep.java @@ -36,18 +36,18 @@ public abstract class GraphJobStep { public abstract JobResult run(Map params); - protected void logResult(JobResult result) { + protected void logResult(String jobName, JobResult result) { if (!LOGGER.isDebugEnabled()) { return; } if (result.isSuccess()) { - LOGGER.debug("graph member '{}' finished", result.getMetadata().getName()); + LOGGER.debug("graph member '{}' finished", jobName); } else { LOGGER.debug("graph member '{}' finished: {} - {}", - result.getMetadata().getName(), + jobName, result.getOutcome(), result.getMessage()); diff --git a/bootique-job/src/main/java/io/bootique/job/runtime/JobLogger.java b/bootique-job/src/main/java/io/bootique/job/runtime/JobLogger.java index 5b1bd8f5..715a676a 100644 --- a/bootique-job/src/main/java/io/bootique/job/runtime/JobLogger.java +++ b/bootique-job/src/main/java/io/bootique/job/runtime/JobLogger.java @@ -43,9 +43,9 @@ public JobResult run(Job delegate, Map params) { try { JobResult result = delegate.run(params); - return onJobFinished(result); + return onJobFinished(metadata, result); } catch (Throwable th) { - return onJobFinished(JobResult.failure(metadata, th)); + return onJobFinished(metadata, JobResult.failure(metadata, th)); } } @@ -54,9 +54,9 @@ private void onJobStarted(JobMetadata metadata, Map params) { LOGGER.info("{} '{}' started with params {}", label, metadata.getName(), params); } - private JobResult onJobFinished(JobResult result) { - String label = result.getMetadata().isGroup() ? "group" : "job"; - String name = result.getMetadata().getName(); + private JobResult onJobFinished(JobMetadata metadata, JobResult result) { + String label = metadata.isGroup() ? "group" : "job"; + String name = metadata.getName(); switch (result.getOutcome()) { case SUCCESS: diff --git a/bootique-job/src/main/java/io/bootique/job/runtime/ParallelJobsStep.java b/bootique-job/src/main/java/io/bootique/job/runtime/ParallelJobsStep.java index 5e4e43a5..054ea869 100644 --- a/bootique-job/src/main/java/io/bootique/job/runtime/ParallelJobsStep.java +++ b/bootique-job/src/main/java/io/bootique/job/runtime/ParallelJobsStep.java @@ -48,17 +48,17 @@ public ParallelJobsStep(GraphExecutor executor, List jobs) { @Override public JobResult run(Map params) { - List> submitted = jobs + List>> submitted = jobs .stream() .skip(1) - .map(j -> executor.submit(j, params)) + .map(j -> Map.entry(j.getMetadata().getName(), executor.submit(j, params))) .collect(Collectors.toList()); JobResult r0 = jobs.get(0).run(params); - logResult(r0); + logResult(jobs.get(0).getMetadata().getName(), r0); if (!r0.isSuccess()) { - LOGGER.debug("First job '{}' failed, canceling the remaining ones", r0.getMetadata().getName()); + LOGGER.debug("First job '{}' failed, canceling the remaining ones", jobs.get(0).getMetadata().getName()); cancelAll(submitted); return r0; } @@ -67,16 +67,16 @@ public JobResult run(Map params) { JobResult r; try { - r = submitted.get(i).get(); + r = submitted.get(i).getValue().get(); } catch (ExecutionException | InterruptedException e) { r = JobResult.failure(jobs.get(i).getMetadata(), e); } - logResult(r); + logResult(submitted.get(i).getKey(), r); if (!r.isSuccess()) { if (i + 1 < submitted.size()) { - LOGGER.debug("Job '{}' failed, canceling the remaining ones", r.getMetadata().getName()); + LOGGER.debug("Job '{}' failed, canceling the remaining ones", submitted.get(i).getKey()); cancelAll(submitted.subList(i + 1, submitted.size())); } @@ -87,7 +87,7 @@ public JobResult run(Map params) { return r0; } - private void cancelAll(List> tasks) { - tasks.forEach(t -> t.cancel(true)); + private void cancelAll(List>> tasks) { + tasks.forEach(t -> t.getValue().cancel(true)); } } diff --git a/bootique-job/src/main/java/io/bootique/job/runtime/SingleJobStep.java b/bootique-job/src/main/java/io/bootique/job/runtime/SingleJobStep.java index baec7348..ad8ccd1d 100644 --- a/bootique-job/src/main/java/io/bootique/job/runtime/SingleJobStep.java +++ b/bootique-job/src/main/java/io/bootique/job/runtime/SingleJobStep.java @@ -38,7 +38,7 @@ public SingleJobStep(Job job) { @Override public JobResult run(Map params) { JobResult result = job.run(params); - logResult(result); + logResult(job.getMetadata().getName(), result); return result; } } diff --git a/bootique-job/src/test/java/io/bootique/job/ExecCommand_DeepGroups.java b/bootique-job/src/test/java/io/bootique/job/ExecCommand_DeepGroups.java index a32004ef..90444a58 100644 --- a/bootique-job/src/test/java/io/bootique/job/ExecCommand_DeepGroups.java +++ b/bootique-job/src/test/java/io/bootique/job/ExecCommand_DeepGroups.java @@ -91,7 +91,7 @@ public JobMetadata getMetadata() { public JobResult run(Map params) { LOGGER.info("running 1"); results.put("j1", 1); - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -106,7 +106,7 @@ public JobMetadata getMetadata() { public JobResult run(Map params) { LOGGER.info("running 2"); results.put("j2", 1); - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -121,7 +121,7 @@ public JobMetadata getMetadata() { public JobResult run(Map params) { LOGGER.info("running 3"); results.put("j3", 1); - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -136,7 +136,7 @@ public JobMetadata getMetadata() { public JobResult run(Map params) { LOGGER.info("running 4"); results.put("j4", 1); - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -151,7 +151,7 @@ public JobMetadata getMetadata() { public JobResult run(Map params) { LOGGER.info("running 5"); results.put("j5", 1); - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -166,7 +166,7 @@ public JobMetadata getMetadata() { public JobResult run(Map params) { LOGGER.info("running 6"); results.put("j6", 1); - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } } diff --git a/bootique-job/src/test/java/io/bootique/job/JobDecoratorIT.java b/bootique-job/src/test/java/io/bootique/job/JobDecoratorIT.java index ed79637a..acc79ab6 100644 --- a/bootique-job/src/test/java/io/bootique/job/JobDecoratorIT.java +++ b/bootique-job/src/test/java/io/bootique/job/JobDecoratorIT.java @@ -231,7 +231,7 @@ public String getActualParam() { @Override public JobResult run(Map params) { this.actualParam = (String) params.get("LP"); - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } diff --git a/bootique-job/src/test/java/io/bootique/job/JobDecorator_FailuresIT.java b/bootique-job/src/test/java/io/bootique/job/JobDecorator_FailuresIT.java index ab4939b9..56272a7a 100644 --- a/bootique-job/src/test/java/io/bootique/job/JobDecorator_FailuresIT.java +++ b/bootique-job/src/test/java/io/bootique/job/JobDecorator_FailuresIT.java @@ -137,7 +137,7 @@ public FailureJob() { @Override public JobResult run(Map params) { - return JobResult.failure(getMetadata(), FAILURE_MESSAGE); + return JobResult.failed(FAILURE_MESSAGE); } } } diff --git a/bootique-job/src/test/java/io/bootique/job/JobRegistryIT.java b/bootique-job/src/test/java/io/bootique/job/JobRegistryIT.java index cfcee803..2d3c67bd 100644 --- a/bootique-job/src/test/java/io/bootique/job/JobRegistryIT.java +++ b/bootique-job/src/test/java/io/bootique/job/JobRegistryIT.java @@ -76,7 +76,7 @@ public JobMetadata getMetadata() { @Override public JobResult run(Map params) { - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -89,7 +89,7 @@ public JobMetadata getMetadata() { @Override public JobResult run(Map params) { - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } } diff --git a/bootique-job/src/test/java/io/bootique/job/JobTest.java b/bootique-job/src/test/java/io/bootique/job/JobTest.java new file mode 100644 index 00000000..fbc85561 --- /dev/null +++ b/bootique-job/src/test/java/io/bootique/job/JobTest.java @@ -0,0 +1,48 @@ +/* + * Licensed to ObjectStyle LLC under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ObjectStyle LLC licenses + * this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.bootique.job; + +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class JobTest { + + @Test + void getMetadata() { + assertEquals("my", new MyJob().getMetadata().getName()); + } + + @Test + void getMetadataLambda() { + Job j = p -> JobResult.succeeded(); + assertTrue(j.getMetadata().getName().startsWith("jobtest$$lambda$"), j.getMetadata().getName()); + } + + static class MyJob implements Job { + + @Override + public JobResult run(Map params) { + return JobResult.succeeded(); + } + } +} diff --git a/bootique-job/src/test/java/io/bootique/job/ListenerIT.java b/bootique-job/src/test/java/io/bootique/job/ListenerIT.java index 6f003bf7..f1f2f21c 100644 --- a/bootique-job/src/test/java/io/bootique/job/ListenerIT.java +++ b/bootique-job/src/test/java/io/bootique/job/ListenerIT.java @@ -196,7 +196,7 @@ public String getActualParam() { @Override public JobResult run(Map params) { this.actualParam = (String) params.get("LP"); - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } diff --git a/bootique-job/src/test/java/io/bootique/job/Listener_FailuresIT.java b/bootique-job/src/test/java/io/bootique/job/Listener_FailuresIT.java index aa39bb5e..72a49870 100644 --- a/bootique-job/src/test/java/io/bootique/job/Listener_FailuresIT.java +++ b/bootique-job/src/test/java/io/bootique/job/Listener_FailuresIT.java @@ -154,7 +154,7 @@ public FailureJob() { @Override public JobResult run(Map params) { - return JobResult.failure(getMetadata(), FAILURE_MESSAGE); + return JobResult.failed(FAILURE_MESSAGE); } } diff --git a/bootique-job/src/test/java/io/bootique/job/fixture/BaseTestJob.java b/bootique-job/src/test/java/io/bootique/job/fixture/BaseTestJob.java index 16cbbbd1..679cb1e9 100644 --- a/bootique-job/src/test/java/io/bootique/job/fixture/BaseTestJob.java +++ b/bootique-job/src/test/java/io/bootique/job/fixture/BaseTestJob.java @@ -44,7 +44,7 @@ protected BaseTestJob(JobMetadata metadata) { public JobResult run(Map params) { this.executedAtNanos = System.nanoTime(); this.params = params; - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } public T assertNotExecuted() { diff --git a/bootique-job/src/test/java/io/bootique/job/fixture/ExecutableAtMostOnceJob.java b/bootique-job/src/test/java/io/bootique/job/fixture/ExecutableAtMostOnceJob.java index b1228384..f77189b1 100644 --- a/bootique-job/src/test/java/io/bootique/job/fixture/ExecutableAtMostOnceJob.java +++ b/bootique-job/src/test/java/io/bootique/job/fixture/ExecutableAtMostOnceJob.java @@ -85,7 +85,7 @@ private JobResult doExecute(Map params) { busyWait(runIterations); finishedAt = System.nanoTime(); executed = true; - return shouldFail ? JobResult.failure(getMetadata()) : JobResult.success(getMetadata()); + return shouldFail ? JobResult.failed() : JobResult.succeeded(); } private void busyWait(long iterations) { diff --git a/bootique-job/src/test/java/io/bootique/job/fixture/ParameterizedJob3.java b/bootique-job/src/test/java/io/bootique/job/fixture/ParameterizedJob3.java index 5f76772a..35134784 100644 --- a/bootique-job/src/test/java/io/bootique/job/fixture/ParameterizedJob3.java +++ b/bootique-job/src/test/java/io/bootique/job/fixture/ParameterizedJob3.java @@ -42,6 +42,6 @@ public JobResult run(Map params) { assertEquals(3L, params.get("longp")); assertEquals("value1", params.get("param1")); assertEquals(2, params.get("param2")); - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } diff --git a/bootique-job/src/test/java/io/bootique/job/fixture/ParameterizedJob4.java b/bootique-job/src/test/java/io/bootique/job/fixture/ParameterizedJob4.java index af0e5117..639ac04c 100644 --- a/bootique-job/src/test/java/io/bootique/job/fixture/ParameterizedJob4.java +++ b/bootique-job/src/test/java/io/bootique/job/fixture/ParameterizedJob4.java @@ -38,7 +38,7 @@ public ParameterizedJob4() { @Override public JobResult run(Map params) { this.params = params; - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } public Map getParams() { diff --git a/bootique-job/src/test/java/io/bootique/job/fixture/ParameterizedJob5.java b/bootique-job/src/test/java/io/bootique/job/fixture/ParameterizedJob5.java index a5ef0b0f..a3961ad1 100644 --- a/bootique-job/src/test/java/io/bootique/job/fixture/ParameterizedJob5.java +++ b/bootique-job/src/test/java/io/bootique/job/fixture/ParameterizedJob5.java @@ -38,7 +38,7 @@ public ParameterizedJob5() { @Override public JobResult run(Map params) { this.params = params; - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } public Map getParams() { diff --git a/bootique-job/src/test/java/io/bootique/job/fixture/ScheduledJob1.java b/bootique-job/src/test/java/io/bootique/job/fixture/ScheduledJob1.java index 0d245d6a..71bbac15 100644 --- a/bootique-job/src/test/java/io/bootique/job/fixture/ScheduledJob1.java +++ b/bootique-job/src/test/java/io/bootique/job/fixture/ScheduledJob1.java @@ -33,6 +33,6 @@ public ScheduledJob1() { @Override public JobResult run(Map params) { - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } diff --git a/bootique-job/src/test/java/io/bootique/job/fixture/ScheduledJob2.java b/bootique-job/src/test/java/io/bootique/job/fixture/ScheduledJob2.java index 90f010d9..06be4112 100644 --- a/bootique-job/src/test/java/io/bootique/job/fixture/ScheduledJob2.java +++ b/bootique-job/src/test/java/io/bootique/job/fixture/ScheduledJob2.java @@ -33,6 +33,6 @@ public ScheduledJob2() { @Override public JobResult run(Map params) { - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } diff --git a/bootique-job/src/test/java/io/bootique/job/graph/GraphJobIT.java b/bootique-job/src/test/java/io/bootique/job/graph/GraphJobIT.java index df4edd72..892695c2 100644 --- a/bootique-job/src/test/java/io/bootique/job/graph/GraphJobIT.java +++ b/bootique-job/src/test/java/io/bootique/job/graph/GraphJobIT.java @@ -112,7 +112,7 @@ public JobMetadata getMetadata() { @Override public JobResult run(Map params) { - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -125,7 +125,7 @@ public JobMetadata getMetadata() { @Override public JobResult run(Map params) { - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -138,7 +138,7 @@ public JobMetadata getMetadata() { @Override public JobResult run(Map params) { - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -153,7 +153,7 @@ public JobMetadata getMetadata() { @Override public JobResult run(Map params) { - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } } diff --git a/bootique-job/src/test/java/io/bootique/job/scheduler/SchedulerParamsIT.java b/bootique-job/src/test/java/io/bootique/job/scheduler/SchedulerParamsIT.java index ad271f39..da5d9949 100644 --- a/bootique-job/src/test/java/io/bootique/job/scheduler/SchedulerParamsIT.java +++ b/bootique-job/src/test/java/io/bootique/job/scheduler/SchedulerParamsIT.java @@ -81,7 +81,7 @@ public JobMetadata getMetadata() { @Override public JobResult run(Map parameters) { - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } diff --git a/bootique-job/src/test/java/io/bootique/job/scheduler/Scheduler_NoDeadlockIT.java b/bootique-job/src/test/java/io/bootique/job/scheduler/Scheduler_NoDeadlockIT.java index 5017c06f..36838df4 100644 --- a/bootique-job/src/test/java/io/bootique/job/scheduler/Scheduler_NoDeadlockIT.java +++ b/bootique-job/src/test/java/io/bootique/job/scheduler/Scheduler_NoDeadlockIT.java @@ -101,7 +101,7 @@ public J1() { @Override public JobResult run(Map params) { LOGGER.info("1 in progress"); - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -114,7 +114,7 @@ public J2() { @Override public JobResult run(Map params) { LOGGER.info("2 in progress"); - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -127,7 +127,7 @@ public J3() { @Override public JobResult run(Map params) { LOGGER.info("3 in progress"); - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -140,7 +140,7 @@ public J4() { @Override public JobResult run(Map params) { LOGGER.info("4 in progress"); - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -153,7 +153,7 @@ public J5() { @Override public JobResult run(Map params) { LOGGER.info("5 in progress"); - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } } diff --git a/bootique-job/src/test/java/io/bootique/job/trigger/JobExecParserIT.java b/bootique-job/src/test/java/io/bootique/job/trigger/JobExecParserIT.java index e316ed44..75877ea6 100644 --- a/bootique-job/src/test/java/io/bootique/job/trigger/JobExecParserIT.java +++ b/bootique-job/src/test/java/io/bootique/job/trigger/JobExecParserIT.java @@ -96,7 +96,7 @@ public JobMetadata getMetadata() { @Override public JobResult run(Map params) { - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } @@ -113,7 +113,7 @@ public JobMetadata getMetadata() { @Override public JobResult run(Map params) { - return JobResult.success(getMetadata()); + return JobResult.succeeded(); } } }