diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2018-05-19 09:37:25 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-19 09:37:25 -0700 |
commit | a7cb99f1bce04366f688d36bc9faef30161da8e7 (patch) | |
tree | 8e4d03449536bd0400f454c863fc3031c4f02f7b /main/client | |
parent | b03cf740533810e95774c079c76788d7ad61d8a2 (diff) | |
download | mill-a7cb99f1bce04366f688d36bc9faef30161da8e7.tar.gz mill-a7cb99f1bce04366f688d36bc9faef30161da8e7.tar.bz2 mill-a7cb99f1bce04366f688d36bc9faef30161da8e7.zip |
WIP keep mill server alive if you Ctrl-C during --watch (#339)
* wip
* Clean up more resources in the Mill client after every command
* catch and ignore SIGINT in Mill server to make it survive Ctrl-C on the client
Diffstat (limited to 'main/client')
-rw-r--r-- | main/client/src/mill/main/client/Lock.java | 3 | ||||
-rw-r--r-- | main/client/src/mill/main/client/Locks.java | 20 | ||||
-rw-r--r-- | main/client/src/mill/main/client/MillClientMain.java (renamed from main/client/src/mill/main/client/Main.java) | 88 |
3 files changed, 66 insertions, 45 deletions
diff --git a/main/client/src/mill/main/client/Lock.java b/main/client/src/mill/main/client/Lock.java index 890a352b..6e5f18b0 100644 --- a/main/client/src/mill/main/client/Lock.java +++ b/main/client/src/mill/main/client/Lock.java @@ -1,7 +1,8 @@ package mill.main.client; -public abstract class Lock{ +public abstract class Lock implements AutoCloseable{ abstract public Locked lock() throws Exception; abstract public Locked tryLock() throws Exception; + public void await() throws Exception{ lock().release(); } diff --git a/main/client/src/mill/main/client/Locks.java b/main/client/src/mill/main/client/Locks.java index 2843973d..64259293 100644 --- a/main/client/src/mill/main/client/Locks.java +++ b/main/client/src/mill/main/client/Locks.java @@ -4,7 +4,7 @@ import java.io.RandomAccessFile; import java.nio.channels.FileChannel; import java.util.concurrent.locks.ReentrantLock; -public class Locks{ +public class Locks implements AutoCloseable{ public Lock processLock; public Lock serverLock; public Lock clientLock; @@ -24,6 +24,13 @@ public class Locks{ this.clientLock = new MemoryLock(); }}; } + + @Override + public void close() throws Exception { + processLock.close(); + serverLock.close(); + clientLock.close(); + } } class FileLocked implements Locked{ private java.nio.channels.FileLock lock; @@ -61,6 +68,12 @@ class FileLock extends Lock{ return true; } } + + @Override + public void close() throws Exception { + raf.close(); + chan.close(); + } } class MemoryLocked implements Locked{ java.util.concurrent.locks.Lock l; @@ -86,4 +99,9 @@ class MemoryLock extends Lock{ if (innerLock.tryLock()) return new MemoryLocked(innerLock); else return null; } + + @Override + public void close() throws Exception { + innerLock.unlock(); + } } diff --git a/main/client/src/mill/main/client/Main.java b/main/client/src/mill/main/client/MillClientMain.java index 98445d3c..17a043f6 100644 --- a/main/client/src/mill/main/client/Main.java +++ b/main/client/src/mill/main/client/MillClientMain.java @@ -9,10 +9,10 @@ import java.net.URL; import java.nio.channels.FileChannel; import java.util.*; -public class Main { +public class MillClientMain { static void initServer(String lockBase, boolean setJnaNoSys) throws IOException,URISyntaxException{ ArrayList<String> selfJars = new ArrayList<String>(); - ClassLoader current = Main.class.getClassLoader(); + ClassLoader current = MillClientMain.class.getClassLoader(); while(current != null){ if (current instanceof java.net.URLClassLoader) { URL[] urls = ((java.net.URLClassLoader) current).getURLs(); @@ -38,7 +38,7 @@ public class Main { } l.add("-cp"); l.add(String.join(File.pathSeparator, selfJars)); - l.add("mill.main.ServerMain"); + l.add("mill.main.MillServerMain"); l.add(lockBase); new java.lang.ProcessBuilder() @@ -48,6 +48,9 @@ public class Main { .start(); } public static void main(String[] args) throws Exception{ + System.exit(main0(args)); + } + public static int main0(String[] args) throws Exception{ boolean setJnaNoSys = System.getProperty("jna.nosys") == null; Map<String, String> env = System.getenv(); if (setJnaNoSys) { @@ -58,33 +61,35 @@ public class Main { index += 1; String lockBase = "out/mill-worker-" + index; new java.io.File(lockBase).mkdirs(); - RandomAccessFile lockFile = new RandomAccessFile(lockBase + "/clientLock", "rw"); - FileChannel channel = lockFile.getChannel(); - java.nio.channels.FileLock tryLock = channel.tryLock(); - if (tryLock == null) { - lockFile.close(); - channel.close(); - } else { - int exitCode = Main.run( - lockBase, - new Runnable() { - @Override - public void run() { - try{ - initServer(lockBase, setJnaNoSys); - }catch(Exception e){ - throw new RuntimeException(e); + + try(RandomAccessFile lockFile = new RandomAccessFile(lockBase + "/clientLock", "rw"); + FileChannel channel = lockFile.getChannel(); + java.nio.channels.FileLock tryLock = channel.tryLock(); + Locks locks = Locks.files(lockBase)){ + if (tryLock != null) { + int exitCode = MillClientMain.run( + lockBase, + new Runnable() { + @Override + public void run() { + try{ + initServer(lockBase, setJnaNoSys); + }catch(Exception e){ + throw new RuntimeException(e); + } } - } - }, - Locks.files(lockBase), - System.in, - System.out, - System.err, - args, - env - ); - System.exit(exitCode); + }, + locks, + System.in, + System.out, + System.err, + args, + env + ); + return exitCode; + } + } finally{ + } } throw new Exception("Reached max process limit: " + 5); @@ -99,12 +104,12 @@ public class Main { String[] args, Map<String, String> env) throws Exception{ - FileOutputStream f = new FileOutputStream(lockBase + "/run"); - f.write(System.console() != null ? 1 : 0); - Util.writeString(f, System.getProperty("MILL_VERSION")); - Util.writeArgs(args, f); - Util.writeMap(env, f); - f.close(); + try(FileOutputStream f = new FileOutputStream(lockBase + "/run")){ + f.write(System.console() != null ? 1 : 0); + Util.writeString(f, System.getProperty("MILL_VERSION")); + Util.writeArgs(args, f); + Util.writeMap(env, f); + } boolean serverInit = false; if (locks.processLock.probe()) { @@ -120,6 +125,7 @@ public class Main { Socket ioSocket = null; long retryStart = System.currentTimeMillis(); + while(ioSocket == null && System.currentTimeMillis() - retryStart < 1000){ try{ ioSocket = Util.isWindows? @@ -146,16 +152,12 @@ public class Main { locks.serverLock.await(); - try{ - return Integer.parseInt( - new BufferedReader( - new InputStreamReader( - new FileInputStream(lockBase + "/exitCode") - ) - ).readLine() - ); + try(FileInputStream fos = new FileInputStream(lockBase + "/exitCode")){ + return Integer.parseInt(new BufferedReader(new InputStreamReader(fos)).readLine()); } catch(Throwable e){ return 1; + } finally{ + ioSocket.close(); } } } |