From 1e3e97737b5a4d5a7d4fd7d6f496381c678a6d13 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Sat, 2 May 2020 23:33:57 +0200 Subject: [PATCH] [New] StrolchJobs now have an ID as well as a name, and model can also specify a delay: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit                                            --- .../java/li/strolch/job/ReloadJobsJob.java | 4 +- .../main/java/li/strolch/job/StrolchJob.java | 11 ++++- .../li/strolch/job/StrolchJobsHandler.java | 48 ++++++++++++++----- .../li/strolch/policy/ReloadPoliciesJob.java | 4 +- .../policy/ReloadPrivilegeHandlerJob.java | 4 +- .../li/strolch/runtime/StrolchConstants.java | 2 + .../model/GroupedParameterizedElement.java | 24 ++++++++++ .../strolch/model/ParameterBagContainer.java | 13 +++++ .../ArchiveExecutedActivitiesJob.java | 7 +-- 9 files changed, 93 insertions(+), 24 deletions(-) diff --git a/li.strolch.agent/src/main/java/li/strolch/job/ReloadJobsJob.java b/li.strolch.agent/src/main/java/li/strolch/job/ReloadJobsJob.java index 22373a2c3..44e76eb08 100644 --- a/li.strolch.agent/src/main/java/li/strolch/job/ReloadJobsJob.java +++ b/li.strolch.agent/src/main/java/li/strolch/job/ReloadJobsJob.java @@ -5,8 +5,8 @@ import li.strolch.privilege.model.PrivilegeContext; public class ReloadJobsJob extends StrolchJob { - public ReloadJobsJob(StrolchAgent agent, String name, JobMode mode) { - super(agent, name, mode); + public ReloadJobsJob(StrolchAgent agent, String id, String name, JobMode mode) { + super(agent, id, name, mode); } @Override diff --git a/li.strolch.agent/src/main/java/li/strolch/job/StrolchJob.java b/li.strolch.agent/src/main/java/li/strolch/job/StrolchJob.java index b27faa659..3b54073fb 100644 --- a/li.strolch.agent/src/main/java/li/strolch/job/StrolchJob.java +++ b/li.strolch.agent/src/main/java/li/strolch/job/StrolchJob.java @@ -46,6 +46,7 @@ public abstract class StrolchJob implements Runnable, Restrictable { protected static final Logger logger = LoggerFactory.getLogger(StrolchJob.class); private final StrolchAgent agent; + private final String id; private final String name; private JobMode mode; @@ -71,8 +72,9 @@ public abstract class StrolchJob implements Runnable, Restrictable { private ConfigureMethod configureMethod; private ZonedDateTime cronStartDate; - public StrolchJob(StrolchAgent agent, String name, JobMode jobMode) { + public StrolchJob(StrolchAgent agent, String id, String name, JobMode jobMode) { this.agent = agent; + this.id = id; this.name = name; this.mode = jobMode; this.first = true; @@ -125,6 +127,10 @@ public abstract class StrolchJob implements Runnable, Restrictable { return this; } + public String getId() { + return this.id; + } + public String getName() { return this.name; } @@ -398,7 +404,8 @@ public abstract class StrolchJob implements Runnable, Restrictable { public JsonObject toJson() { JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty(Tags.Json.NAME, getName()); + jsonObject.addProperty(Tags.Json.ID, this.id); + jsonObject.addProperty(Tags.Json.NAME, this.name); jsonObject.addProperty(Tags.Json.REALM, this.realmName); jsonObject.addProperty("mode", this.mode.name()); jsonObject.addProperty("configureMethod", this.configureMethod == null ? "-" : this.configureMethod.name()); diff --git a/li.strolch.agent/src/main/java/li/strolch/job/StrolchJobsHandler.java b/li.strolch.agent/src/main/java/li/strolch/job/StrolchJobsHandler.java index db8f6b029..e97209092 100644 --- a/li.strolch.agent/src/main/java/li/strolch/job/StrolchJobsHandler.java +++ b/li.strolch.agent/src/main/java/li/strolch/job/StrolchJobsHandler.java @@ -8,11 +8,13 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import li.strolch.agent.api.ComponentContainer; import li.strolch.agent.api.StrolchAgent; import li.strolch.agent.api.StrolchComponent; import li.strolch.model.parameter.DateParameter; +import li.strolch.model.parameter.IntegerParameter; import li.strolch.persistence.api.StrolchTransaction; import li.strolch.privilege.model.Certificate; @@ -46,9 +48,13 @@ public class StrolchJobsHandler extends StrolchComponent { List jobs = new ArrayList<>(); - // copy any already existing programmatically added jobs if (this.jobs != null) { this.jobs.values().forEach(value -> { + + // cancel any schedule jobs + value.cancel(false); + + // copy any already existing programmatically added jobs if (value.getConfigureMethod() == ConfigureMethod.Programmatic) jobs.add(value); }); @@ -61,13 +67,26 @@ public class StrolchJobsHandler extends StrolchComponent { tx.streamResources(TYPE_STROLCH_JOB).forEach(jobRes -> { String className = jobRes.getParameter(PARAM_CLASS_NAME, true).getValue(); - String cron = jobRes.getParameter(PARAM_CRON, true).getValue(); DateParameter startDateP = jobRes.getParameter(PARAM_START_DATE, true); JobMode mode = JobMode.valueOf(jobRes.getParameter(PARAM_MODE, true).getValue()); - StrolchJob job = instantiateJob(className, jobRes.getName(), mode); - job.setConfigureMethod(ConfigureMethod.Model).setMode(mode) - .setCronExpression(cron, startDateP.toZonedDateTime()); + StrolchJob job = instantiateJob(className, jobRes.getId(), jobRes.getName(), mode); + job.setConfigureMethod(ConfigureMethod.Model).setMode(mode); + + if (jobRes.hasParameter(PARAM_CRON)) { + String cron = jobRes.getParameter(PARAM_CRON, true).getValue(); + job.setCronExpression(cron, startDateP.toZonedDateTime()); + } else if (jobRes.hasParameter(PARAM_INITIAL_DELAY) && jobRes.hasParameter(PARAM_DELAY)) { + IntegerParameter initialDelayP = jobRes.getParameter(PARAM_INITIAL_DELAY, true); + IntegerParameter delayP = jobRes.getParameter(PARAM_DELAY, true); + TimeUnit initialDelayUnit = TimeUnit.valueOf(initialDelayP.getUom()); + TimeUnit delayUnit = TimeUnit.valueOf(delayP.getUom()); + job.setDelay(initialDelayP.getValue(), initialDelayUnit, delayP.getValue(), delayUnit); + } else { + logger.error("Job " + jobRes.getId() + + " is inconsistent, as either cron, or initialDelay/delay is missing!"); + return; + } jobs.add(job); logger.info("Added job " + job.getName() + ": " + job.getCron() + " from model."); @@ -77,7 +96,8 @@ public class StrolchJobsHandler extends StrolchComponent { }); StrolchAgent agent = getContainer().getAgent(); - ReloadJobsJob reloadJobsJob = new ReloadJobsJob(agent, ReloadJobsJob.class.getSimpleName(), JobMode.Manual); + ReloadJobsJob reloadJobsJob = new ReloadJobsJob(agent, ReloadJobsJob.class.getSimpleName(), + ReloadJobsJob.class.getSimpleName(), JobMode.Manual); reloadJobsJob.setConfigureMethod(ConfigureMethod.Programmatic); jobs.add(reloadJobsJob); @@ -86,7 +106,7 @@ public class StrolchJobsHandler extends StrolchComponent { } @SuppressWarnings("unchecked") - private StrolchJob instantiateJob(String className, String name, JobMode mode) { + private StrolchJob instantiateJob(String className, String id, String name, JobMode mode) { Class clazz; try { clazz = (Class) Class.forName(className); @@ -94,15 +114,15 @@ public class StrolchJobsHandler extends StrolchComponent { throw new IllegalArgumentException("StrolchJob class " + className + " does not exist!"); } - return instantiateJob(clazz, name, mode); + return instantiateJob(clazz, id, name, mode); } - private StrolchJob instantiateJob(Class clazz, String name, JobMode mode) { + private StrolchJob instantiateJob(Class clazz, String id, String name, JobMode mode) { StrolchJob strolchJob; try { Constructor constructor = clazz - .getConstructor(StrolchAgent.class, String.class, JobMode.class); - strolchJob = constructor.newInstance(getContainer().getAgent(), name, mode); + .getConstructor(StrolchAgent.class, String.class, String.class, JobMode.class); + strolchJob = constructor.newInstance(getContainer().getAgent(), id, name, mode); } catch (Exception e) { throw new IllegalArgumentException("Failed to instantiate job " + clazz.getName(), e); } @@ -110,13 +130,15 @@ public class StrolchJobsHandler extends StrolchComponent { } public StrolchJob registerAndScheduleJob(Class strolchJobClass) { - StrolchJob job = instantiateJob(strolchJobClass, strolchJobClass.getSimpleName(), JobMode.Manual); + StrolchJob job = instantiateJob(strolchJobClass, strolchJobClass.getSimpleName(), + strolchJobClass.getSimpleName(), JobMode.Manual); job.setConfigureMethod(ConfigureMethod.Programmatic); return register(job).schedule(); } public StrolchJob register(Class strolchJobClass) { - StrolchJob job = instantiateJob(strolchJobClass, strolchJobClass.getSimpleName(), JobMode.Manual); + StrolchJob job = instantiateJob(strolchJobClass, strolchJobClass.getSimpleName(), + strolchJobClass.getSimpleName(), JobMode.Manual); job.setConfigureMethod(ConfigureMethod.Programmatic); return register(job); } diff --git a/li.strolch.agent/src/main/java/li/strolch/policy/ReloadPoliciesJob.java b/li.strolch.agent/src/main/java/li/strolch/policy/ReloadPoliciesJob.java index 7dbf29eb1..096f60e79 100644 --- a/li.strolch.agent/src/main/java/li/strolch/policy/ReloadPoliciesJob.java +++ b/li.strolch.agent/src/main/java/li/strolch/policy/ReloadPoliciesJob.java @@ -7,8 +7,8 @@ import li.strolch.privilege.model.PrivilegeContext; public class ReloadPoliciesJob extends StrolchJob { - public ReloadPoliciesJob(StrolchAgent agent, String name, JobMode mode) { - super(agent, name, mode); + public ReloadPoliciesJob(StrolchAgent agent, String id, String name, JobMode mode) { + super(agent, id, name, mode); } @Override diff --git a/li.strolch.agent/src/main/java/li/strolch/policy/ReloadPrivilegeHandlerJob.java b/li.strolch.agent/src/main/java/li/strolch/policy/ReloadPrivilegeHandlerJob.java index b3b64b74f..59bded772 100644 --- a/li.strolch.agent/src/main/java/li/strolch/policy/ReloadPrivilegeHandlerJob.java +++ b/li.strolch.agent/src/main/java/li/strolch/policy/ReloadPrivilegeHandlerJob.java @@ -7,8 +7,8 @@ import li.strolch.privilege.model.PrivilegeContext; public class ReloadPrivilegeHandlerJob extends StrolchJob { - public ReloadPrivilegeHandlerJob(StrolchAgent agent, String name, JobMode jobMode) { - super(agent, name, jobMode); + public ReloadPrivilegeHandlerJob(StrolchAgent agent, String id, String name, JobMode jobMode) { + super(agent, id, name, jobMode); } @Override diff --git a/li.strolch.agent/src/main/java/li/strolch/runtime/StrolchConstants.java b/li.strolch.agent/src/main/java/li/strolch/runtime/StrolchConstants.java index 0282703d8..1a0af935b 100644 --- a/li.strolch.agent/src/main/java/li/strolch/runtime/StrolchConstants.java +++ b/li.strolch.agent/src/main/java/li/strolch/runtime/StrolchConstants.java @@ -75,6 +75,8 @@ public class StrolchConstants extends StrolchModelConstants { public static final String PARAM_CLASS_NAME = "className"; public static final String PARAM_CRON = "cron"; + public static final String PARAM_INITIAL_DELAY = "initialDelay"; + public static final String PARAM_DELAY = "delay"; public static final String PARAM_START_DATE = "startDate"; public static final String PARAM_MODE = "mode"; diff --git a/li.strolch.model/src/main/java/li/strolch/model/GroupedParameterizedElement.java b/li.strolch.model/src/main/java/li/strolch/model/GroupedParameterizedElement.java index af926317b..500172e88 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/GroupedParameterizedElement.java +++ b/li.strolch.model/src/main/java/li/strolch/model/GroupedParameterizedElement.java @@ -596,6 +596,30 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement return bag.hasParameter(paramKey); } + /** + * Returns true if the {@link Parameter} with the given paramKey exists on the {@link ParameterBag} with the id + * {@link StrolchModelConstants#BAG_RELATIONS} + * + * @param paramKey + * the key of the {@link Parameter} to be found + * + * @return true if the {@link Parameter} with the given paramKey exists on the {@link ParameterBag} with the given + * bagKey. False is returned if the {@link ParameterBag} does not exist, or the {@link Parameter} does not exist on + * the {@link ParameterBag} + */ + @Override + public boolean hasRelation(String paramKey) { + if (this.parameterBagMap == null) { + return false; + } + ParameterBag bag = this.parameterBagMap.get(BAG_RELATIONS); + if (bag == null) { + return false; + } + + return bag.hasParameter(paramKey); + } + /** * Returns true if the {@link Parameter} with the given paramKey exists on the {@link ParameterBag} with the given * bagKey diff --git a/li.strolch.model/src/main/java/li/strolch/model/ParameterBagContainer.java b/li.strolch.model/src/main/java/li/strolch/model/ParameterBagContainer.java index 329b73b79..70dcdd00a 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/ParameterBagContainer.java +++ b/li.strolch.model/src/main/java/li/strolch/model/ParameterBagContainer.java @@ -331,6 +331,19 @@ public interface ParameterBagContainer extends StrolchElement { */ boolean hasParameter(String paramKey); + /** + * Returns true if the {@link Parameter} with the given paramKey exists on the {@link ParameterBag} with the id + * {@link StrolchModelConstants#BAG_RELATIONS} + * + * @param paramKey + * the key of the {@link Parameter} to be found + * + * @return true if the {@link Parameter} with the given paramKey exists on the {@link ParameterBag} with the given + * bagKey. False is returned if the {@link ParameterBag} does not exist, or the {@link Parameter} does not exist on + * the {@link ParameterBag} + */ + boolean hasRelation(String paramKey); + /** * Returns true if the {@link Parameter} with the given paramKey exists on the {@link ParameterBag} with the given * bagKey diff --git a/li.strolch.service/src/main/java/li/strolch/execution/ArchiveExecutedActivitiesJob.java b/li.strolch.service/src/main/java/li/strolch/execution/ArchiveExecutedActivitiesJob.java index 90cf616a9..90ba5c89c 100644 --- a/li.strolch.service/src/main/java/li/strolch/execution/ArchiveExecutedActivitiesJob.java +++ b/li.strolch.service/src/main/java/li/strolch/execution/ArchiveExecutedActivitiesJob.java @@ -12,13 +12,14 @@ import li.strolch.privilege.model.PrivilegeContext; public class ArchiveExecutedActivitiesJob extends StrolchJob { - public ArchiveExecutedActivitiesJob(StrolchAgent agent, String name, JobMode jobMode) { - super(agent, name, jobMode); + public ArchiveExecutedActivitiesJob(StrolchAgent agent, String id, String name, JobMode jobMode) { + super(agent, id, name, jobMode); } public ArchiveExecutedActivitiesJob(StrolchAgent agent, JobMode jobMode, long initialDelay, TimeUnit initialDelayTimeUnit, long delay, TimeUnit delayTimeUnit) { - super(agent, ArchiveExecutedActivitiesJob.class.getSimpleName(), jobMode); + super(agent, ArchiveExecutedActivitiesJob.class.getSimpleName(), + ArchiveExecutedActivitiesJob.class.getSimpleName(), jobMode); setDelay(initialDelay, initialDelayTimeUnit, delay, delayTimeUnit); }