Skip to content

Commit

Permalink
fix(core): provide default value for variable configuration
Browse files Browse the repository at this point in the history
mostly for unit test
  • Loading branch information
tchiotludo committed Jan 21, 2022
1 parent 00ce27d commit 5675a3f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cli/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ kestra:
env-vars-prefix: KESTRA_
globals: {}
disable-handlebars: true
cacheEnabled: true
cacheSize: 1000
cache-enabled: true
cache-size: 1000

metrics:
prefix: kestra
Expand Down
22 changes: 15 additions & 7 deletions core/src/main/java/io/kestra/core/runners/VariableRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.io.StringWriter;
import java.io.Writer;
import java.util.*;

import io.micronaut.core.annotation.Nullable;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import lombok.Getter;
Expand All @@ -35,10 +37,10 @@ public class VariableRenderer {

@SuppressWarnings("unchecked")
@Inject
public VariableRenderer(ApplicationContext applicationContext, VariableConfiguration variableConfiguration) {
this.variableConfiguration = variableConfiguration;
public VariableRenderer(ApplicationContext applicationContext, @Nullable VariableConfiguration variableConfiguration) {
this.variableConfiguration = variableConfiguration != null ? variableConfiguration : new VariableConfiguration();

if (!variableConfiguration.getDisableHandlebars()) {
if (!this.variableConfiguration.getDisableHandlebars()) {
this.handlebars = new Handlebars()
.with(EscapingStrategy.NOOP)
.registerHelpers(ConditionalHelpers.class)
Expand Down Expand Up @@ -71,16 +73,16 @@ public VariableRenderer(ApplicationContext applicationContext, VariableConfigura
PebbleEngine.Builder pebbleBuilder = new PebbleEngine.Builder()
.registerExtensionCustomizer(ExtensionCustomizer::new)
.strictVariables(true)
.cacheActive(variableConfiguration.getCacheEnabled())
.cacheActive(this.variableConfiguration.getCacheEnabled())

.newLineTrimming(false)
.autoEscaping(false);

applicationContext.getBeansOfType(AbstractExtension.class)
.forEach(pebbleBuilder::extension);

if (variableConfiguration.getCacheEnabled()) {
pebbleBuilder.templateCache(new PebbleLruCache(variableConfiguration.getCacheSize()));
if (this.variableConfiguration.getCacheEnabled()) {
pebbleBuilder.templateCache(new PebbleLruCache(this.variableConfiguration.getCacheSize()));
}

pebbleEngine = pebbleBuilder.build();
Expand All @@ -103,7 +105,7 @@ public String recursiveRender(String inline, Map<String, Object> variables) thro
compiledTemplate.evaluate(writer, variables);
current = writer.toString();
} catch (IOException | PebbleException e) {
if (variableConfiguration.disableHandlebars) {
if (this.variableConfiguration.disableHandlebars) {
if (e instanceof PebbleException) {
throw properPebbleException((PebbleException) e);
}
Expand Down Expand Up @@ -186,6 +188,12 @@ public List<String> render(List<String> list, Map<String, Object> variables) thr
@Getter
@ConfigurationProperties("kestra.variables")
public static class VariableConfiguration {
public VariableConfiguration() {
this.disableHandlebars = true;
this.cacheEnabled = true;
this.cacheSize = 1000;
}

Boolean disableHandlebars;
Boolean cacheEnabled;
Integer cacheSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ public PebbleLruCache(int maximumSize) {
public PebbleTemplate computeIfAbsent(Object key, Function<? super Object, ? extends PebbleTemplate> mappingFunction) {
try {
return cache.get(key, () -> mappingFunction.apply(key));
} catch (ExecutionException e) {
log.warn("Error during cache compute", e);

} catch (Exception e) {
// we retry the mapping function in order to let the exception be thrown instead of being capture by cache
return mappingFunction.apply(key);
}
}
Expand Down

0 comments on commit 5675a3f

Please sign in to comment.