Skip to content

Commit

Permalink
added cron support
Browse files Browse the repository at this point in the history
  • Loading branch information
dprzybyl committed Oct 18, 2023
1 parent ccb65b8 commit 632607d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 125 deletions.

This file was deleted.

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,18 +49,10 @@ 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));
}

public static Predicate<Script> onCronExpression(LaunchEnvironment environment, RunModesProvider runModesProvider) {
return enabled()
.and(withLaunchMode(LaunchMode.ON_CRON_EXPRESSION))
.and(withLaunchEnvironment(environment))
.and(withSchedule().or(withCronExpression()))
.and(withLaunchEnvironment(runModesProvider))
.and(withLaunchRunModes(runModesProvider.getRunModes()));
}

Expand All @@ -88,16 +79,30 @@ 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 script -> script.getLaunchEnvironment() == LaunchEnvironment.ALL
|| environment == script.getLaunchEnvironment();
}

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
@@ -1,7 +1,10 @@
package com.cognifide.apm.core.services;

import static com.cognifide.apm.core.scripts.ScriptFilters.onScheduleOrCronExpression;

import com.cognifide.apm.api.scripts.LaunchMode;
import com.cognifide.apm.api.scripts.Script;
import com.cognifide.apm.api.services.RunModesProvider;
import com.cognifide.apm.api.services.ScriptFinder;
import com.cognifide.apm.api.services.ScriptManager;
import com.cognifide.apm.core.Apm;
Expand All @@ -24,6 +27,7 @@
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;

@Component(
Expand All @@ -47,6 +51,9 @@ public class ScriptsResourceChangeListener implements ResourceChangeListener {
@Reference
private ScriptManager scriptManager;

@Reference
private RunModesProvider runModesProvider;

private Map<String, RegisterScript> registeredScripts;

@Activate
Expand All @@ -56,15 +63,18 @@ public void activate() {
BundleContext bundleContext = currentBundle.getBundleContext();

SlingHelper.operateTraced(resolverProvider, resolver ->
scriptFinder.findAll(script -> {
LaunchMode launchMode = script.getLaunchMode();
return launchMode == LaunchMode.ON_SCHEDULE && script.getLaunchSchedule() != null
|| launchMode == LaunchMode.ON_CRON_EXPRESSION && StringUtils.isNotEmpty(script.getCronExpression());
}, resolver)
scriptFinder.findAll(onScheduleOrCronExpression(runModesProvider), resolver)
.forEach(script -> registerService(script, bundleContext))
);
}

@Deactivate
public void deactivate() {
registeredScripts.values()
.forEach(registeredScript -> registeredScript.registration.unregister());
registeredScripts.clear();
}

@Override
public void onChange(List<ResourceChange> list) {
Bundle currentBundle = FrameworkUtil.getBundle(ScriptsResourceChangeListener.class);
Expand All @@ -76,7 +86,7 @@ public void onChange(List<ResourceChange> list) {
.forEach(resourceChange -> {
if (resourceChange.getType() == ResourceChange.ChangeType.ADDED) {
Script script = scriptFinder.find(resourceChange.getPath(), resolver);
if (script != null) {
if (onScheduleOrCronExpression(runModesProvider).test(script)) {
registerService(script, bundleContext);
}
} else if (resourceChange.getType() == ResourceChange.ChangeType.REMOVED) {
Expand All @@ -88,7 +98,7 @@ public void onChange(List<ResourceChange> list) {
} else if (resourceChange.getType() == ResourceChange.ChangeType.CHANGED) {
Script script = scriptFinder.find(resourceChange.getPath(), resolver);
RegisterScript registeredScript = registeredScripts.get(resourceChange.getPath());
if (script != null && !Objects.equals(script, registeredScript.script)) {
if (onScheduleOrCronExpression(runModesProvider).test(script) && !Objects.equals(script, registeredScript.script)) {
registeredScript.registration.unregister();
registeredScripts.remove(resourceChange.getPath());
registerService(script, bundleContext);
Expand All @@ -111,11 +121,11 @@ private void registerService(Script script, BundleContext bundleContext) {
registeredScripts.put(script.getPath(), new RegisterScript(script, registration));
}

private class RegisterScript {
private static class RegisterScript {

private Script script;
private final Script script;

private ServiceRegistration<Runnable> registration;
private final ServiceRegistration<Runnable> registration;

public RegisterScript(Script script, ServiceRegistration<Runnable> registration) {
this.script = script;
Expand Down

0 comments on commit 632607d

Please sign in to comment.