Skip to content

Commit

Permalink
Merge pull request #294 from wttech/feature/make-auto-run-script-feat…
Browse files Browse the repository at this point in the history
…ure-to-take-run-modes-into-account

added run modes into account to auto-run script feature
  • Loading branch information
dprzybyl authored Nov 22, 2021
2 parents dc3db2c + 7322266 commit 784751e
Show file tree
Hide file tree
Showing 19 changed files with 142 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.cognifide.apm.api.scripts;

import java.util.Date;
import java.util.Set;

public interface Script {

Expand All @@ -40,6 +41,8 @@ public interface Script {

LaunchEnvironment getLaunchEnvironment();

Set<String> getLaunchRunModes();

String getLaunchHook();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.cognifide.apm.api.scripts;

import java.util.Date;
import java.util.Set;

public class TransientScript implements MutableScript {

Expand Down Expand Up @@ -66,6 +67,11 @@ public LaunchEnvironment getLaunchEnvironment() {
return LaunchEnvironment.ALL;
}

@Override
public Set<String> getLaunchRunModes() {
return null;
}

@Override
public String getLaunchHook() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.settings.SlingSettingsService;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Modified;
Expand Down Expand Up @@ -65,6 +66,9 @@ public class ScheduledScriptLauncher extends AbstractLauncher implements Runnabl
@Reference
private InstanceTypeProvider instanceTypeProvider;

@Reference
private SlingSettingsService slingSettings;

@Reference
private ResourceResolverFactory resolverFactory;

Expand All @@ -85,7 +89,7 @@ public void run() {

private void runScheduled(ResourceResolver resolver) throws PersistenceException {
LaunchEnvironment environment = instanceTypeProvider.isOnAuthor() ? AUTHOR : PUBLISH;
List<Script> scripts = scriptFinder.findAll(onSchedule(environment, new Date()), resolver);
List<Script> scripts = scriptFinder.findAll(onSchedule(environment, slingSettings, new Date()), resolver);
processScripts(scripts, resolver, LauncherType.SCHEDULED);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.settings.SlingSettingsService;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

Expand All @@ -66,6 +67,9 @@ public class StartupScriptLauncher extends AbstractLauncher {
@Reference
private InstanceTypeProvider instanceTypeProvider;

@Reference
private SlingSettingsService slingSettings;

@Reference
private VersionService versionService;

Expand All @@ -84,8 +88,8 @@ private void process(ResourceResolver resolver) {

private void executeScripts(LaunchEnvironment currentEnvironment, ResourceResolver resolver) {
List<Script> scripts = new ArrayList<>();
scripts.addAll(scriptFinder.findAll(onStartup(currentEnvironment), resolver));
scripts.addAll(modifiedScriptFinder.findAll(onStartupIfModified(currentEnvironment), resolver));
scripts.addAll(scriptFinder.findAll(onStartup(currentEnvironment, slingSettings), resolver));
scripts.addAll(modifiedScriptFinder.findAll(onStartupIfModified(currentEnvironment, slingSettings), resolver));

scripts.forEach(script -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,16 @@ public class LaunchMetadata {
private final boolean executionEnabled;
private final LaunchMode launchMode;
private final LaunchEnvironment launchEnvironment;
private final String[] launchRunModes;
private final String executionHook;
private final LocalDateTime executionSchedule;

public static LaunchMetadata disabled() {
return new LaunchMetadata(false, LaunchMode.ON_DEMAND, null, null, null);
}

public static LaunchMetadata onDemand() {
return new LaunchMetadata(true, LaunchMode.ON_DEMAND, null, null, null);
}

public static LaunchMetadata onModify() {
return new LaunchMetadata(true, LaunchMode.ON_STARTUP_IF_MODIFIED, null, null, null);
}

public static LaunchMetadata onStart() {
return new LaunchMetadata(true, LaunchMode.ON_STARTUP, null, null, null);
}

public static LaunchMetadata onHook(LaunchEnvironment launchEnvironment, String executionHook) {
return new LaunchMetadata(true, LaunchMode.ON_DEMAND, launchEnvironment, executionHook, null);
}

public static LaunchMetadata onSchedule(LocalDateTime executionSchedule) {
return new LaunchMetadata(true, LaunchMode.ON_DEMAND, null, null, executionSchedule);
}

public LaunchMetadata(boolean executionEnabled, LaunchMode launchMode,
LaunchEnvironment launchEnvironment, String executionHook, LocalDateTime executionSchedule) {
public LaunchMetadata(boolean executionEnabled, LaunchMode launchMode, LaunchEnvironment launchEnvironment,
String[] launchRunModes, String executionHook, LocalDateTime executionSchedule) {
this.executionEnabled = executionEnabled;
this.launchMode = launchMode;
this.launchEnvironment = launchEnvironment;
this.launchRunModes = launchRunModes;
this.executionHook = executionHook;
this.executionSchedule = executionSchedule;
}
Expand All @@ -77,6 +55,10 @@ public LaunchEnvironment getLaunchEnvironment() {
return launchEnvironment;
}

public String[] getLaunchRunModes() {
return launchRunModes;
}

public String getExecutionHook() {
return executionHook;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,53 @@
import com.cognifide.apm.api.scripts.LaunchMode;
import com.cognifide.apm.api.scripts.Script;
import java.util.Date;
import java.util.Set;
import java.util.function.Predicate;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.settings.SlingSettingsService;

/**
* Due to the ResourceResolver dependency these filters should not be used lazy
* i.e. in a context where the resolver passed as an argument is no longer alive.
*/
public class ScriptFilters {

public static Predicate<Script> onInstall(LaunchEnvironment environment, String currentHook) {
public static Predicate<Script> onInstall(LaunchEnvironment environment, SlingSettingsService slingSettings, String currentHook) {
return enabled()
.and(withLaunchMode(LaunchMode.ON_INSTALL))
.and(withLaunchEnvironment(environment))
.and(withLaunchRunModes(slingSettings.getRunModes()))
.and(withLaunchHook(currentHook));
}

public static Predicate<Script> onInstallIfModified(LaunchEnvironment environment, String currentHook) {
public static Predicate<Script> onInstallIfModified(LaunchEnvironment environment, SlingSettingsService slingSettings, String currentHook) {
return enabled()
.and(withLaunchMode(LaunchMode.ON_INSTALL_IF_MODIFIED))
.and(withLaunchEnvironment(environment))
.and(withLaunchRunModes(slingSettings.getRunModes()))
.and(withLaunchHook(currentHook));
}

public static Predicate<Script> onSchedule(LaunchEnvironment environment, Date date) {
public static Predicate<Script> onSchedule(LaunchEnvironment environment, SlingSettingsService slingSettings, Date date) {
return enabled()
.and(withLaunchMode(LaunchMode.ON_SCHEDULE))
.and(withLaunchEnvironment(environment))
.and(withLaunchRunModes(slingSettings.getRunModes()))
.and(script -> script.getLastExecuted() == null && script.getLaunchSchedule().before(date));
}

public static Predicate<Script> onStartup(LaunchEnvironment environment) {
public static Predicate<Script> onStartup(LaunchEnvironment environment, SlingSettingsService slingSettings) {
return enabled()
.and(withLaunchMode(LaunchMode.ON_STARTUP))
.and(withLaunchEnvironment(environment));
.and(withLaunchEnvironment(environment))
.and(withLaunchRunModes(slingSettings.getRunModes()));
}

public static Predicate<Script> onStartupIfModified(LaunchEnvironment environment) {
public static Predicate<Script> onStartupIfModified(LaunchEnvironment environment, SlingSettingsService slingSettings) {
return enabled()
.and(withLaunchMode(LaunchMode.ON_STARTUP_IF_MODIFIED))
.and(withLaunchEnvironment(environment));
.and(withLaunchEnvironment(environment))
.and(withLaunchRunModes(slingSettings.getRunModes()));
}

private static Predicate<Script> withLaunchHook(String currentHook) {
Expand All @@ -74,6 +81,11 @@ private static Predicate<Script> withLaunchEnvironment(LaunchEnvironment environ
|| environment == script.getLaunchEnvironment();
}

private static Predicate<? super Script> withLaunchRunModes(Set<String> runModes) {
return script -> script.getLaunchRunModes() == null
|| runModes.containsAll(script.getLaunchRunModes());
}

private static Predicate<Script> withLaunchMode(final LaunchMode mode) {
return script -> script.getLaunchMode() == mode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
import com.cognifide.apm.core.utils.ResourceMixinUtil;
import com.day.cq.commons.jcr.JcrConstants;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.commons.lang.BooleanUtils;
Expand Down Expand Up @@ -66,6 +69,11 @@ public class ScriptModel implements MutableScript {
@Optional
private String launchEnvironment;

@Inject
@Named(ScriptNode.APM_LAUNCH_RUN_MODES)
@Optional
private String[] launchRunModes;

@Inject
@Named(ScriptNode.APM_LAUNCH_HOOK)
@Optional
Expand Down Expand Up @@ -145,6 +153,11 @@ public LaunchEnvironment getLaunchEnvironment() {
});
}

@Override
public Set<String> getLaunchRunModes() {
return launchRunModes == null ? null : Sets.newHashSet(launchRunModes);
}

@Override
public Date getLaunchSchedule() {
return launchSchedule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public final class ScriptNode {

public static final String APM_LAUNCH_ENVIRONMENT = "apm:launchEnvironment";

public static final String APM_LAUNCH_RUN_MODES = "apm:launchRunModes";

public static final String APM_LAUNCH_HOOK = "apm:launchHook";

public static final String APM_LAUNCH_SCHEDULE = "apm:launchSchedule";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private Script saveScript(FileDescriptor descriptor, LaunchMetadata launchMetada
fileNode.setProperty(ScriptNode.APM_LAUNCH_ENABLED, launchMetadata.isExecutionEnabled());
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_MODE, launchMetadata.getLaunchMode());
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_ENVIRONMENT, launchMetadata.getLaunchEnvironment());
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_RUN_MODES, launchMetadata.getLaunchRunModes());
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_HOOK, launchMetadata.getExecutionHook());
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_SCHEDULE, launchMetadata.getExecutionSchedule());
removeProperty(fileNode, ScriptNode.APM_LAST_EXECUTED);
Expand All @@ -135,6 +136,8 @@ private void setOrRemoveProperty(Node node, String name, Object value) throws Re
calendar.set(localDateTime.getYear(), localDateTime.getMonthValue() - 1, localDateTime.getDayOfMonth(),
localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond());
node.setProperty(name, calendar);
} else if (value instanceof String[]) {
node.setProperty(name, (String[]) value);
} else {
node.setProperty(name, value.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import lombok.Getter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;
import org.apache.sling.api.resource.Resource;
Expand Down Expand Up @@ -100,6 +101,9 @@ protected void afterCreated() {
this.runs.add(createScriptRun("runOnPublish", script, scriptHistory.getLastRemoteRun()));
this.launchMode = label(script.getLaunchMode());
this.launchEnvironment = label(script.getLaunchEnvironment());
if (CollectionUtils.isNotEmpty(script.getLaunchRunModes())) {
this.launchEnvironment += "+" + StringUtils.join(script.getLaunchRunModes(), ",");
}
this.isLaunchEnabled = script.isLaunchEnabled();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ class ScriptUploadForm @Inject constructor(
@param:RequestParameter(ScriptNode.APM_LAUNCH_ENABLED) val launchEnabled: Boolean,
@param:RequestParameter(ScriptNode.APM_LAUNCH_MODE) val launchMode: LaunchMode,
@param:RequestParameter(ScriptNode.APM_LAUNCH_ENVIRONMENT) val launchEnvironment: LaunchEnvironment?,
@param:RequestParameter(ScriptNode.APM_LAUNCH_RUN_MODES) val launchRunModes: Array<String>?,
@param:RequestParameter(ScriptNode.APM_LAUNCH_HOOK) val launchHook: String?,
@param:RequestParameter(ScriptNode.APM_LAUNCH_SCHEDULE) @param:DateFormat("yyyy-MM-dd'T'HH:mm:ss") val launchSchedule: LocalDateTime?
) {
fun toLaunchMetadata(): LaunchMetadata {
return LaunchMetadata(launchEnabled, launchMode, launchEnvironment, launchHook, launchSchedule)
return LaunchMetadata(launchEnabled, launchMode, launchEnvironment, launchRunModes, launchHook, launchSchedule)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package com.cognifide.apm.core.endpoints.params


import com.cognifide.apm.core.Property
import com.google.common.primitives.Ints
import org.apache.commons.lang.StringUtils
Expand Down Expand Up @@ -77,6 +76,7 @@ class RequestParameterInjector : Injector, StaticInjectAnnotationProcessorFactor
fieldClass == InputStream::class.java -> parameterValue.inputStream
fieldClass == LocalDateTime::class.java -> toLocalDateTime(annotatedElement, parameterValue)
Enum::class.java.isAssignableFrom(fieldClass) -> toEnum(fieldClass, parameterValue)
fieldClass.canonicalName == "java.lang.String[]" -> parameterValue.string.split(",").toTypedArray()
else -> parameterValue.string
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ class ReferenceFinder(

override fun getLaunchEnvironment(): LaunchEnvironment? = null

override fun getLaunchRunModes(): Set<String>? = null

override fun getLaunchHook(): String? = null

override fun getLaunchSchedule(): Date? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import org.apache.jackrabbit.vault.packaging.InstallContext
import org.apache.jackrabbit.vault.packaging.PackageException
import org.apache.sling.api.resource.ResourceResolver
import org.apache.sling.api.resource.ResourceResolverFactory
import org.apache.sling.settings.SlingSettingsService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.util.*
Expand Down Expand Up @@ -75,10 +76,11 @@ class ApmInstallHook : OsgiAwareInstallHook() {
val scriptManager = getService(ScriptManager::class.java)
val scriptFinder = getService(ScriptFinder::class.java)
val modifiedScriptFinder = getService(ModifiedScriptFinder::class.java)
val slingSettings = getService(SlingSettingsService::class.java)

val scripts = mutableListOf<Script>()
scripts.addAll(scriptFinder.findAll(onInstall(currentEnvironment, currentHook), resolver))
scripts.addAll(modifiedScriptFinder.findAll(onInstallIfModified(currentEnvironment, currentHook), resolver))
scripts.addAll(scriptFinder.findAll(onInstall(currentEnvironment, slingSettings, currentHook), resolver))
scripts.addAll(modifiedScriptFinder.findAll(onInstallIfModified(currentEnvironment, slingSettings, currentHook), resolver))
scripts.forEach { script ->
val result: ExecutionResult = scriptManager.process(script, ExecutionMode.AUTOMATIC_RUN, resolver)
logStatus(context, script.path, result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- apm:launchEnabled (boolean)
- apm:launchMode (string)
- apm:launchEnvironment (string)
- apm:launchRunModes (string) multiple
- apm:launchHook (string)
- apm:launchSchedule (date)
- apm:lastExecuted (date)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'apm:launchEnabled',
'apm:launchMode',
'apm:launchEnvironment',
'apm:launchRunModes',
'apm:launchHook',
'apm:launchSchedule',
];
Expand Down Expand Up @@ -97,7 +98,9 @@

const formData = new FormData();
$.each(fieldNames, (index, fieldName) => {
const originalValue = originalFormData.get(fieldName);
const originalValue = fieldName === 'apm:launchRunModes'
? originalFormData.getAll(fieldName)
: originalFormData.get(fieldName);
if (originalValue) {
formData.set(fieldName, originalValue);
}
Expand Down
Loading

0 comments on commit 784751e

Please sign in to comment.