Skip to content

Commit 9ecfd0a

Browse files
Make runBackground use process IDs (0.12.x branch) (#5120)
Backport of #5115 --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 5dd2bd3 commit 9ecfd0a

File tree

9 files changed

+268
-75
lines changed

9 files changed

+268
-75
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ end_of_line = lf
55
insert_final_newline = true
66
charset = utf-8
77
indent_style = space
8-
indent_size = 4
8+
indent_size = 2
99

1010
[*.scala]
1111
end_of_line = lf

build.mill

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ trait MillPublishJavaModule extends MillJavaModule with PublishModule {
431431
"info.releaseNotesURL" -> Settings.changelogUrl
432432
)
433433
def pomSettings = MillPublishJavaModule.commonPomSettings(artifactName())
434-
def javacOptions = Seq("-source", "1.8", "-target", "1.8", "-encoding", "UTF-8")
434+
def javacOptions = Seq("-source", "11", "-target", "11", "-encoding", "UTF-8")
435435
}
436436

437437
object MillPublishJavaModule {
@@ -448,7 +448,6 @@ object MillPublishJavaModule {
448448
)
449449
)
450450
}
451-
452451
}
453452

454453
/**

docs/modules/ROOT/pages/cli/flags.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ forcefully terminating the previous process even though it may be still alive:
131131
$ mill -w foo.runBackground
132132
----
133133

134+
Note that even if you interrupt mill watch via CTRL+C, the server spawned by `runBackground` still runs in the
135+
background. To actually stop the background server use:
136+
137+
[source,console]
138+
----
139+
> mill clean foo.runBackground
140+
----
134141

135142
=== `--jobs`/`-j`
136143

integration/invalidation/run-background/src/RunBackgroundTests.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ object RunBackgroundTests extends UtestIntegrationTestSuite {
3737
os.write(stop, "")
3838
eventually { probeLockAvailable(lock) }
3939
}
40+
41+
test("sequential") - integrationTest { tester =>
42+
import tester._
43+
val lock1 = os.temp()
44+
val lock2 = os.temp()
45+
val stop = os.temp()
46+
os.remove(stop)
47+
eval(("foo.runBackground", lock1, stop))
48+
eventually { !probeLockAvailable(lock1) }
49+
eval(("foo.runBackground", lock2, stop))
50+
eventually { !probeLockAvailable(lock2) }
51+
Predef.assert(
52+
probeLockAvailable(lock1),
53+
"first process should be exited after second process is running"
54+
)
55+
56+
if (tester.clientServerMode) eval("shutdown")
57+
continually { !probeLockAvailable(lock2) }
58+
os.write(stop, "")
59+
eventually { probeLockAvailable(lock2) }
60+
}
61+
4062
test("clean") - integrationTest { tester =>
4163
import tester._
4264
val lock = os.temp()
@@ -45,6 +67,7 @@ object RunBackgroundTests extends UtestIntegrationTestSuite {
4567
eval(("foo.runBackground", lock, stop))
4668
eventually {
4769
!probeLockAvailable(lock)
70+
4871
}
4972

5073
eval(("clean", "foo.runBackground"))

main/define/src/mill/define/Task.scala

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,33 @@ object Task extends TaskBase {
122122
cls: EnclosingClass
123123
): Command[T] = macro Target.Internal.commandImpl[T]
124124

125+
/** Binary compatibility forwarder. */
126+
def Command(
127+
t: NamedParameterOnlyDummy,
128+
exclusive: Boolean
129+
): CommandFactory = new CommandFactory(exclusive = exclusive, persistent = false)
130+
125131
/**
126132
* @param exclusive Exclusive commands run serially at the end of an evaluation,
127133
* without any other tasks running parallel, and without the
128134
* terminal logging prefixes that are applied to normal tasks.
129135
* These are normally used for "top level" commands which are
130136
* run directly to perform some action or display some output
131137
* to the user.
138+
* @param persistent Persistent comands do not erase the `Task.dest` folder
139+
* between runs.
132140
*/
133141
def Command(
134142
t: NamedParameterOnlyDummy = new NamedParameterOnlyDummy,
135-
exclusive: Boolean = false
136-
): CommandFactory = new CommandFactory(exclusive)
137-
class CommandFactory private[mill] (val exclusive: Boolean) extends TaskBase.TraverseCtxHolder {
143+
exclusive: Boolean = false,
144+
persistent: Boolean = false
145+
): CommandFactory = new CommandFactory(exclusive = exclusive, persistent = persistent)
146+
class CommandFactory private[mill] (val exclusive: Boolean, val persistent: Boolean)
147+
extends TaskBase.TraverseCtxHolder {
148+
149+
/** Binary compatibility forwarder */
150+
private[mill] def this(exclusive: Boolean) = this(exclusive, persistent = false)
151+
138152
def apply[T](t: Result[T])(implicit
139153
w: W[T],
140154
ctx: mill.define.Ctx,
@@ -662,7 +676,8 @@ object Target extends TaskBase {
662676
w.splice,
663677
cls.splice.value,
664678
taskIsPrivate.splice,
665-
exclusive = c.prefix.splice.asInstanceOf[Task.CommandFactory].exclusive
679+
exclusive = c.prefix.splice.asInstanceOf[Task.CommandFactory].exclusive,
680+
persistent = c.prefix.splice.asInstanceOf[Task.CommandFactory].persistent
666681
)
667682
)
668683
}
@@ -871,15 +886,30 @@ class Command[+T](
871886
val writer: W[_],
872887
val cls: Class[_],
873888
val isPrivate: Option[Boolean],
874-
val exclusive: Boolean
889+
val exclusive: Boolean,
890+
val persistent: Boolean
875891
) extends NamedTask[T] {
892+
override def flushDest: Boolean = !persistent
893+
894+
/** Binary compatibility forwarder. */
876895
def this(
877896
t: Task[T],
878897
ctx0: mill.define.Ctx,
879898
writer: W[_],
880899
cls: Class[_],
881900
isPrivate: Option[Boolean]
882-
) = this(t, ctx0, writer, cls, isPrivate, false)
901+
) = this(t, ctx0, writer, cls, isPrivate, exclusive = false, persistent = false)
902+
903+
/** Binary compatibility forwarder. */
904+
def this(
905+
t: Task[T],
906+
ctx0: mill.define.Ctx,
907+
writer: W[_],
908+
cls: Class[_],
909+
isPrivate: Option[Boolean],
910+
exclusive: Boolean
911+
) = this(t, ctx0, writer, cls, isPrivate, exclusive = exclusive, persistent = false)
912+
883913
override def asCommand: Some[Command[T]] = Some(this)
884914
// FIXME: deprecated return type: Change to Option
885915
override def writerOpt: Some[W[_]] = Some(writer)

