From 9de08bb814b287d09b91320caa350aef116d611c Mon Sep 17 00:00:00 2001 From: Tobias Roeser Date: Wed, 16 Mar 2022 10:45:45 +0100 Subject: [PATCH 1/3] Let some workers implement AutoCloseable --- contrib/playlib/src/mill/playlib/RouteCompilerWorker.scala | 6 +++++- contrib/scalapblib/src/ScalaPBWorker.scala | 6 +++++- contrib/scoverage/src/ScoverageReportWorker.scala | 6 +++++- scalajslib/src/ScalaJSWorkerApi.scala | 5 ++++- scalalib/src/scalafmt/ScalafmtWorker.scala | 6 +++++- .../worker/src/mill/scalalib/worker/ZincWorkerImpl.scala | 6 +++++- scalanativelib/src/ScalaNativeWorkerApi.scala | 7 ++++++- 7 files changed, 35 insertions(+), 7 deletions(-) diff --git a/contrib/playlib/src/mill/playlib/RouteCompilerWorker.scala b/contrib/playlib/src/mill/playlib/RouteCompilerWorker.scala index 58ec70f3cdb..d0c695bff66 100644 --- a/contrib/playlib/src/mill/playlib/RouteCompilerWorker.scala +++ b/contrib/playlib/src/mill/playlib/RouteCompilerWorker.scala @@ -5,7 +5,7 @@ import mill.playlib.api.{RouteCompilerType, RouteCompilerWorkerApi} import mill.scalalib.api.CompilationResult import mill.{Agg, T} -private[playlib] class RouteCompilerWorker { +private[playlib] class RouteCompilerWorker extends AutoCloseable { private var routeCompilerInstanceCache = Option.empty[(Long, mill.playlib.api.RouteCompilerWorkerApi)] @@ -63,4 +63,8 @@ private[playlib] class RouteCompilerWorker { case err => Result.Failure(err) } } + + override def close(): Unit = { + routeCompilerInstanceCache = None + } } diff --git a/contrib/scalapblib/src/ScalaPBWorker.scala b/contrib/scalapblib/src/ScalaPBWorker.scala index 223f69bb4ce..3d0525705be 100644 --- a/contrib/scalapblib/src/ScalaPBWorker.scala +++ b/contrib/scalapblib/src/ScalaPBWorker.scala @@ -9,7 +9,7 @@ import mill.api.PathRef import mill.T import mill.define.{Discover, ExternalModule, Worker} -class ScalaPBWorker { +class ScalaPBWorker extends AutoCloseable { private var scalaPBInstanceCache = Option.empty[(Long, ScalaPBWorkerApi)] @@ -100,6 +100,10 @@ class ScalaPBWorker { mill.api.Result.Success(PathRef(dest)) } + + override def close(): Unit = { + scalaPBInstanceCache = None + } } trait ScalaPBWorkerApi { diff --git a/contrib/scoverage/src/ScoverageReportWorker.scala b/contrib/scoverage/src/ScoverageReportWorker.scala index 49b9aca2f18..a5236a3cfd8 100644 --- a/contrib/scoverage/src/ScoverageReportWorker.scala +++ b/contrib/scoverage/src/ScoverageReportWorker.scala @@ -5,7 +5,7 @@ import mill.api.{ClassLoader, Ctx} import mill.contrib.scoverage.api.ScoverageReportWorkerApi import mill.define.{Discover, ExternalModule, Worker} -class ScoverageReportWorker { +class ScoverageReportWorker extends AutoCloseable { private[this] var scoverageClCache = Option.empty[(Long, ClassLoader)] def bridge(classpath: Agg[os.Path])(implicit ctx: Ctx): ScoverageReportWorkerApi = { @@ -31,6 +31,10 @@ class ScoverageReportWorker { .newInstance() .asInstanceOf[api.ScoverageReportWorkerApi] } + + override def close(): Unit = { + scoverageClCache = None + } } object ScoverageReportWorker extends ExternalModule { diff --git a/scalajslib/src/ScalaJSWorkerApi.scala b/scalajslib/src/ScalaJSWorkerApi.scala index 3b1a4c51724..fd454f59c17 100644 --- a/scalajslib/src/ScalaJSWorkerApi.scala +++ b/scalajslib/src/ScalaJSWorkerApi.scala @@ -6,7 +6,7 @@ import mill.api.{Ctx, Result} import mill.define.Discover import mill.scalajslib.api._ import mill.{Agg, T} -class ScalaJSWorker { +class ScalaJSWorker extends AutoCloseable { private var scalaInstanceCache = Option.empty[(Long, ScalaJSWorkerApi)] private def bridge(toolsClasspath: Agg[os.Path])(implicit ctx: Ctx.Home) = { @@ -68,6 +68,9 @@ class ScalaJSWorker { bridge(toolsClasspath).getFramework(config, frameworkName, linkedFile, moduleKind) } + override def close(): Unit = { + scalaInstanceCache = None + } } object ScalaJSWorkerApi extends mill.define.ExternalModule { diff --git a/scalalib/src/scalafmt/ScalafmtWorker.scala b/scalalib/src/scalafmt/ScalafmtWorker.scala index 5fa9822c309..c1d61d8f839 100644 --- a/scalalib/src/scalafmt/ScalafmtWorker.scala +++ b/scalalib/src/scalafmt/ScalafmtWorker.scala @@ -14,7 +14,7 @@ object ScalafmtWorkerModule extends ExternalModule { lazy val millDiscover = Discover[this.type] } -private[scalafmt] class ScalafmtWorker { +private[scalafmt] class ScalafmtWorker extends AutoCloseable { private val reformatted: mutable.Map[os.Path, Int] = mutable.Map.empty private var configSig: Int = 0 @@ -94,4 +94,8 @@ private[scalafmt] class ScalafmtWorker { } } + override def close(): Unit = { + reformatted.clear() + configSig = 0 + } } diff --git a/scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala b/scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala index e0b331886e5..4aff9d9a04a 100644 --- a/scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala +++ b/scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala @@ -27,7 +27,7 @@ class ZincWorkerImpl( compilerJarNameGrep: (Agg[os.Path], String) => os.Path, compilerCache: KeyedLockedCache[Compilers], compileToJar: Boolean -) extends ZincWorkerApi { +) extends ZincWorkerApi with AutoCloseable { private val ic = new sbt.internal.inc.IncrementalCompilerImpl() lazy val javaOnlyCompilers = { // Keep the classpath as written by the user @@ -558,4 +558,8 @@ class ZincWorkerImpl( reporter.foreach(_.finish()) } } + + override def close(): Unit = { + classloaderCache.clear() + } } diff --git a/scalanativelib/src/ScalaNativeWorkerApi.scala b/scalanativelib/src/ScalaNativeWorkerApi.scala index 71a4fefd096..6cdb4813627 100644 --- a/scalanativelib/src/ScalaNativeWorkerApi.scala +++ b/scalanativelib/src/ScalaNativeWorkerApi.scala @@ -7,7 +7,7 @@ import mill.define.{Discover, Worker} import mill.{Agg, T} import mill.scalanativelib.api._ -class ScalaNativeWorker { +class ScalaNativeWorker extends AutoCloseable { private var scalaInstanceCache = Option.empty[(Long, ScalaNativeWorkerApi)] def impl(toolsClasspath: Agg[os.Path])(implicit ctx: mill.api.Ctx.Home): ScalaNativeWorkerApi = { @@ -37,6 +37,11 @@ class ScalaNativeWorker { } } } + + override def close(): Unit = { + // drop instance + scalaInstanceCache = None + } } object ScalaNativeWorkerApi extends mill.define.ExternalModule { From a5d1899cb4083e62da66e6e329713471db476438 Mon Sep 17 00:00:00 2001 From: Tobias Roeser Date: Wed, 16 Mar 2022 10:48:07 +0100 Subject: [PATCH 2/3] Let VizWorker extends AutoCloseable --- main/src/mill/main/MainModule.scala | 11 +++-------- main/src/mill/main/VisualizeModule.scala | 20 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/main/src/mill/main/MainModule.scala b/main/src/mill/main/MainModule.scala index 9aa12353ad8..b91f7e2c7fe 100644 --- a/main/src/mill/main/MainModule.scala +++ b/main/src/mill/main/MainModule.scala @@ -7,6 +7,7 @@ import mill.define.{Command, NamedTask, Task} import mill.eval.{Evaluator, EvaluatorPaths} import mill.util.{PrintLogger, Watched} import mill.define.SelectMode +import mill.main.VisualizeModule.VizWorker import pprint.{Renderer, Truncated} import ujson.Value @@ -380,11 +381,6 @@ trait MainModule extends mill.Module { System.exit(0) } - private type VizWorker = ( - LinkedBlockingQueue[(scala.Seq[_], scala.Seq[_], os.Path)], - LinkedBlockingQueue[Result[scala.Seq[PathRef]]] - ) - private def visualize0( evaluator: Evaluator, targets: Seq[String], @@ -396,9 +392,8 @@ trait MainModule extends mill.Module { rs: List[NamedTask[Any]], allRs: List[NamedTask[Any]] ): Result[Seq[PathRef]] = { - val (in, out) = vizWorker - in.put((rs, allRs, ctx.dest)) - out.take() + vizWorker.in.put((rs, allRs, ctx.dest)) + vizWorker.out.take() } RunScript.resolveTasks( diff --git a/main/src/mill/main/VisualizeModule.scala b/main/src/mill/main/VisualizeModule.scala index 8b5b1f0ceb6..dcfa9d18feb 100644 --- a/main/src/mill/main/VisualizeModule.scala +++ b/main/src/mill/main/VisualizeModule.scala @@ -1,15 +1,24 @@ package mill.main import java.util.concurrent.LinkedBlockingQueue - import coursier.LocalRepositories import coursier.core.Repository import coursier.maven.MavenRepository import mill.T import mill.define.{Discover, ExternalModule} import mill.api.{PathRef, Result} +import mill.main.VisualizeModule.VizWorker object VisualizeModule extends ExternalModule with VisualizeModule { + + private[main] class VizWorker( + val in: LinkedBlockingQueue[(scala.Seq[_], scala.Seq[_], os.Path)], + val out: LinkedBlockingQueue[Result[scala.Seq[PathRef]]], + closer: () => Unit = () => {} + ) extends AutoCloseable { + override def close(): Unit = closer() + } + def repositories = Seq( LocalRepositories.ivy2Local, MavenRepository("https://repo1.maven.org/maven2"), @@ -19,6 +28,7 @@ object VisualizeModule extends ExternalModule with VisualizeModule { implicit def millScoptEvaluatorReads[T] = new mill.main.EvaluatorScopt[T]() lazy val millDiscover = Discover[this.type] } + trait VisualizeModule extends mill.define.TaskModule { def repositories: Seq[Repository] def defaultCommandName() = "run" @@ -55,6 +65,12 @@ trait VisualizeModule extends mill.define.TaskModule { ) visualizeThread.setDaemon(true) visualizeThread.start() - (in, out) + new VizWorker( + in, + out, + () => { + visualizeThread.interrupt() + } + ) } } From 72810fb144accdac6bc74c7f9d892a545f7a0a78 Mon Sep 17 00:00:00 2001 From: Tobias Roeser Date: Wed, 16 Mar 2022 23:17:16 +0100 Subject: [PATCH 3/3] Revert "Let VizWorker extends AutoCloseable" This reverts commit a5d1899cb4083e62da66e6e329713471db476438. --- main/src/mill/main/MainModule.scala | 11 ++++++++--- main/src/mill/main/VisualizeModule.scala | 20 ++------------------ 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/main/src/mill/main/MainModule.scala b/main/src/mill/main/MainModule.scala index b91f7e2c7fe..9aa12353ad8 100644 --- a/main/src/mill/main/MainModule.scala +++ b/main/src/mill/main/MainModule.scala @@ -7,7 +7,6 @@ import mill.define.{Command, NamedTask, Task} import mill.eval.{Evaluator, EvaluatorPaths} import mill.util.{PrintLogger, Watched} import mill.define.SelectMode -import mill.main.VisualizeModule.VizWorker import pprint.{Renderer, Truncated} import ujson.Value @@ -381,6 +380,11 @@ trait MainModule extends mill.Module { System.exit(0) } + private type VizWorker = ( + LinkedBlockingQueue[(scala.Seq[_], scala.Seq[_], os.Path)], + LinkedBlockingQueue[Result[scala.Seq[PathRef]]] + ) + private def visualize0( evaluator: Evaluator, targets: Seq[String], @@ -392,8 +396,9 @@ trait MainModule extends mill.Module { rs: List[NamedTask[Any]], allRs: List[NamedTask[Any]] ): Result[Seq[PathRef]] = { - vizWorker.in.put((rs, allRs, ctx.dest)) - vizWorker.out.take() + val (in, out) = vizWorker + in.put((rs, allRs, ctx.dest)) + out.take() } RunScript.resolveTasks( diff --git a/main/src/mill/main/VisualizeModule.scala b/main/src/mill/main/VisualizeModule.scala index dcfa9d18feb..8b5b1f0ceb6 100644 --- a/main/src/mill/main/VisualizeModule.scala +++ b/main/src/mill/main/VisualizeModule.scala @@ -1,24 +1,15 @@ package mill.main import java.util.concurrent.LinkedBlockingQueue + import coursier.LocalRepositories import coursier.core.Repository import coursier.maven.MavenRepository import mill.T import mill.define.{Discover, ExternalModule} import mill.api.{PathRef, Result} -import mill.main.VisualizeModule.VizWorker object VisualizeModule extends ExternalModule with VisualizeModule { - - private[main] class VizWorker( - val in: LinkedBlockingQueue[(scala.Seq[_], scala.Seq[_], os.Path)], - val out: LinkedBlockingQueue[Result[scala.Seq[PathRef]]], - closer: () => Unit = () => {} - ) extends AutoCloseable { - override def close(): Unit = closer() - } - def repositories = Seq( LocalRepositories.ivy2Local, MavenRepository("https://repo1.maven.org/maven2"), @@ -28,7 +19,6 @@ object VisualizeModule extends ExternalModule with VisualizeModule { implicit def millScoptEvaluatorReads[T] = new mill.main.EvaluatorScopt[T]() lazy val millDiscover = Discover[this.type] } - trait VisualizeModule extends mill.define.TaskModule { def repositories: Seq[Repository] def defaultCommandName() = "run" @@ -65,12 +55,6 @@ trait VisualizeModule extends mill.define.TaskModule { ) visualizeThread.setDaemon(true) visualizeThread.start() - new VizWorker( - in, - out, - () => { - visualizeThread.interrupt() - } - ) + (in, out) } }