Merge branch 'develop' of https://github.com/4treesCH/strolch.git into develop
This commit is contained in:
commit
f585aa696d
|
@ -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 (Throwable e) {
|
} catch (Exception 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 {
|
||||||
|
|
|
@ -38,8 +38,7 @@ public class StrolchJobsHandler extends StrolchComponent {
|
||||||
super.initialize(configuration);
|
super.initialize(configuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
public StrolchJob registerAndScheduleJob(Class<? extends StrolchJob> strolchJobClass) {
|
private StrolchJob instantiateStrolchJob(Class<? extends StrolchJob> strolchJobClass) {
|
||||||
|
|
||||||
StrolchJob strolchJob;
|
StrolchJob strolchJob;
|
||||||
try {
|
try {
|
||||||
Constructor<? extends StrolchJob> constructor = strolchJobClass.getConstructor(StrolchAgent.class);
|
Constructor<? extends StrolchJob> constructor = strolchJobClass.getConstructor(StrolchAgent.class);
|
||||||
|
@ -47,13 +46,22 @@ public class StrolchJobsHandler extends StrolchComponent {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new IllegalArgumentException("Failed to instantiate class " + strolchJobClass.getName(), e);
|
throw new IllegalArgumentException("Failed to instantiate class " + strolchJobClass.getName(), e);
|
||||||
}
|
}
|
||||||
|
return strolchJob;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StrolchJob registerAndScheduleJob(Class<? extends StrolchJob> strolchJobClass) {
|
||||||
|
return register(instantiateStrolchJob(strolchJobClass)).schedule();
|
||||||
|
}
|
||||||
|
|
||||||
|
public StrolchJob register(Class<? extends StrolchJob> strolchJobClass) {
|
||||||
|
return register(instantiateStrolchJob(strolchJobClass));
|
||||||
|
}
|
||||||
|
|
||||||
|
public StrolchJob register(StrolchJob strolchJob) {
|
||||||
if (this.jobs.containsKey(strolchJob.getName()))
|
if (this.jobs.containsKey(strolchJob.getName()))
|
||||||
throw new IllegalArgumentException("Job " + strolchJob.getName() + " is already registered!");
|
throw new IllegalArgumentException("Job " + strolchJob.getName() + " is already registered!");
|
||||||
|
|
||||||
strolchJob.schedule();
|
|
||||||
|
|
||||||
this.jobs.put(strolchJob.getName(), strolchJob);
|
this.jobs.put(strolchJob.getName(), strolchJob);
|
||||||
|
|
||||||
return strolchJob;
|
return strolchJob;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue