summaryrefslogtreecommitdiff
path: root/main/src/mill/ServerClient.scala
blob: 383a886571c2ab9b2c1d75bd81a26f5b0ee09306 (plain) (blame)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package mill

import java.io._
import java.nio.channels.FileChannel
import java.util

import ammonite.main.Cli
import mill.eval.Evaluator
import mill.main.MainRunner

class ServerClient(lockBase: String){
  val inFile = new java.io.File(lockBase + "/stdin")
  val outErrFile = new java.io.File(lockBase + "/stdouterr")
  val metaFile = new java.io.File(lockBase + "/stdmeta")
  val logFile = new java.io.File(lockBase + "/log")
  val runFile = new java.io.File(lockBase + "/run")
  val tmpRunFile = new java.io.File(lockBase + "/run-tmp")
  val pidFile = new java.io.File(lockBase + "/pid")
}
object Client{
  def WithLock[T](index: Int)(f: String => T): T = {
    val lockBase = "out/mill-worker-" + index
    new java.io.File(lockBase).mkdirs()
    val lockFile = new RandomAccessFile(lockBase+ "/lock", "rw")
    val channel = lockFile.getChannel
    channel.tryLock() match{
      case null =>
        lockFile.close()
        channel.close()
        if (index < 5) WithLock(index + 1)(f)
        else throw new Exception("Reached max process limit: " + 5)
      case locked =>
        try f(lockBase)
        finally{
          locked.release()
          lockFile.close()
          channel.close()
        }
    }
  }

  def main(args: Array[String]): Unit = {
    WithLock(1) { lockBase =>
      new Client(lockBase).run(args)
    }
  }
}


class Client(lockBase: String) extends ServerClient(lockBase){
  def run(args: Array[String]) = {

    outErrFile.delete()
    metaFile.delete()
    outErrFile.createNewFile()
    metaFile.createNewFile()
    inFile.createNewFile()
    inFile.createNewFile()
    logFile.createNewFile()

    val f = new FileOutputStream(tmpRunFile)
    f.write(if (System.console() != null) 1 else 0)
    f.write(args.length)
    var i = 0
    while (i < args.length){
      f.write(args(i).length)
      f.write(args(i).getBytes)
      i += 1
    }
    f.flush()

    tmpRunFile.renameTo(runFile)
    val in = new FileOutputStream(inFile)
    val outErr = new FileInputStream(outErrFile)
    val meta = new FileInputStream(metaFile)

    val pidRaf = new RandomAccessFile(pidFile, "rw")
    val pidLockChannel = pidRaf.getChannel

    if (!probeLock(pidLockChannel)){
      val selfJar = getClass.getProtectionDomain.getCodeSource.getLocation.toURI.getPath

      val l = new java.util.ArrayList[String]
      l.add("java")
      val props = System.getProperties
      val keys = props.stringPropertyNames().iterator()
      while(keys.hasNext){
        val k = keys.next()
        if (k.startsWith("MILL_")) l.add("-D" + k + "=" + props.getProperty(k))
      }
      l.add("-cp")
      l.add(selfJar)
      l.add("mill.Server")
      l.add(lockBase.toString)

      new java.lang.ProcessBuilder()
        .command(l)
        .redirectInput(inFile)
        .redirectOutput(logFile)
        .redirectError(logFile)
        .start()
    }


    while(!probeLock(pidLockChannel)) Thread.sleep(3)

    try {
      val buffer = new Array[Byte](1024)
      val metaBuffer = new Array[Byte](1024)
      while ({
        Thread.sleep(3)
        while ( {
          forwardForked(buffer, metaBuffer, meta, outErr) |
          forward(buffer, System.in, in)
        }) ()

        runFile.exists() && probeLock(pidLockChannel)
      }) ()
    }finally {
      pidLockChannel.close()

//      pidFile.delete()
      inFile.delete()
      outErrFile.delete()
      metaFile.delete()
    }
  }

  def probeLock(pidLockChannel: FileChannel) = {

    pidLockChannel.tryLock() match{
      case null => true
      case locked =>
        locked.release()
        false
    }

  }
  def forwardForked(buffer: Array[Byte],
                    metaBuffer: Array[Byte],
                    meta: InputStream,
                    outErr: InputStream) = {

    if (outErr.available() > 0){
      val outErrN = outErr.read(buffer)
      if (outErrN > 0) {
        var metaN = 0
        while (metaN < outErrN) {
          val delta = meta.read(metaBuffer, 0, outErrN - metaN)
          if (delta > 0) {
            var i = 0
            while (i < delta) {
              metaBuffer(i) match {
                case 0 => System.out.write(buffer(metaN + i))
                case 1 => System.err.write(buffer(metaN + i))
              }
              i += 1
            }
            metaN += delta
          }
        }
      }

      true
    }else false
  }
  def forward(buffer: Array[Byte], src: InputStream, dest: OutputStream) = {
    if (src.available() != 0){
      val n = src.read(buffer)
      dest.write(buffer, 0, n)
      true
    }else false
  }
}

class ProxyOutputStream(x: => java.io.OutputStream,
                        meta: => java.io.OutputStream,
                        key: Int) extends java.io.OutputStream  {
  override def write(b: Int) = Server.synchronized{
    x.write(b)
    meta.write(key)
  }
}
class ProxyInputStream(x: => java.io.InputStream) extends java.io.InputStream{
  def read() = x.read()
  override def read(b: Array[Byte], off: Int, len: Int) = x.read(b, off, len)
  override def read(b: Array[Byte]) = x.read(b)
}

object Server{
  def main(args0: Array[String]): Unit = {
    new Server(args0(0)).run()

  }
}

class ProbeThread(lockChannel: FileChannel,
                  mainThread: Thread,
                  log: PrintStream) extends Runnable{
  var running = true
  def run() = {
    while({
      Thread.sleep(3)
      lockChannel.tryLock() match{
        case null => true && running
        case locked =>
          locked.release()
          lockChannel.close()
          System.exit(0)
          false
      }
    })()
  }
}

class Server(lockBase: String) extends ServerClient(lockBase){
  var lastRun = System.currentTimeMillis()
  var currentIn = System.in
  var currentOutErr: OutputStream = System.out
  var currentMeta: OutputStream = System.err
  val lockFile = new RandomAccessFile(lockBase + "/lock", "rw")
  val channel = lockFile.getChannel
  var stateCache = Option.empty[Evaluator.State]

  def run() = {
    val originalStdout = System.out
    originalStdout.println("Initializing")
    val pidRaf = new RandomAccessFile(pidFile, "rw")
    val lockChannel = pidRaf.getChannel
    val lock = lockChannel.tryLock()
    if (lock == null) throw new Exception("PID already present")
    originalStdout.println("Locked pid file")
    try {
      while (System.currentTimeMillis() - lastRun < 60000) {
        pollOrRun(originalStdout)
        originalStdout.println("Delta " + (System.currentTimeMillis() - lastRun))
        originalStdout.println("Threads " + Thread.activeCount())
      }
    }finally{
      originalStdout.println("Exiting Server... " + System.currentTimeMillis())
      lock.release()
      lockChannel.close()
      pidRaf.close()
      pidFile.delete()
    }
    originalStdout.println("END")
  }
  def pollOrRun(originalStdout: PrintStream) = {
    if (!runFile.exists()) Thread.sleep(30)
    else try{
      originalStdout.println("Handling Run")
      handleRun(originalStdout)
    }catch{
      case e: Throwable =>
        originalStdout.println("Run Failed")
        e.printStackTrace(originalStdout)
    }finally{
      lastRun = System.currentTimeMillis()
      originalStdout.println("Updating lastRun " + lastRun)
    }
  }
  def handleRun(originalStdout: PrintStream) = {
    currentIn = new FileInputStream(inFile)
    currentOutErr = new FileOutputStream(outErrFile)
    currentMeta = new FileOutputStream(metaFile)
    val lockChannel = lockFile.getChannel
    val probe = new ProbeThread(lockChannel, Thread.currentThread(), originalStdout)
    val probeThread = new Thread(probe)
    probeThread.start()
    val argStream = new FileInputStream(runFile)
    val interactive = argStream.read() != 0
    val argsLength = argStream.read()
    val args = Array.fill(argsLength){
      val n = argStream.read()
      val arr = new Array[Byte](n)
      argStream.read(arr)
      new String(arr)
    }
    originalStdout.println("Parsed Args " + args.toList)
    try {
      originalStdout.println("Running Main")
      val (_, newStateCache) = mill.Main.main0(
        args,
        stateCache,
        interactive,
        () => {
          channel.tryLock()  match{
            case null =>
              false
            case lock =>
              lock.release()
              true
          }
        },
        new ProxyInputStream(currentIn),
        new PrintStream(new ProxyOutputStream(currentOutErr, currentMeta, 0), true),
        new PrintStream(new ProxyOutputStream(currentOutErr, currentMeta, 1), true)
      )
      originalStdout.println("Finished Main")
      stateCache = newStateCache
    } catch{case MainRunner.WatchInterrupted(sc) =>
      stateCache = sc
    } finally{
//      lockChannel.close()
//      lockFile.close()
      probe.running = false
      probeThread.join()
      runFile.delete()

    }
  }
}