Skip to content

Commit

Permalink
Job to provide default metadata #123
Browse files Browse the repository at this point in the history
  • Loading branch information
andrus committed May 4, 2024
1 parent 6012271 commit f51680c
Show file tree
Hide file tree
Showing 39 changed files with 286 additions and 86 deletions.
4 changes: 4 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.0-M5

* #123 Jobs as lambdas

## 3.0-M4

* #122 Upgrade Spring to 5.3.34
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public JobResult run(Job delegate, Map<String, Object> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public JobResult run(Map<String, Object> params) {
try {
Thread.sleep(DELAY);
} catch (InterruptedException e) {
return JobResult.failure(getMetadata());
return JobResult.failed();
}
return JobResult.success(getMetadata());
return JobResult.succeeded();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public JobResult run(Job delegate, Map<String, Object> 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);
}
}

Expand All @@ -62,17 +62,17 @@ protected JobMeter onMeteredJobStarted(JobMetadata metadata, Map<String, Object>
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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public JobResult run(Map<String, Object> 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();
}
}

Expand All @@ -152,7 +152,7 @@ public JobResult run(Map<String, Object> 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();
}
}

Expand All @@ -164,7 +164,7 @@ public Job3() {

@Override
public JobResult run(Map<String, Object> params) {
return JobResult.success(getMetadata());
return JobResult.succeeded();
}
}

Expand All @@ -176,7 +176,7 @@ public Job4() {

@Override
public JobResult run(Map<String, Object> params) {
return JobResult.success(getMetadata());
return JobResult.succeeded();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public JobMetadata getMetadata() {

@Override
public JobResult run(Map<String, Object> parameters) {
return JobResult.success(metadata);
return JobResult.succeeded();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public JobResult run(Job delegate, Map<String, Object> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public JobResult run(Map<String, Object> 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();
}
}

3 changes: 3 additions & 0 deletions bootique-job/src/main/java/io/bootique/job/BaseJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions bootique-job/src/main/java/io/bootique/job/Job.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
/**
* Represents a runnable job with metadata.
*/
@FunctionalInterface
public interface Job {

JobMetadata getMetadata();
JobResult run(Map<String, Object> params);

JobResult run(Map<String, Object> params);
default JobMetadata getMetadata() {
return JobMetadata.build(getClass());
}
}
Loading

0 comments on commit f51680c

Please sign in to comment.