[Fix] Properly check if OperationsLog is available in StrolchJob

This commit is contained in:
Robert von Burg 2022-07-19 10:56:50 +02:00
parent 3a355bb383
commit 0fcc42c2eb
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
1 changed files with 18 additions and 14 deletions

View File

@ -183,8 +183,8 @@ public abstract class StrolchJob implements Runnable, Restrictable {
} }
/** /**
* Performs the given {@link PrivilegedRunnable} as the privileged system user {@link * Performs the given {@link PrivilegedRunnable} as the privileged system user
* StrolchConstants#SYSTEM_USER_AGENT} * {@link StrolchConstants#SYSTEM_USER_AGENT}
* *
* @param runnable * @param runnable
* the runnable to perform * the runnable to perform
@ -276,14 +276,15 @@ public abstract class StrolchJob implements Runnable, Restrictable {
this.lastException = e; this.lastException = e;
logger.error("Execution of Job " + getName() + " failed.", e); logger.error("Execution of Job " + getName() + " failed.", e);
OperationsLog operationsLog = getContainer().getComponent(OperationsLog.class); if (getContainer().hasComponent(OperationsLog.class)) {
if (operationsLog != null) { OperationsLog operationsLog = getContainer().getComponent(OperationsLog.class);
operationsLog.addMessage( operationsLog.addMessage(
new LogMessage(this.realmName == null ? StrolchConstants.DEFAULT_REALM : this.realmName, new LogMessage(this.realmName == null ? StrolchConstants.DEFAULT_REALM : this.realmName,
SYSTEM_USER_AGENT, Locator.valueOf(AGENT, "strolch-agent", StrolchAgent.getUniqueId()), SYSTEM_USER_AGENT, Locator.valueOf(AGENT, "strolch-agent", StrolchAgent.getUniqueId()),
LogSeverity.Exception, LogMessageState.Information, LogSeverity.Exception, LogMessageState.Information,
ResourceBundle.getBundle("strolch-agent"), "strolchjob.failed").withException(e) ResourceBundle.getBundle("strolch-agent"), "strolchjob.failed").withException(e)
.value("jobName", getClass().getName()).value("reason", e)); .value("jobName", getClass().getName())
.value("reason", e));
} }
} }
@ -359,8 +360,8 @@ public abstract class StrolchJob implements Runnable, Restrictable {
return this; return this;
} }
logger.info("First execution of " + getName() + " will be at " + executionTime logger.info("First execution of " + getName() + " will be at " + executionTime.format(
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); DateTimeFormatter.ISO_OFFSET_DATE_TIME));
long delay = PeriodDuration.between(ZonedDateTime.now(), executionTime).toMillis(); long delay = PeriodDuration.between(ZonedDateTime.now(), executionTime).toMillis();
this.future = getScheduledExecutor().schedule(this, delay, TimeUnit.MILLISECONDS); this.future = getScheduledExecutor().schedule(this, delay, TimeUnit.MILLISECONDS);
@ -369,7 +370,8 @@ public abstract class StrolchJob implements Runnable, Restrictable {
long millis = this.initialDelayTimeUnit.toMillis(this.initialDelay); long millis = this.initialDelayTimeUnit.toMillis(this.initialDelay);
logger.info("First execution of " + getName() + " will be at " + ZonedDateTime.now() logger.info("First execution of " + getName() + " will be at " + ZonedDateTime.now()
.plus(millis, ChronoUnit.MILLIS).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); .plus(millis, ChronoUnit.MILLIS)
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
this.future = getScheduledExecutor().schedule(this, this.initialDelay, this.initialDelayTimeUnit); this.future = getScheduledExecutor().schedule(this, this.initialDelay, this.initialDelayTimeUnit);
} }
@ -386,8 +388,8 @@ public abstract class StrolchJob implements Runnable, Restrictable {
return this; return this;
} }
logger.info("Next execution of " + getName() + " will be at " + executionTime logger.info("Next execution of " + getName() + " will be at " + executionTime.format(
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); DateTimeFormatter.ISO_OFFSET_DATE_TIME));
long delay = PeriodDuration.between(ZonedDateTime.now(), executionTime).toMillis(); long delay = PeriodDuration.between(ZonedDateTime.now(), executionTime).toMillis();
this.future = getScheduledExecutor().schedule(this, delay, TimeUnit.MILLISECONDS); this.future = getScheduledExecutor().schedule(this, delay, TimeUnit.MILLISECONDS);
@ -396,7 +398,8 @@ public abstract class StrolchJob implements Runnable, Restrictable {
long millis = this.delayTimeUnit.toMillis(this.delay); long millis = this.delayTimeUnit.toMillis(this.delay);
logger.info("Next execution of " + getName() + " will be at " + ZonedDateTime.now() logger.info("Next execution of " + getName() + " will be at " + ZonedDateTime.now()
.plus(millis, ChronoUnit.MILLIS).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); .plus(millis, ChronoUnit.MILLIS)
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
this.future = getScheduledExecutor().schedule(this, this.delay, this.delayTimeUnit); this.future = getScheduledExecutor().schedule(this, this.delay, this.delayTimeUnit);
} }
@ -439,8 +442,8 @@ public abstract class StrolchJob implements Runnable, Restrictable {
if (this.lastExecution == null) { if (this.lastExecution == null) {
jsonObject.addProperty("lastExecution", "-"); jsonObject.addProperty("lastExecution", "-");
} else { } else {
String lastExecution = this.lastExecution String lastExecution = this.lastExecution.format(
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault())); DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()));
jsonObject.addProperty("lastExecution", lastExecution); jsonObject.addProperty("lastExecution", lastExecution);
} }
@ -449,7 +452,8 @@ public abstract class StrolchJob implements Runnable, Restrictable {
} else { } else {
long delay = this.future.getDelay(TimeUnit.MILLISECONDS); long delay = this.future.getDelay(TimeUnit.MILLISECONDS);
String nextExecution = ZonedDateTime.now().plus(delay, ChronoUnit.MILLIS) String nextExecution = ZonedDateTime.now()
.plus(delay, ChronoUnit.MILLIS)
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault())); .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()));
jsonObject.addProperty("nextExecution", nextExecution); jsonObject.addProperty("nextExecution", nextExecution);
} }