1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
package io.crashbox.ci
import akka.actor.{ Actor, ActorLogging, ActorRef, ActorSystem }
import akka.stream.SourceShape
import akka.stream.stage.{ GraphStageLogic, InHandler, OutHandler, StageLogging }
import akka.stream.{ Attributes, FanInShape2, Inlet, Outlet }
import akka.stream.stage.{ GraphStage }
import java.io.{ File, OutputStream }
import java.util.concurrent.atomic.AtomicInteger
import scala.collection.concurrent.TrieMap
import scala.collection.mutable.{ HashMap, Queue }
import scala.concurrent.Future
import scala.util.{ Failure, Success }
class BuildSource[Env <: Environment, Id <: ExecutionId](
taskId: TaskId,
taskDef: TaskDef[Env],
executor: Executor[Env, Id],
mkdir: => File,
mkout: => OutputStream // TODO refactor this into a two-output stage
) extends GraphStage[SourceShape[Builder.BuildState]] {
import Builder._
val states = Outlet[BuildState]("Builder.states")
val shape = new SourceShape(states)
def createLogic(attributes: Attributes) = new GraphStageLogic(shape) with StageLogging {
implicit def ec = materializer.executionContext
lazy val instance: Future[Id] = executor.start(
taskDef.environment,
taskDef.script,
mkdir,
mkout
)
val changeState = getAsyncCallback[BuildState]{state =>
log.info(s"${state.taskId} transitioned to $state")
emit(states, state)
}
val asyncClose = getAsyncCallback[Unit]{_ =>
completeStage()
}
override def preStart() = {
val pipeline = for (
_ <- Future.unit;
_ = changeState.invoke(TaskStarting(taskId, taskDef));
eid <- instance;
_ = changeState.invoke(TaskRunning(taskId, eid));
status <- executor.result(eid)
) yield status
pipeline onComplete { result =>
result match {
case Success(status) =>
changeState.invoke(TaskFinished(taskId, status))
case Failure(err) =>
changeState.invoke(TaskFailed(taskId, err.toString))
}
asyncClose.invoke(())
}
}
override def postStop() = {
instance.foreach{ eid => executor.stop(eid)}
}
setHandler(states, GraphStageLogic.IgnoreTerminateOutput)
}
}
object Builder {
sealed trait BuildState {
def taskId: TaskId
}
case class TaskStarting(taskId: TaskId, taskDef: TaskDef[Environment]) extends BuildState
case class TaskRunning(taskId: TaskId, execId: ExecutionId) extends BuildState
case class TaskFinished(taskId: TaskId, status: Int) extends BuildState
case class TaskFailed(taskId: TaskId, message: String) extends BuildState
}
|