Skip to content

Commit

Permalink
Make the stored procedure check interval configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
gmokki committed Jun 7, 2022
1 parent 066ca5b commit 7d9a191
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import static java.lang.Long.MAX_VALUE;
import static java.lang.System.currentTimeMillis;
import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.synchronizedMap;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.ArrayList;
Expand Down Expand Up @@ -35,21 +36,23 @@ public class WorkflowDefinitionService {
private static final Logger logger = getLogger(WorkflowDefinitionService.class);

private final Map<String, WorkflowDefinition> workflowDefinitions = synchronizedMap(new LinkedHashMap<>());
private List<WorkflowDefinition> workflowDefinitionValues = emptyList();
private List<WorkflowDefinition> workflowDefinitionValues = unmodifiableList(emptyList());
private final WorkflowDefinitionDao workflowDefinitionDao;
private final boolean persistWorkflowDefinitions;
private final boolean autoInit;
/**
* The next time the stored definitions from database are checked for changes.
*/
private long nextCheckOfStoredDefinitions;
private long storedDefinitionCheckInterval;

@Inject
public WorkflowDefinitionService(WorkflowDefinitionDao workflowDefinitionDao, Environment env) {
this.workflowDefinitionDao = workflowDefinitionDao;
this.persistWorkflowDefinitions = env.getRequiredProperty("nflow.definition.persist", Boolean.class);
this.autoInit = env.getRequiredProperty("nflow.autoinit", Boolean.class);
this.nextCheckOfStoredDefinitions = env.getProperty("nflow.definition.load", Boolean.class, true) ? 0 : MAX_VALUE;
this.storedDefinitionCheckInterval = SECONDS.toMillis(env.getProperty("nflow.definition.load.interval", Integer.class, 60));
this.nextCheckOfStoredDefinitions = storedDefinitionCheckInterval > 0 ? 0 : MAX_VALUE;
}

/**
Expand Down Expand Up @@ -109,7 +112,7 @@ public void addWorkflowDefinition(WorkflowDefinition wd) {
workflowDefinitionDao.storeWorkflowDefinition(wd);
}
synchronized (workflowDefinitions) {
workflowDefinitionValues = new ArrayList<>(workflowDefinitions.values());
workflowDefinitionValues = unmodifiableList(new ArrayList<>(workflowDefinitions.values()));
}
logger.info("Added workflow type: {} ({})", wd.getType(), wd.getClass().getName());
}
Expand All @@ -119,7 +122,7 @@ private synchronized void refreshStoredDefinitions() {
if (nextCheckOfStoredDefinitions > now) {
return;
}
nextCheckOfStoredDefinitions = now + MINUTES.toMillis(1);
nextCheckOfStoredDefinitions = now + storedDefinitionCheckInterval;
boolean changed = false;
for (StoredWorkflowDefinition def : workflowDefinitionDao.queryStoredWorkflowDefinitions(emptyList())) {
WorkflowDefinition current = workflowDefinitions.get(def.type);
Expand All @@ -131,7 +134,7 @@ private synchronized void refreshStoredDefinitions() {
}
if (changed) {
synchronized (workflowDefinitions) {
workflowDefinitionValues = new ArrayList<>(workflowDefinitions.values());
workflowDefinitionValues = unmodifiableList(new ArrayList<>(workflowDefinitions.values()));
}
}
}
Expand Down

0 comments on commit 7d9a191

Please sign in to comment.