Skip to content

Commit f5fb8f0

Browse files
author
Johann Egger
committed
Add a timeout to the application
you can set the timeout via the java property, the value has to be a (Finite)Duration. example: -Dtimeout="10.seconds"
1 parent 11164c3 commit f5fb8f0

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

build.sbt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ scalacOptions := Seq("-deprecation", "-feature", "-unchecked", "-Ywarn-adapted-a
1313
resolvers += "Bintray Typesafe Repo" at "http://dl.bintray.com/typesafe/maven-releases/"
1414

1515
libraryDependencies ++= Seq(
16-
"com.typesafe.play" %% "play-json" % "2.3.10",
17-
"org.scalatest" %% "scalatest" % "2.2.4" % "test"
16+
"com.typesafe.play" %% "play-json" % "2.3.10",
17+
"org.scalatest" %% "scalatest" % "2.2.4" % "test",
18+
"com.typesafe.akka" %% "akka-actor" % "2.3.14"
1819
)
1920

2021
organizationName := "Codacy"

src/main/scala/codacy/dockerApi/DockerEngine.scala

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
package codacy.dockerApi
22

3+
import akka.actor.ActorSystem
34
import codacy.dockerApi.DockerEnvironment._
45
import play.api.libs.json.{Json, Writes}
56

6-
import scala.util.{Failure, Success}
7+
import scala.concurrent.ExecutionContext
8+
import scala.concurrent.duration._
9+
import scala.util.{Failure, Success, Try}
710

811
abstract class DockerEngine(Tool: Tool) {
912

13+
lazy val sys = ActorSystem("timeoutSystem")
14+
15+
def initTimeout(duration: FiniteDuration) = {
16+
implicit val ct: ExecutionContext = sys.dispatcher
17+
sys.scheduler.scheduleOnce(duration){
18+
Runtime.getRuntime().halt(1)
19+
}
20+
}
21+
22+
lazy val timeout = Option(System.getProperty("timeout")).flatMap{ case rawDuration =>
23+
Try(Duration(rawDuration)).toOption.collect{ case d:FiniteDuration => d }
24+
}.getOrElse(30.minutes)
25+
1026
def main(args: Array[String]): Unit = {
27+
initTimeout(timeout)
28+
1129
spec.flatMap { implicit spec =>
1230
config.flatMap { case maybeConfig =>
1331
//search for our config

0 commit comments

Comments
 (0)