@@ -14,6 +14,7 @@ import java.util.concurrent.Executors
14
14
import java .util .concurrent .ThreadFactory
15
15
import java .util .concurrent .atomic .AtomicInteger
16
16
17
+ import scala .annotation .tailrec
17
18
import scala .concurrent .duration .DurationInt
18
19
import scala .util .Properties
19
20
import scala .util .Try
@@ -85,12 +86,31 @@ object Bloop {
85
86
case Left (hostPort) =>
86
87
startServer(Left (hostPort))
87
88
case Right (lockFiles) =>
88
- Lock .tryAcquire(lockFiles, LockProcess .default) {
89
- startServer(Right (lockFiles.socketPaths))
90
- } match {
91
- case Left (err) => throw new Exception (err)
92
- case Right (()) =>
89
+ val period = 3 .second
90
+ val attempts = 10
91
+
92
+ @ tailrec
93
+ def loop (remainingAttempts : Int ): Unit = {
94
+ val res = Lock .tryAcquire(lockFiles, LockProcess .default) {
95
+ startServer(Right (lockFiles.socketPaths))
96
+ }
97
+ res match {
98
+ case Left (err : LockError .RecoverableError ) =>
99
+ if (remainingAttempts > 0 ) {
100
+ System .err.println(s " Caught $err, trying again in $period" )
101
+ Thread .sleep(period.toMillis)
102
+ loop(remainingAttempts - 1 )
103
+ } else
104
+ throw new Exception (err)
105
+ case Left (_ : LockError .AlreadyRunning ) =>
106
+ sys.exit(222 ) // Special exit code if a server is already running
107
+ case Left (err : LockError .FatalError ) =>
108
+ throw new Exception (err)
109
+ case Right (()) =>
110
+ }
93
111
}
112
+
113
+ loop(attempts)
94
114
}
95
115
}
96
116
0 commit comments