Skip to content

Commit 717d710

Browse files
Merge pull request #128 from alexarchambault/retry
Re-attempt to acquire lock upon start-up when possible
2 parents 1e07247 + 5fc67ca commit 717d710

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

frontend/src/main/scala/bloop/Bloop.scala

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import java.util.concurrent.Executors
1414
import java.util.concurrent.ThreadFactory
1515
import java.util.concurrent.atomic.AtomicInteger
1616

17+
import scala.annotation.tailrec
1718
import scala.concurrent.duration.DurationInt
1819
import scala.util.Properties
1920
import scala.util.Try
@@ -85,12 +86,31 @@ object Bloop {
8586
case Left(hostPort) =>
8687
startServer(Left(hostPort))
8788
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+
}
93111
}
112+
113+
loop(attempts)
94114
}
95115
}
96116

0 commit comments

Comments
 (0)