feat(plugin): add new priority attr on Plugin annotation (#10975)

This commit is contained in:
François Delbrayelle
2025-09-04 14:21:38 +02:00
committed by GitHub
parent 88acc91323
commit 6e21d650f9
4 changed files with 40 additions and 11 deletions

View File

@@ -103,11 +103,13 @@ public class DefaultPluginRegistry implements PluginRegistry {
*/ */
@Override @Override
public void registerIfAbsent(final Path pluginPath) { public void registerIfAbsent(final Path pluginPath) {
long start = System.currentTimeMillis();
if (isPluginPathValid(pluginPath) && !isPluginPathScanned(pluginPath)) { if (isPluginPathValid(pluginPath) && !isPluginPathScanned(pluginPath)) {
List<RegisteredPlugin> scanned = scanner.scan(pluginPath); List<RegisteredPlugin> scanned = scanner.scan(pluginPath);
scanned.forEach(this::register); scanned.forEach(this::register);
scannedPluginPaths.add(pluginPath); scannedPluginPaths.add(pluginPath);
} }
log.debug("Registered if absent plugins from path {} in {} ms", pluginPath, System.currentTimeMillis() - start);
} }
private boolean isPluginPathScanned(final Path pluginPath) { private boolean isPluginPathScanned(final Path pluginPath) {
@@ -119,10 +121,12 @@ public class DefaultPluginRegistry implements PluginRegistry {
*/ */
@Override @Override
public void register(final Path pluginPath) { public void register(final Path pluginPath) {
long start = System.currentTimeMillis();
if (isPluginPathValid(pluginPath)) { if (isPluginPathValid(pluginPath)) {
List<RegisteredPlugin> scanned = scanner.scan(pluginPath); List<RegisteredPlugin> scanned = scanner.scan(pluginPath);
scanned.forEach(this::register); scanned.forEach(this::register);
} }
log.debug("Registered plugins from path {} in {} ms", pluginPath, System.currentTimeMillis() - start);
} }
/** /**

View File

@@ -10,7 +10,12 @@ class PluginTest {
@Test @Test
void shouldReturnTrueForInternal() { void shouldReturnTrueForInternal() {
Assertions.assertTrue( io.kestra.core.models.Plugin.isInternal(TestPlugin.class)); Assertions.assertTrue(io.kestra.core.models.Plugin.isInternal(TestPlugin.class));
}
@Test
void shouldReturnTrueForPrimary() {
Assertions.assertTrue(io.kestra.core.models.Plugin.isPrimary(TestPlugin.class));
} }
@Test @Test
@@ -18,7 +23,7 @@ class PluginTest {
Assertions.assertEquals(Optional.of("test"), io.kestra.core.models.Plugin.getId(TestPlugin.class)); Assertions.assertEquals(Optional.of("test"), io.kestra.core.models.Plugin.getId(TestPlugin.class));
} }
@Plugin(internal = true) @Plugin(internal = true, priority = Plugin.Priority.PRIMARY)
@Plugin.Id("test") @Plugin.Id("test")
public static class TestPlugin implements io.kestra.core.models.Plugin { public static class TestPlugin implements io.kestra.core.models.Plugin {

View File

@@ -1,7 +1,6 @@
package io.kestra.core.models; package io.kestra.core.models;
import io.kestra.core.models.annotations.Plugin.Id; import io.kestra.core.models.annotations.Plugin.Id;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import java.util.Arrays; import java.util.Arrays;
@@ -18,7 +17,7 @@ public interface Plugin {
/** /**
* Gets the type of this plugin. * Gets the type of this plugin.
* *
* @return the string type of the plugin. * @return the string type of the plugin.
*/ */
@NotNull @NotNull
default String getType() { default String getType() {
@@ -28,12 +27,12 @@ public interface Plugin {
/** /**
* Static helper method to get the aliases of a given plugin. * Static helper method to get the aliases of a given plugin.
* *
* @param plugin The plugin type. * @param plugin The plugin type.
* @return {@code true} if the plugin is internal. * @return {@code true} if the plugin is internal.
*/ */
static Set<String> getAliases(final Class<?> plugin) { static Set<String> getAliases(final Class<?> plugin) {
io.kestra.core.models.annotations.Plugin annotation = plugin.getAnnotation(io.kestra.core.models.annotations.Plugin.class); io.kestra.core.models.annotations.Plugin annotation = plugin.getAnnotation(io.kestra.core.models.annotations.Plugin.class);
return Optional.ofNullable(annotation) return Optional.ofNullable(annotation)
.map(io.kestra.core.models.annotations.Plugin::aliases) .map(io.kestra.core.models.annotations.Plugin::aliases)
.stream() .stream()
.flatMap(Arrays::stream) .flatMap(Arrays::stream)
@@ -43,8 +42,8 @@ public interface Plugin {
/** /**
* Static helper method to check whether a given plugin is internal. * Static helper method to check whether a given plugin is internal.
* *
* @param plugin The plugin type. * @param plugin The plugin type.
* @return {@code true} if the plugin is internal. * @return {@code true} if the plugin is internal.
*/ */
static boolean isInternal(final Class<?> plugin) { static boolean isInternal(final Class<?> plugin) {
Objects.requireNonNull(plugin, "Cannot check if a plugin is internal from null"); Objects.requireNonNull(plugin, "Cannot check if a plugin is internal from null");
@@ -57,8 +56,8 @@ public interface Plugin {
/** /**
* Static helper method to check whether a given plugin is deprecated. * Static helper method to check whether a given plugin is deprecated.
* *
* @param plugin The plugin type. * @param plugin The plugin type.
* @return {@code true} if the plugin is deprecated. * @return {@code true} if the plugin is deprecated.
*/ */
static boolean isDeprecated(final Class<?> plugin) { static boolean isDeprecated(final Class<?> plugin) {
Objects.requireNonNull(plugin, "Cannot check if a plugin is deprecated from null"); Objects.requireNonNull(plugin, "Cannot check if a plugin is deprecated from null");
@@ -66,6 +65,20 @@ public interface Plugin {
return annotation != null; return annotation != null;
} }
/**
* Static helper method to check whether a given plugin has PRIMARY priority.
*
* @param plugin The plugin class.
* @return {@code true} if the plugin is annotated with {@link Plugin} and its priority is PRIMARY.
*/
static boolean isPrimary(final Class<?> plugin) {
Objects.requireNonNull(plugin, "Cannot check priority on null class");
io.kestra.core.models.annotations.Plugin annotation = plugin.getAnnotation(io.kestra.core.models.annotations.Plugin.class);
return Optional.ofNullable(annotation)
.map(a -> a.priority() == io.kestra.core.models.annotations.Plugin.Priority.PRIMARY)
.orElse(false);
}
/** /**
* Static helper method to get the id of a plugin. * Static helper method to get the id of a plugin.
* *

View File

@@ -36,6 +36,13 @@ public @interface Plugin {
*/ */
String[] aliases() default {}; String[] aliases() default {};
Priority priority() default Priority.SECONDARY;
enum Priority {
PRIMARY,
SECONDARY
}
@Documented @Documented
@Inherited @Inherited
@Retention(RUNTIME) @Retention(RUNTIME)