Skip to content

Commit

Permalink
NMS-16518: Fix scriptd's broken reloadDaemonConfig handling (#7427)
Browse files Browse the repository at this point in the history
* Make scriptd handle reloadDaemonConfig events correctly and fully reload config and scripts
  • Loading branch information
dino2gnt authored Sep 16, 2024
1 parent e765ae0 commit a57deaf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ public ScriptdRunnable(IEvent event) {
public void run() {

if (isReloadConfigEvent(m_event)) {
// reloads are handled by Scriptd#handleReloadConfigEvent
return;
doReload();
}

if (m_config.getTransactional()) {
Expand All @@ -201,6 +200,52 @@ protected void doReload() {
registerEventProcessor();
loadConfig();

// Run all stop scripts before terminating engines
LOG.debug("Running all stop scripts...");
for (final StopScript stopScript : m_config.getStopScripts()) {
if (stopScript.getContent().isPresent()) {
try {
m_scriptManager.exec(stopScript.getLanguage(), "", 0, 0, stopScript.getContent().get());
} catch (BSFException e) {
LOG.error("Stop script failed: {}", stopScript, e);
}
} else {
LOG.warn("Stop script has no script contents: {}", stopScript);
}
}

LOG.debug("Terminating existing engines");
m_scriptManager.terminate();

for (final Engine engine : m_config.getEngines()) {
LOG.debug("Re-Registering engine: {}", engine.getLanguage());
String[] extensions = null;
if (engine.getExtensions().isPresent()) {
StringTokenizer st = new StringTokenizer(engine.getExtensions().get());
extensions = new String[st.countTokens()];
int j = 0;
while (st.hasMoreTokens()) {
extensions[j++] = st.nextToken();
}
}
BSFManager.registerScriptingEngine(engine.getLanguage(), engine.getClassName(), extensions);
}

// Run all start scripts
LOG.debug("Running all start scripts...");
for (final StartScript startScript : m_config.getStartScripts()) {
if (startScript.getContent().isPresent()) {
try {
m_scriptManager.exec(startScript.getLanguage(), "", 0, 0, startScript.getContent().get());
} catch (BSFException e) {
LOG.error("Start script failed: {}", startScript, e);
}
} else {
LOG.warn("Start script has no script content: {}", startScript);
}
}
// run the explicit reload scripts since this is a reload
LOG.debug("Running all reload scripts...");
for (final ReloadScript script : m_config.getReloadScripts()) {
final var scriptContent = script.getContent();
if (scriptContent.isPresent()) {
Expand All @@ -213,8 +258,7 @@ protected void doReload() {
LOG.warn("Reload Script does not have script contents: {}", script);
}
}

LOG.debug("Scriptd configuration reloaded");
LOG.debug("Scriptd configuration reloaded!");
} catch (final Exception e) {
LOG.error("Unable to reload Scriptd configuration", e);
}
Expand Down Expand Up @@ -413,4 +457,5 @@ public synchronized void stop() {

LOG.debug("Scriptd executor stopped");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.opennms.netmgt.dao.api.NodeDao;
import org.opennms.netmgt.dao.api.SessionUtils;
import org.opennms.netmgt.events.api.EventConstants;
import org.opennms.netmgt.events.api.annotations.EventHandler;
import org.opennms.netmgt.events.api.annotations.EventListener;
import org.opennms.netmgt.events.api.model.IEvent;
import org.slf4j.Logger;
Expand Down Expand Up @@ -139,10 +138,4 @@ public static Scriptd getInstance() {
return m_singleton;
}

@EventHandler(uei = EventConstants.RELOAD_DAEMON_CONFIG_UEI)
public void handleReloadConfigEvent(final IEvent event) {
if (Executor.isReloadConfigEvent(event)) {
m_executor.doReload();
}
}
}

0 comments on commit a57deaf

Please sign in to comment.