summaryrefslogtreecommitdiff
path: root/crashboxd/src/main/scala/io/crashbox/ci/Builder.scala
blob: a6269e8c195436e8c900c2e1409199d6764d2995 (plain) (blame)
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
89
90
91
92
93
94
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 }


case class BuildId(id: String) extends AnyVal

case class TaskId(buildId: String, taskIdx: Int) {
  override def toString = s"$buildId#$taskIdx"
}

class BuildSource[E <: Environment](
  taskId: TaskId,
  taskDef: TaskDef[E],
  executor: Executor[E],
  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[ExecutionId] = 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

}