Move suspendChannel to EvaluateTask

This commit is contained in:
Eugene Yokota 2024-10-01 03:15:28 -04:00
parent 683b09afa8
commit 8aa740574b
5 changed files with 37 additions and 23 deletions

View File

@ -111,4 +111,9 @@ abstract class JobHandle {
def spawningTask: ScopedKey[_] def spawningTask: ScopedKey[_]
} }
case class RunInfo(handle: JobHandle) /**
* This datatype is used signal the task engine or the commands
* that the background job is emulated to be a foreground job on
* the originating channel.
*/
case class EmulateForeground(handle: JobHandle)

View File

@ -2152,27 +2152,17 @@ object Defaults extends BuildCommon {
} }
// `runMain` calls bgRunMain in the background and pauses the current channel // `runMain` calls bgRunMain in the background and pauses the current channel
def foregroundRunMainTask: Initialize[InputTask[RunInfo]] = def foregroundRunMainTask: Initialize[InputTask[EmulateForeground]] =
Def.inputTask { Def.inputTask {
val handle = bgRunMain.evaluated val handle = bgRunMain.evaluated
val service = bgJobService.value EmulateForeground(handle)
val st = state.value
st.remainingCommands match
case Nil => service.waitForTry(handle).get
case _ => service.pauseChannelDuringJob(st, handle)
RunInfo(handle)
} }
// `run` task calls bgRun in the background and pauses the current channel // `run` task calls bgRun in the background and pauses the current channel
def foregroundRunTask: Initialize[InputTask[RunInfo]] = def foregroundRunTask: Initialize[InputTask[EmulateForeground]] =
Def.inputTask { Def.inputTask {
val handle = bgRun.evaluated val handle = bgRun.evaluated
val service = bgJobService.value EmulateForeground(handle)
val st = state.value
st.remainingCommands match
case Nil => service.waitForTry(handle).get
case _ => service.pauseChannelDuringJob(st, handle)
RunInfo(handle)
} }
def runMainTask( def runMainTask(

View File

@ -512,6 +512,16 @@ object EvaluateTask {
case Some(t: Task[?]) => transformNode(t).isEmpty case Some(t: Task[?]) => transformNode(t).isEmpty
case _ => true case _ => true
} }
def suspendChannel[A1](
state: State,
result: Result[A1]
): Unit =
(state.getSetting(Global / Keys.bgJobService), result) match
case (Some(service), Result.Value(List(KeyValue(_, EmulateForeground(handle))))) =>
state.remainingCommands match
case Nil => service.waitForTry(handle).get
case _ => service.pauseChannelDuringJob(state, handle)
case _ => ()
def run() = { def run() = {
val x = new Execute( val x = new Execute(
Execute.config(config.checkCycles, overwriteNode), Execute.config(config.checkCycles, overwriteNode),
@ -529,6 +539,7 @@ object EvaluateTask {
} finally shutdown() } finally shutdown()
val replaced = transformInc(result) val replaced = transformInc(result)
logIncResult(replaced, state, streams) logIncResult(replaced, state, streams)
suspendChannel(newState, replaced)
(newState, replaced) (newState, replaced)
} }
object runningEngine extends RunningTaskEngine { object runningEngine extends RunningTaskEngine {

View File

@ -319,9 +319,9 @@ object Keys {
// Run Keys // Run Keys
val selectMainClass = taskKey[Option[String]]("Selects the main class to run.").withRank(BMinusTask) val selectMainClass = taskKey[Option[String]]("Selects the main class to run.").withRank(BMinusTask)
val mainClass = taskKey[Option[String]]("Defines the main class for packaging or running.").withRank(BPlusTask) val mainClass = taskKey[Option[String]]("Defines the main class for packaging or running.").withRank(BPlusTask)
val run = inputKey[RunInfo]("Runs a main class, passing along arguments provided on the command line.").withRank(APlusTask) val run = inputKey[EmulateForeground]("Runs a main class, passing along arguments provided on the command line.").withRank(APlusTask)
val runBlock = inputKey[RunInfo]("Runs a main class, and blocks until it's done.").withRank(DTask) val runBlock = inputKey[EmulateForeground]("Runs a main class, and blocks until it's done.").withRank(DTask)
val runMain = inputKey[RunInfo]("Runs the main class selected by the first argument, passing the remaining arguments to the main method.").withRank(ATask) val runMain = inputKey[EmulateForeground]("Runs the main class selected by the first argument, passing the remaining arguments to the main method.").withRank(ATask)
val discoveredMainClasses = taskKey[Seq[String]]("Auto-detects main classes.").withRank(BMinusTask) val discoveredMainClasses = taskKey[Seq[String]]("Auto-detects main classes.").withRank(BMinusTask)
val runner = taskKey[ScalaRun]("Implementation used to run a main class.").withRank(DTask) val runner = taskKey[ScalaRun]("Implementation used to run a main class.").withRank(DTask)
val trapExit = settingKey[Boolean]("If true, enables exit trapping and thread management for 'run'-like tasks. This was removed in sbt 1.6.0 due to JDK 17 deprecating Security Manager.").withRank(CSetting) val trapExit = settingKey[Boolean]("If true, enables exit trapping and thread management for 'run'-like tasks. This was removed in sbt 1.6.0 due to JDK 17 deprecating Security Manager.").withRank(CSetting)

View File

@ -79,17 +79,25 @@ object Aggregation {
val success = results match val success = results match
case Result.Value(_) => true case Result.Value(_) => true
case Result.Inc(_) => false case Result.Inc(_) => false
// run task ends earlier than the program run val isPaused = currentChannel(state) match
val isRunInfo = results match case Some(channel) => channel.isPaused
case Result.Value(Seq(KeyValue(_, RunInfo(_)))) => true case None => false
case _ => false
results.toEither.foreach { r => results.toEither.foreach { r =>
if show.taskValues then printSettings(r, show.print) else () if show.taskValues then printSettings(r, show.print) else ()
} }
if show.success && !isRunInfo && !state.get(suppressShow).getOrElse(false) then if !isPaused && show.success && !state.get(suppressShow).getOrElse(false) then
printSuccess(start, stop, extracted, success, cacheSummary, log) printSuccess(start, stop, extracted, success, cacheSummary, log)
else () else ()
private def currentChannel(state: State): Option[CommandChannel] =
state.currentCommand match
case Some(exec) =>
exec.source match
case Some(source) =>
StandardMain.exchange.channels.find(_.name == source.channelName)
case _ => None
case _ => None
def timedRun[A]( def timedRun[A](
s: State, s: State,
ts: Values[Task[A]], ts: Values[Task[A]],