sergehuber commented on code in PR #720: URL: https://github.com/apache/unomi/pull/720#discussion_r2216103513
########## extensions/groovy-actions/services/src/main/java/org/apache/unomi/groovy/actions/services/impl/GroovyActionsServiceImpl.java: ########## @@ -209,48 +328,182 @@ private void saveActionType(Action action) { definitionsService.setActionType(actionType); } + /** + * {@inheritDoc} + */ @Override public void remove(String id) { - if (groovyCodeSourceMap.containsKey(id)) { - try { - definitionsService.removeActionType( - groovyShell.parse(groovyCodeSourceMap.get(id)).getClass().getMethod("execute").getAnnotation(Action.class).id()); - } catch (NoSuchMethodException e) { - LOGGER.error("Failed to delete the action type for the id {}", id, e); + validateNotEmpty(id, "Script ID"); + + LOGGER.info("Removing script: {}", id); + + ScriptMetadata removedMetadata = scriptMetadataCache.remove(id); + persistenceService.remove(id, GroovyAction.class); + + if (removedMetadata != null) { + Action actionAnnotation = getActionAnnotation(removedMetadata.getCompiledClass()); + if (actionAnnotation != null) { + definitionsService.removeActionType(actionAnnotation.id()); } - persistenceService.remove(id, GroovyAction.class); } + + LOGGER.info("Script {} removed successfully", id); } + + /** + * {@inheritDoc} + * Performance Warning: Compiles on-demand if not cached. + */ @Override - public GroovyCodeSource getGroovyCodeSource(String id) { - return groovyCodeSourceMap.get(id); + public Class<? extends Script> getOrCompileScript(String id) { + validateNotEmpty(id, "Script ID"); + + ScriptMetadata metadata = scriptMetadataCache.get(id); + if (metadata != null) { + return metadata.getCompiledClass(); + } + + long startTime = System.currentTimeMillis(); + LOGGER.warn("Script {} not found in cache, compiling on-demand (performance warning)", id); + + GroovyAction groovyAction = persistenceService.load(id, GroovyAction.class); + if (groovyAction == null) { + LOGGER.warn("Script {} not found in persistence, returning null ({}ms)", id, + System.currentTimeMillis() - startTime); + return null; + } + + try { + long compilationStartTime = System.currentTimeMillis(); + ScriptMetadata newMetadata = compileAndCreateMetadata(id, groovyAction.getScript()); + long compilationTime = System.currentTimeMillis() - compilationStartTime; + + scriptMetadataCache.put(id, newMetadata); + + long totalTime = System.currentTimeMillis() - startTime; + LOGGER.warn("On-demand compilation completed for {} (total: {}ms, compilation: {}ms)", + id, totalTime, compilationTime); + return newMetadata.getCompiledClass(); + } catch (Exception e) { + long totalTime = System.currentTimeMillis() - startTime; + LOGGER.error("Failed to compile script {} on-demand ({}ms)", id, totalTime, e); + return null; + } } /** - * Build a GroovyCodeSource object and add it to the class loader of the groovyScriptEngine - * - * @param groovyScript groovy script as a string - * @param actionName Name of the action - * @return Built GroovyCodeSource + * {@inheritDoc} + */ + @Override + public Class<? extends Script> getCompiledScript(String id) { + validateNotEmpty(id, "Script ID"); + + ScriptMetadata metadata = scriptMetadataCache.get(id); Review Comment: I agree we should make this the same everywhere. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@unomi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org