[New] added duration and running flag to StrolchJob

This commit is contained in:
Robert von Burg 2018-09-11 12:18:51 +02:00
parent e4a91526d8
commit 5736cead97
1 changed files with 34 additions and 14 deletions

View File

@ -1,6 +1,7 @@
package li.strolch.job; package li.strolch.job;
import static li.strolch.model.Tags.AGENT; import static li.strolch.model.Tags.AGENT;
import static li.strolch.utils.helper.StringHelper.formatMillisecondsDuration;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
@ -49,11 +50,14 @@ public abstract class StrolchJob implements Runnable {
private long delay; private long delay;
private TimeUnit delayTimeUnit; private TimeUnit delayTimeUnit;
private boolean running;
private long nrOfExecutions; private long nrOfExecutions;
private long totalDuration;
private long lastDuration;
private boolean first; private boolean first;
private ScheduledFuture<?> future; private ScheduledFuture<?> future;
private ZonedDateTime lastExecution; private ZonedDateTime lastExecution;
private Throwable lastException; private Exception lastException;
public StrolchJob(StrolchAgent agent, JobMode jobMode, long initialDelay, TimeUnit initialDelayTimeUnit, long delay, public StrolchJob(StrolchAgent agent, JobMode jobMode, long initialDelay, TimeUnit initialDelayTimeUnit, long delay,
TimeUnit delayTimeUnit) { TimeUnit delayTimeUnit) {
@ -167,21 +171,21 @@ public abstract class StrolchJob implements Runnable {
/** /**
* Executes this job now, but if the job is currently running, then it is blocked till the job is complete * Executes this job now, but if the job is currently running, then it is blocked till the job is complete
*/ */
public void runNow() { public void runNow() throws Exception {
synchronized (this) { doWork();
runAsAgent(this::execute); if (this.lastException != null)
this.lastExecution = ZonedDateTime.now(); throw this.lastException;
this.nrOfExecutions++;
}
} }
@Override private synchronized void doWork() {
public final void run() { this.running = true;
long start = System.currentTimeMillis();
try { try {
synchronized (this) { runAsAgent(this::execute);
runAsAgent(this::execute); this.lastException = null;
} } catch (Exception e) {
} catch (Throwable e) { this.running = false;
this.lastException = e; this.lastException = e;
logger.error("Execution of Job " + this.getClass().getSimpleName() + " failed.", e); logger.error("Execution of Job " + this.getClass().getSimpleName() + " failed.", e);
@ -195,8 +199,18 @@ public abstract class StrolchJob implements Runnable {
} }
} }
long took = System.currentTimeMillis() - start;
this.totalDuration += took;
this.lastDuration = took;
this.running = false;
this.lastExecution = ZonedDateTime.now(); this.lastExecution = ZonedDateTime.now();
this.nrOfExecutions++; this.nrOfExecutions++;
}
@Override
public final void run() {
doWork();
if (this.first) { if (this.first) {
this.first = false; this.first = false;
@ -220,8 +234,10 @@ public abstract class StrolchJob implements Runnable {
* @see Future#cancel(boolean) * @see Future#cancel(boolean)
*/ */
public StrolchJob cancel(boolean mayInterruptIfRunning) { public StrolchJob cancel(boolean mayInterruptIfRunning) {
if (this.future != null) if (this.future != null) {
this.future.cancel(mayInterruptIfRunning); this.future.cancel(mayInterruptIfRunning);
this.future = null;
}
return this; return this;
} }
@ -274,6 +290,10 @@ public abstract class StrolchJob implements Runnable {
jsonObject.addProperty("delay", this.delay); jsonObject.addProperty("delay", this.delay);
jsonObject.addProperty("delayTimeUnit", this.delayTimeUnit.name()); jsonObject.addProperty("delayTimeUnit", this.delayTimeUnit.name());
jsonObject.addProperty("running", this.running);
jsonObject.addProperty("totalDuration", formatMillisecondsDuration(totalDuration));
jsonObject.addProperty("lastDuration", formatMillisecondsDuration(lastDuration));
if (this.lastExecution == null) { if (this.lastExecution == null) {
jsonObject.addProperty("lastExecution", "-"); jsonObject.addProperty("lastExecution", "-");
} else { } else {