pythonlib/src/mill/pythonlib/PythonModule.scala

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ trait PythonModule extends PipModule with TaskModule { outer =>
175175
*
176176
* @see [[mainScript]]
177177
*/
178-
def runBackground(args: mill.define.Args) = Task.Command {
179-
val (procUuidPath, procLockfile, procUuid) = mill.scalalib.RunModule.backgroundSetup(Task.dest)
178+
def runBackground(args: mill.define.Args) = Task.Command(persistent = true) {
179+
val backgroundPaths = new mill.scalalib.RunModule.BackgroundPaths(destDir = Task.dest)
180180
val pwd0 = os.Path(java.nio.file.Paths.get(".").toAbsolutePath)
181181

182182
os.checker.withValue(os.Checker.Nop) {
@@ -185,11 +185,7 @@ trait PythonModule extends PipModule with TaskModule { outer =>
185185
classPath = mill.scalalib.JvmWorkerModule.backgroundWrapperClasspath().map(_.path).toSeq,
186186
jvmArgs = Nil,
187187
env = runnerEnvTask(),
188-
mainArgs = Seq(
189-
procUuidPath.toString,
190-
procLockfile.toString,
191-
procUuid,
192-
"500",
188+
mainArgs = backgroundPaths.toArgs ++ Seq(
193189
"<subprocess>",
194190
pythonExe().path.toString,
195191
mainScript().path.toString

0 commit comments

Comments
 (0)