Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stopAfter task cannot be set from inside task #53

Open
rschuetz opened this issue Apr 5, 2022 · 0 comments
Open

stopAfter task cannot be set from inside task #53

rschuetz opened this issue Apr 5, 2022 · 0 comments

Comments

@rschuetz
Copy link

rschuetz commented Apr 5, 2022

According to the documentation, the "stopAfter" task can be set from within the (Java)ExecFork task, i.e. the following should work (the "run" task simulates a task for example for integration tests that needs a server running) IMHO, right? (KTS)

plugins {
    id("com.github.psxpaul.execfork") version "0.2.0"
}

defaultTasks("run")

tasks {
    val triggerShutdown = register("triggerShutdown") {
    }

    register<com.github.psxpaul.task.ExecFork>("startServer") {
        executable = "sleep"
        args = mutableListOf("60")
        stopAfter = triggerShutdown
    }

    register("run") {
        dependsOn("startServer")
        finalizedBy("triggerShutdown", "sleep")
        doFirst {
             Thread.sleep(30 * 1000)
        }
    }

    register("sleep") {
        doFirst {
             Thread.sleep(30 * 1000)
        }
    }
}

However, it fails (tested with Gradle 7.1.1 and 7.4.2) with

* Exception is:
org.gradle.api.internal.tasks.DefaultTaskContainer$TaskCreationException: Could not create task ':startServer'.
 [...]
Caused by: org.gradle.api.internal.AbstractMutationGuard$IllegalMutationException: DefaultTaskContainer#register(String, Class, Action) on task set cannot be executed in the current context.
	at org.gradle.api.internal.AbstractMutationGuard.createIllegalStateException(AbstractMutationGuard.java:39)
	at org.gradle.api.internal.AbstractMutationGuard.assertMutationAllowed(AbstractMutationGuard.java:27)
	at org.gradle.api.internal.DefaultDomainObjectCollection.assertMutable(DefaultDomainObjectCollection.java:458)
	at org.gradle.api.internal.tasks.DefaultTaskContainer.register(DefaultTaskContainer.java:374)
	at com.github.psxpaul.task.AbstractExecFork.setStopAfter(AbstractExecFork.kt:98)
	at Build_gradle$1$1.invoke(build.gradle.kts:14)
	at Build_gradle$1$1.invoke(build.gradle.kts:1)
	at Build_gradle$inlined$sam$i$org_gradle_api_Action$0.execute(TaskContainerExtensions.kt)
 [...]

when the stop task gets registered in AbstractExecFork.kt. The same happens if

 stopAfter = triggerShutdown 

is being replaced with

 stopAfter = project.tasks.named("triggerShutdown")

The only workaround I found so far was linking both tasks later on like this:

plugins {
    id("com.github.psxpaul.execfork") version "0.2.0"
}

defaultTasks("run")

tasks {
    val triggerShutdown = register("triggerShutdown") {
    }

    val startServer = register<com.github.psxpaul.task.ExecFork>("startServer") {
        executable = "sleep"
        args = mutableListOf("60")
    }

    startServer.get().stopAfter = triggerShutdown

    register("run") {
        dependsOn("startServer")
        finalizedBy("triggerShutdown", "sleep")
        doFirst {
             Thread.sleep(30 * 1000)
        }
    }

    register("sleep") {
        doFirst {
             Thread.sleep(30 * 1000)
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant