Skip to content

Commit

Permalink
Merge pull request #411 from wttech/add-cron-support
Browse files Browse the repository at this point in the history
added CRON expression support
  • Loading branch information
dprzybyl committed Oct 19, 2023
2 parents 5edfa4b + bf0b093 commit 1c38e48
Show file tree
Hide file tree
Showing 20 changed files with 302 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public enum LaunchMode {
*/
ON_SCHEDULE,

/**
* Executed on CRON expression
*/
ON_CRON_EXPRESSION,

/**
* Executed always on bundle start
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public interface Script {
*/
Date getLaunchSchedule();

/**
* Get CRON expression
*/
String getCronExpression();

/**
* Get last execution date
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public Date getLaunchSchedule() {
return null;
}

@Override
public String getCronExpression() {
return null;
}

@Override
public Date getLastExecuted() {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*-
* ========================LICENSE_START=================================
* AEM Permission Management
* %%
* Copyright (C) 2013 Wunderman Thompson Technology
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =========================LICENSE_END==================================
*/
package com.cognifide.apm.core.launchers;

import com.cognifide.apm.api.scripts.Script;
import com.cognifide.apm.api.services.ScriptFinder;
import com.cognifide.apm.api.services.ScriptManager;
import com.cognifide.apm.core.services.ResourceResolverProvider;
import com.cognifide.apm.core.utils.RuntimeUtils;
import com.cognifide.apm.core.utils.sling.SlingHelper;
import java.util.Collections;

public class ApmInstallService extends AbstractLauncher implements Runnable {

private final String scriptPath;

private final ResourceResolverProvider resolverProvider;

private final ScriptManager scriptManager;

private final ScriptFinder scriptFinder;

public ApmInstallService(String scriptPath, ResourceResolverProvider resolverProvider, ScriptManager scriptManager, ScriptFinder scriptFinder) {
this.scriptPath = scriptPath;
this.resolverProvider = resolverProvider;
this.scriptManager = scriptManager;
this.scriptFinder = scriptFinder;
}

@Override
public void run() {
SlingHelper.operateTraced(resolverProvider, resolver -> {
if (RuntimeUtils.isMutableContentInstance(resolver)) {
Script script = scriptFinder.find(scriptPath, resolver);
processScripts(Collections.singletonList(script), resolver);
}
});
}

@Override
protected ScriptManager getScriptManager() {
return scriptManager;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ public class LaunchMetadata {
private final String[] launchRunModes;
private final String executionHook;
private final LocalDateTime executionSchedule;
private final String cronExpression;

public LaunchMetadata(boolean executionEnabled, LaunchMode launchMode, LaunchEnvironment launchEnvironment,
String[] launchRunModes, String executionHook, LocalDateTime executionSchedule) {
String[] launchRunModes, String executionHook, LocalDateTime executionSchedule, String cronExpression) {
this.executionEnabled = executionEnabled;
this.launchMode = launchMode;
this.launchEnvironment = launchEnvironment;
this.launchRunModes = launchRunModes;
this.executionHook = executionHook;
this.executionSchedule = executionSchedule;
this.cronExpression = cronExpression;
}

public boolean isExecutionEnabled() {
Expand Down Expand Up @@ -75,4 +77,8 @@ public String getExecutionHook() {
public LocalDateTime getExecutionSchedule() {
return executionSchedule;
}

public String getCronExpression() {
return cronExpression;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.cognifide.apm.api.scripts.LaunchMode;
import com.cognifide.apm.api.scripts.Script;
import com.cognifide.apm.api.services.RunModesProvider;
import java.util.Date;
import java.util.Set;
import java.util.function.Predicate;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -50,12 +49,11 @@ public static Predicate<Script> onInstallIfModified(LaunchEnvironment environmen
.and(withLaunchHook(currentHook));
}

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

public static Predicate<Script> onStartup(LaunchEnvironment environment, RunModesProvider runModesProvider) {
Expand All @@ -81,16 +79,29 @@ private static Predicate<Script> withLaunchEnvironment(LaunchEnvironment environ
|| environment == script.getLaunchEnvironment();
}

private static Predicate<? super Script> withLaunchRunModes(Set<String> runModes) {
private static Predicate<Script> withLaunchEnvironment(RunModesProvider runModesProvider) {
LaunchEnvironment environment = LaunchEnvironment.of(runModesProvider);
return withLaunchEnvironment(environment);
}

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

private static Predicate<Script> withLaunchMode(final LaunchMode mode) {
private static Predicate<Script> withLaunchMode(LaunchMode mode) {
return script -> script.getLaunchMode() == mode;
}

private static Predicate<Script> withSchedule() {
return script -> script.getLaunchMode() == LaunchMode.ON_SCHEDULE && script.getLaunchSchedule() != null;
}

private static Predicate<Script> withCronExpression() {
return script -> script.getLaunchMode() == LaunchMode.ON_CRON_EXPRESSION && StringUtils.isNotEmpty(script.getCronExpression());
}

private static Predicate<Script> enabled() {
return script -> script.isLaunchEnabled();
return Script::isLaunchEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import javax.inject.Named;
import javax.jcr.RepositoryException;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
Expand Down Expand Up @@ -93,6 +94,10 @@ public class ScriptModel implements MutableScript {
@Named(ScriptNode.APM_LAUNCH_SCHEDULE)
private Date launchSchedule;

@Inject
@Named(ScriptNode.APM_LAUNCH_CRON_EXPRESSION)
private String cronExpression;

@Inject
@Named(ScriptNode.APM_LAST_EXECUTED)
private Date lastExecuted;
Expand Down Expand Up @@ -168,6 +173,11 @@ public Date getLaunchSchedule() {
return launchSchedule;
}

@Override
public String getCronExpression() {
return StringUtils.defaultString(cronExpression);
}

@Override
public boolean isLaunchEnabled() {
return BooleanUtils.isNotFalse(launchEnabled);
Expand Down Expand Up @@ -277,6 +287,7 @@ public boolean equals(Object obj) {
&& Arrays.equals(launchRunModes, that.launchRunModes)
&& Objects.equals(launchHook, that.launchHook)
&& Objects.equals(launchSchedule, that.launchSchedule)
&& Objects.equals(cronExpression, that.cronExpression)
&& Objects.equals(checksum, that.checksum)
&& Objects.equals(verified, that.verified);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public final class ScriptNode {

public static final String APM_LAUNCH_SCHEDULE = "apm:launchSchedule";

public static final String APM_LAUNCH_CRON_EXPRESSION = "apm:launchCronExpression";

public static final String APM_LAST_EXECUTED = "apm:lastExecuted";

public static final String APM_CHECKSUM = "apm:checksum";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ private Script saveScript(FileDescriptor descriptor, LaunchMetadata launchMetada
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());
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_CRON_EXPRESSION, launchMetadata.getCronExpression());
removeProperty(fileNode, ScriptNode.APM_LAST_EXECUTED);
JcrUtils.setLastModified(fileNode, Calendar.getInstance());
session.save();
Expand Down
Loading

0 comments on commit 1c38e48

Please sign in to comment.