Search in sources :

Example 11 with StoredScript

use of net.robinfriedli.aiode.entities.StoredScript in project aiode by robinfriedli.

the class SafeGroovyScriptRunner method runScripts.

/**
 * Run all provided scripts sequentially in the same thread. This counts as one single script execution, thus all scripts
 * share the same method invocation limits and run with one time limit. This method is mainly used by the ScriptCommandInterceptor
 * to run all interceptors or all finalizers. Just like {@link #evaluateScript(String, long, TimeUnit)} this applies
 * all security checks and expression transformation if isPrivileged is false.
 *
 * @param scripts       a list of {@link StoredScript} entities representing the scripts to run
 * @param currentScript a reference pointing to the script that is currently being executed, can be used to reference
 *                      the last executed script if execution fails or execution runs into a timeout
 * @param timeout       the time delta in which all provided scripts have to run, ignored if isPrivileged is true
 * @param timeUnit      the time unit of the timeout
 * @throws ExecutionException if a script fails due to an exception
 * @throws TimeoutException   if not all scripts finish within the given time limit
 */
public void runScripts(List<StoredScript> scripts, AtomicReference<StoredScript> currentScript, long timeout, TimeUnit timeUnit) throws ExecutionException, TimeoutException {
    GroovyShell groovyShell = createShell();
    Future<Object> result = scriptExecution(() -> {
        for (StoredScript script : scripts) {
            currentScript.set(script);
            groovyShell.evaluate(script.getScript());
        }
        return null;
    });
    runScriptWithTimeout(result, timeout, timeUnit);
}
Also used : StoredScript(net.robinfriedli.aiode.entities.StoredScript) GroovyShell(groovy.lang.GroovyShell)

Aggregations

StoredScript (net.robinfriedli.aiode.entities.StoredScript)11 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)6 Aiode (net.robinfriedli.aiode.Aiode)4 Session (org.hibernate.Session)4 GroovyShell (groovy.lang.GroovyShell)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 CommandContext (net.robinfriedli.aiode.command.CommandContext)3 QueryBuilderFactory (net.robinfriedli.aiode.persist.qb.QueryBuilderFactory)3 GroovyVariableManager (net.robinfriedli.aiode.scripting.GroovyVariableManager)3 List (java.util.List)2 Optional (java.util.Optional)2 AbstractCommand (net.robinfriedli.aiode.command.AbstractCommand)2 CommandContribution (net.robinfriedli.aiode.entities.xml.CommandContribution)2 MultipleCompilationErrorsException (org.codehaus.groovy.control.MultipleCompilationErrorsException)2 Lists (com.google.common.collect.Lists)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1