-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathunitd.mill.scala
60 lines (57 loc) · 1.53 KB
/
unitd.mill.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package build
import java.util.concurrent.atomic.AtomicBoolean
private val dest = os.home / ".cache" / "snunit"
val control = dest / "control.sock"
private val pid = dest / "unit.pid"
// Variables are not maintained among invocations in interactive (`-i`) mode.
// Running Mill with `-i` in TestUtils breaks makes it impossible to kill the previous processes.
private var optProc: Option[os.SubProcess] = None
private def closeUnitd(): Unit = {
optProc.foreach { proc =>
proc.close()
// Wait for Unit to close itself gracefully
Thread.sleep(100)
optProc = None
}
// We also try to kill the process in the pid file
if (os.exists(pid)) {
os.proc("kill", os.read(pid).trim).call(check = false)
}
}
def runBackground(config: ujson.Obj): Unit = {
closeUnitd()
val statedir = dest / "statedir"
os.makeDir.all(statedir)
os.write.over(statedir / "conf.json", config)
os.remove(control)
val started = new AtomicBoolean(false)
optProc = Some(
os.proc(
"unitd",
"--no-daemon",
"--log",
dest / "log.txt",
"--statedir",
statedir,
"--control",
s"unix:$control",
"--pid",
pid
).spawn(
stdout = os.Inherit,
stderr = os.ProcessOutput.Readlines(line => {
line match {
case s"$_ unit $_ started" =>
Thread.sleep(100)
started.set(true)
case _ =>
}
System.err.println(line)
})
)
)
while (!started.get()) {
println("Waiting for unit to start...")
Thread.sleep(100)
}
}