Merge pull request #7916 from Friendseeker/ctrl-c-background-task

This commit is contained in:
eugene yokota 2024-11-27 20:23:28 -05:00 committed by GitHub
commit f03ff1f32b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 25 additions and 1 deletions

View File

@ -1071,6 +1071,7 @@ lazy val mainProj = (project in file("main"))
exclude[MissingClassProblem]("sbt.internal.server.BuildServerReporter$"),
exclude[IncompatibleTemplateDefProblem]("sbt.internal.server.BuildServerReporter"),
exclude[MissingClassProblem]("sbt.internal.CustomHttp*"),
exclude[ReversedMissingMethodProblem]("sbt.JobHandle.isAutoCancel"),
)
)
.configure(

View File

@ -100,4 +100,5 @@ abstract class JobHandle {
def id: Long
def humanReadableName: String
def spawningTask: ScopedKey[_]
def isAutoCancel: Boolean
}

View File

@ -2039,6 +2039,11 @@ object Defaults extends BuildCommon {
def foregroundRunMainTask: Initialize[InputTask[Unit]] =
Def.inputTask {
val handle = bgRunMain.evaluated
handle match {
case threadJobHandle: AbstractBackgroundJobService#ThreadJobHandle =>
threadJobHandle.isAutoCancel = true
case _ =>
}
val service = bgJobService.value
service.waitForTry(handle).get
}
@ -2047,6 +2052,11 @@ object Defaults extends BuildCommon {
def foregroundRunTask: Initialize[InputTask[Unit]] =
Def.inputTask {
val handle = bgRun.evaluated
handle match {
case threadJobHandle: AbstractBackgroundJobService#ThreadJobHandle =>
threadJobHandle.isAutoCancel = true
case _ =>
}
val service = bgJobService.value
service.waitForTry(handle).get
}

View File

@ -531,6 +531,7 @@ object EvaluateTask {
log.warn("Canceling execution...")
RunningProcesses.killAll()
ConcurrentRestrictions.cancelAll()
DefaultBackgroundJobService.stop()
shutdownImpl(true)
}
}

View File

@ -114,7 +114,8 @@ private[sbt] abstract class AbstractBackgroundJobService extends BackgroundJobSe
override val spawningTask: ScopedKey[_],
val logger: ManagedLogger,
val workingDirectory: File,
val job: BackgroundJob
val job: BackgroundJob,
@volatile var isAutoCancel: Boolean = false,
) extends AbstractJobHandle {
// EC for onStop handler below
implicit val executionContext: ExecutionContext =
@ -140,6 +141,7 @@ private[sbt] abstract class AbstractBackgroundJobService extends BackgroundJobSe
private final class DeadHandle(override val id: Long, override val humanReadableName: String)
extends AbstractJobHandle {
override val spawningTask: ScopedKey[_] = unknownTask
override val isAutoCancel = false
}
def doRunInBackground(
@ -508,6 +510,15 @@ private[sbt] object DefaultBackgroundJobService {
backgroundJobServices.values.forEach(_.shutdown())
backgroundJobServices.clear()
}
private[sbt] def stop(): Unit = {
backgroundJobServices
.values()
.forEach(jobService => {
jobService.jobs.filter(_.isAutoCancel).foreach(jobService.stop)
})
}
private[sbt] lazy val backgroundJobServiceSetting: Setting[_] =
(GlobalScope / Keys.bgJobService) := {
val path = (GlobalScope / sbt.Keys.bgJobServiceDirectory).value