Skip to content

Commit

Permalink
Add execAndRetrieveOutput to fork a process and retrieve its output. (
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumebort authored Jul 27, 2017
1 parent f4d8a3d commit 083e17a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
val devMode = settingKey[Boolean]("Some build optimization are applied in devMode.")
val writeClasspath = taskKey[File]("Write the project classpath to a file.")

val VERSION = "0.1.7"
val VERSION = "0.1.8"

lazy val commonSettings = Seq(
organization := "com.criteo.cuttle",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ object LocalPlatform {
class LocalProcess(command: String) {
val id = UUID.randomUUID().toString

def exec[S <: Scheduling](env: Map[String, String] = sys.env)(implicit execution: Execution[S]): Future[Unit] = {
private def exec0[S <: Scheduling](
env: Map[String, String] = sys.env,
outLogger: (String) => Unit,
errLogger: (String) => Unit
)(implicit execution: Execution[S]): Future[Unit] = {
val streams = execution.streams
streams.debug(s"Forking:")
streams.debug(this.toString)
Expand All @@ -43,12 +47,16 @@ class LocalProcess(command: String) {
override def onStdout(buffer: ByteBuffer, closed: Boolean) = {
val bytes = Array.ofDim[Byte](buffer.remaining)
buffer.get(bytes)
streams.info(new String(bytes))
val str = new String(bytes)
streams.info(str)
outLogger(str)
}
override def onStderr(buffer: ByteBuffer, closed: Boolean) = {
val bytes = Array.ofDim[Byte](buffer.remaining)
buffer.get(bytes)
streams.error(new String(bytes))
val str = new String(bytes)
streams.error(str)
errLogger(str)
}
override def onExit(statusCode: Int) =
statusCode match {
Expand All @@ -68,5 +76,15 @@ class LocalProcess(command: String) {
result.future
}
}

def exec[S <: Scheduling](env: Map[String, String] = sys.env)(implicit execution: Execution[S]): Future[Unit] =
exec0(env, _ => (), _ => ())

def execAndRetrieveOutput[S <: Scheduling](env: Map[String, String] = sys.env)(implicit execution: Execution[S]): Future[(String,String)] = {
val out = new StringBuffer
val err = new StringBuffer
exec0(env, x => out.append(x), x => err.append(x)).map(_ => (out.toString, err.toString))
}

override def toString = command
}

0 comments on commit 083e17a

Please sign in to comment.