Skip to content

Ability to execute and queue scripts from another script #304

@jakub-przybytek-wttech

Description

@jakub-przybytek-wttech

Because we had a rather big script that builds our permissions configuration on the AEM repository, we decided to split it into few smaller ones that are easer to maintain. But because ACM doesn't allow to run scripts from other scripts (which we would utilize by creating yet another script that runs those small ones in a sequence) they have to be run manually and in the right order.
To work this around we managed to actually write such script because all we needed is already exposed as OSGi services, see:

import dev.vml.es.acm.core.script.ScriptRepository
import dev.vml.es.acm.core.code.*
import dev.vml.es.acm.core.util.ResolverUtils

boolean canRun() {
    return conditions.always()
}

void doRun() {
    ScriptRepository scriptRepository = new ScriptRepository(resourceResolver)
    ExecutionQueue executionQueue = osgi.getService(ExecutionQueue.class)
    runScript(scriptRepository, executionQueue, "tools/clearAll.groovy")

    runScript(scriptRepository, executionQueue, "first-script.groovy")
    runScript(scriptRepository, executionQueue, "second-script.groovy")
    runScript(scriptRepository, executionQueue, "third-script.groovy")
}

void runScript(ScriptRepository scriptRepository, ExecutionQueue executionQueue, String name) {
    def path = "/conf/acm/settings/script/manual/permissions/${name}"
    def script = scriptRepository.read(path)
    if (script.isEmpty()) {
        log.error "Script not found: $path"
        return
    }
    def options = new ExecutionContextOptions(ExecutionMode.RUN, ResolverUtils.Subservice.CONTENT.userId, new InputValues())
    executionQueue.submit(script.get(), options)
    log.info "Script scheduled to run: $path"
}

Would it be possible to incorporate ability to execute and queue scripts in official ACM API?
It could look something like that:

doRun {
   context.execute("sibling-script.groovy")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions