src/Pure/Build/build_manager.scala
author Fabian Huch <huch@in.tum.de>
Tue, 04 Jun 2024 18:55:55 +0200
changeset 80251 6ae378791c52
parent 80250 8ae6f4e8cc2a
child 80252 96543177ab7e
permissions -rw-r--r--
read prefs properly;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
80246
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     1
/*  Title:      Pure/Build/build_manager.scala
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     2
    Author:     Fabian Huch, TU Muenchen
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     3
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     4
Isabelle manager for automated and quasi-interactive builds, with web frontend.
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     5
*/
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     6
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     7
package isabelle
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     8
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
     9
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    10
import scala.collection.mutable
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    11
import scala.annotation.tailrec
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    12
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    13
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    14
object Build_Manager {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    15
  /* task state synchronized via db */
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    16
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    17
  object Component {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    18
    def parse(s: String): Component =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    19
      space_explode('/', s) match {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    20
        case name :: rev :: Nil => Component(name, rev)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    21
        case _ => error("Malformed component: " + quote(s))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    22
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    23
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    24
    def AFP(rev: String = "") = Component("AFP", rev)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    25
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    26
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    27
  case class Component(name: String, rev: String = "") {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    28
    override def toString: String = name + "/" + rev
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    29
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    30
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    31
  sealed trait Build_Config {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    32
    def name: String
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    33
    def components: List[Component]
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    34
    def fresh_build: Boolean
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    35
    def command(build_hosts: List[Build_Cluster.Host]): String
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    36
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    37
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    38
  case class CI_Build(name: String, components: List[Component]) extends Build_Config {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    39
    def fresh_build: Boolean = true
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    40
    def command(build_hosts: List[Build_Cluster.Host]): String = " ci_build " + name
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    41
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    42
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    43
  object User_Build {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    44
    val name: String = "user"
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    45
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    46
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    47
  case class User_Build(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    48
    afp_rev: Option[String] = None,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    49
    prefs: List[Options.Spec] = Nil,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    50
    requirements: Boolean = false,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    51
    all_sessions: Boolean = false,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    52
    base_sessions: List[String] = Nil,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    53
    exclude_session_groups: List[String] = Nil,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    54
    exclude_sessions: List[String] = Nil,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    55
    session_groups: List[String] = Nil,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    56
    sessions: List[String] = Nil,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    57
    build_heap: Boolean = false,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    58
    clean_build: Boolean = false,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    59
    export_files: Boolean = false,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    60
    fresh_build: Boolean = false,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    61
    presentation: Boolean = false
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    62
  ) extends Build_Config {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    63
    def name: String = User_Build.name
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    64
    def components: List[Component] = afp_rev.map(Component.AFP).toList
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    65
    def command(build_hosts: List[Build_Cluster.Host]): String = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    66
      " build" +
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    67
        if_proper(afp_rev, " -A:") +
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    68
        base_sessions.map(session => " -B " + Bash.string(session)).mkString +
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    69
        if_proper(build_hosts, build_hosts.map(host => " -H " + Bash.string(host.print)).mkString) +
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    70
        if_proper(presentation, " -P:") +
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    71
        if_proper(requirements, " -R") +
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    72
        if_proper(all_sessions, " -a") +
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    73
        if_proper(build_heap, " -b") +
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    74
        if_proper(clean_build, " -c") +
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    75
        if_proper(export_files, " -e") +
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    76
        if_proper(fresh_build, " -f") +
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    77
        Options.Spec.bash_strings(prefs, bg = true) +
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    78
        " -v" +
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    79
        sessions.map(session => " " + Bash.string(session)).mkString
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    80
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    81
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    82
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    83
  enum Priority { case low, normal, high }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    84
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    85
  sealed trait T extends Library.Named
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    86
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    87
  sealed case class Task(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    88
    build_config: Build_Config,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    89
    id: UUID.T = UUID.random(),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    90
    submit_date: Date = Date.now(),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    91
    priority: Priority = Priority.normal,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    92
    isabelle_rev: String = ""
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    93
  ) extends T {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    94
    def name: String = id.toString
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    95
    def kind: String = build_config.name
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    96
    def components: List[Component] = build_config.components
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    97
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    98
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
    99
  sealed case class Job(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   100
    id: UUID.T,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   101
    kind: String,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   102
    number: Long,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   103
    isabelle_rev: String,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   104
    components: List[Component],
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   105
    start_date: Date = Date.now(),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   106
    cancelled: Boolean = false
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   107
  ) extends T { def name: String = kind + "/" + number }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   108
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   109
  object Status {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   110
    def from_result(result: Process_Result): Status = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   111
      if (result.ok) Status.ok
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   112
      else if (result.interrupted) Status.cancelled
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   113
      else Status.failed
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   114
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   115
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   116
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   117
  enum Status { case ok, cancelled, aborted, failed  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   118
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   119
  sealed case class Result(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   120
    kind: String,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   121
    number: Long,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   122
    status: Status,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   123
    id: Option[UUID.T] = None,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   124
    date: Date = Date.now(),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   125
    serial: Long = 0,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   126
  ) extends T { def name: String = kind + "/" + number }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   127
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   128
  object State {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   129
    def max_serial(serials: Iterable[Long]): Long = serials.maxOption.getOrElse(0L)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   130
    def inc_serial(serial: Long): Long = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   131
      require(serial < Long.MaxValue, "number overflow")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   132
      serial + 1
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   133
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   134
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   135
    type Pending = Library.Update.Data[Task]
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   136
    type Running = Library.Update.Data[Job]
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   137
    type Finished = Map[String, Result]
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   138
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   139
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   140
  sealed case class State(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   141
    serial: Long = 0,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   142
    pending: State.Pending = Map.empty,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   143
    running: State.Running = Map.empty,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   144
    finished: State.Finished = Map.empty
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   145
  ) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   146
    def next_serial: Long = State.inc_serial(serial)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   147
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   148
    def add_pending(task: Task): State = copy(pending = pending + (task.name -> task))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   149
    def remove_pending(name: String): State = copy(pending = pending - name)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   150
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   151
    def num_builds = running.size + finished.size
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   152
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   153
    def next: List[Task] =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   154
      if (pending.isEmpty) Nil
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   155
      else {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   156
        val priority = pending.values.map(_.priority).maxBy(_.ordinal)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   157
        pending.values.filter(_.priority == priority).toList.sortBy(_.submit_date)(Date.Ordering)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   158
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   159
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   160
    def add_running(job: Job): State = copy(running = running + (job.name -> job))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   161
    def remove_running(name: String): State = copy(running = running - name)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   162
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   163
    def add_finished(result: Result): State = copy(finished = finished + (result.name -> result))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   164
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   165
    lazy val kinds = (
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   166
      pending.values.map(_.kind) ++
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   167
      running.values.map(_.kind) ++
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   168
      finished.values.map(_.kind)).toList.distinct
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   169
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   170
    def next_number(kind: String): Long = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   171
      val serials = get_finished(kind).map(_.number) ::: get_running(kind).map(_.number)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   172
      State.inc_serial(State.max_serial(serials))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   173
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   174
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   175
    def get_running(kind: String): List[Job] =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   176
      (for ((_, job) <- running if job.kind == kind) yield job).toList
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   177
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   178
    def get_finished(kind: String): List[Result] =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   179
      (for ((_, result) <- finished if result.kind == kind) yield result).toList
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   180
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   181
    def get(name: String): Option[T] =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   182
      pending.get(name).orElse(running.get(name)).orElse(finished.get(name))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   183
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   184
    def get(id: UUID.T): Option[T] =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   185
      pending.values.find(_.id == id).orElse(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   186
        running.values.find(_.id == id)).orElse(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   187
        finished.values.find(_.id.contains(id)))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   188
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   189
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   190
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   191
  /* SQL data model */
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   192
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   193
  object private_data extends SQL.Data("isabelle_build_manager") {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   194
    /* tables */
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   195
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   196
    override lazy val tables: SQL.Tables =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   197
      SQL.Tables(State.table, Pending.table, Running.table, Finished.table)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   198
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   199
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   200
    /* state */
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   201
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   202
    object State {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   203
      val serial = SQL.Column.long("serial").make_primary_key
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   204
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   205
      val table = make_table(List(serial), name = "state")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   206
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   207
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   208
    def read_serial(db: SQL.Database): Long =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   209
      db.execute_query_statementO[Long](
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   210
        State.table.select(List(State.serial.max)),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   211
        _.long(State.serial)).getOrElse(0L)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   212
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   213
    def pull_state(db: SQL.Database, state: State): State = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   214
      val serial_db = read_serial(db)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   215
      if (serial_db == state.serial) state
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   216
      else {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   217
        val serial = serial_db max state.serial
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   218
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   219
        val pending = pull_pending(db)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   220
        val running = pull_running(db)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   221
        val finished = pull_finished(db, state.finished)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   222
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   223
        state.copy(serial = serial, pending = pending, running = running, finished = finished)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   224
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   225
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   226
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   227
    def push_state(db: SQL.Database, old_state: State, state: State): State = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   228
      val finished = push_finished(db, state.finished)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   229
      val updates =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   230
        List(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   231
          update_pending(db, old_state.pending, state.pending),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   232
          update_running(db, old_state.running, state.running),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   233
        ).filter(_.defined)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   234
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   235
      if (updates.isEmpty && finished == old_state.finished) state
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   236
      else {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   237
        val serial = state.next_serial
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   238
        db.execute_statement(State.table.delete(State.serial.where_equal(old_state.serial)))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   239
        db.execute_statement(State.table.insert(), body =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   240
          { (stmt: SQL.Statement) =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   241
            stmt.long(1) = serial
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   242
          })
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   243
        state.copy(serial = serial, finished = finished)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   244
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   245
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   246
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   247
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   248
    /* pending */
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   249
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   250
    object Pending {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   251
      val kind = SQL.Column.string("kind")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   252
      val id = SQL.Column.string("id").make_primary_key
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   253
      val submit_date = SQL.Column.date("submit_date")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   254
      val priority = SQL.Column.string("priority")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   255
      val isabelle_rev = SQL.Column.string("isabelle_rev")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   256
      val components = SQL.Column.string("components")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   257
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   258
      val prefs = SQL.Column.string("prefs")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   259
      val requirements = SQL.Column.bool("requirements")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   260
      val all_sessions = SQL.Column.bool("all_sessions")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   261
      val base_sessions = SQL.Column.string("base_sessions")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   262
      val exclude_session_groups = SQL.Column.string("exclude_session_groups")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   263
      val exclude_sessions = SQL.Column.string("exclude_sessions")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   264
      val session_groups = SQL.Column.string("session_groups")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   265
      val sessions = SQL.Column.string("sessions")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   266
      val build_heap = SQL.Column.bool("build_heap")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   267
      val clean_build = SQL.Column.bool("clean_build")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   268
      val export_files = SQL.Column.bool("export_files")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   269
      val fresh_build = SQL.Column.bool("fresh_build")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   270
      val presentation = SQL.Column.bool("presentation")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   271
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   272
      val table =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   273
        make_table(List(kind, id, submit_date, priority, isabelle_rev, components, prefs,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   274
          requirements, all_sessions, base_sessions, exclude_session_groups, exclude_sessions,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   275
          session_groups, sessions, build_heap, clean_build, export_files, fresh_build,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   276
          presentation),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   277
        name = "pending")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   278
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   279
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   280
    def pull_pending(db: SQL.Database): Build_Manager.State.Pending =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   281
      db.execute_query_statement(Pending.table.select(), Map.from[String, Task], get =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   282
        { res =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   283
          val kind = res.string(Pending.kind)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   284
          val id = res.string(Pending.id)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   285
          val submit_date = res.date(Pending.submit_date)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   286
          val priority = Priority.valueOf(res.string(Pending.priority))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   287
          val isabelle_rev = res.string(Pending.isabelle_rev)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   288
          val components = space_explode(',', res.string(Pending.components)).map(Component.parse)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   289
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   290
          val build_config =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   291
            if (kind != User_Build.name) CI_Build(kind, components)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   292
            else {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   293
              val prefs = Options.Spec.parse(res.string(Pending.prefs))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   294
              val requirements = res.bool(Pending.requirements)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   295
              val all_sessions = res.bool(Pending.all_sessions)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   296
              val base_sessions = space_explode(',', res.string(Pending.base_sessions))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   297
              val exclude_session_groups =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   298
                space_explode(',', res.string(Pending.exclude_session_groups))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   299
              val exclude_sessions = space_explode(',', res.string(Pending.exclude_sessions))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   300
              val session_groups = space_explode(',', res.string(Pending.session_groups))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   301
              val sessions = space_explode(',', res.string(Pending.sessions))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   302
              val build_heap = res.bool(Pending.build_heap)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   303
              val clean_build = res.bool(Pending.clean_build)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   304
              val export_files = res.bool(Pending.export_files)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   305
              val fresh_build = res.bool(Pending.fresh_build)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   306
              val presentation = res.bool(Pending.presentation)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   307
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   308
              val afp_rev = components.find(_.name == Component.AFP().name).map(_.rev)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   309
              User_Build(afp_rev, prefs, requirements, all_sessions, base_sessions,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   310
                exclude_session_groups, exclude_sessions, session_groups, sessions, build_heap,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   311
                clean_build, export_files, fresh_build, presentation)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   312
            }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   313
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   314
          val task = Task(build_config, UUID.make(id), submit_date, priority, isabelle_rev)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   315
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   316
          task.name -> task
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   317
        })
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   318
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   319
    def update_pending(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   320
      db: SQL.Database,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   321
      old_pending: Build_Manager.State.Pending,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   322
      pending: Build_Manager.State.Pending
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   323
    ): Library.Update = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   324
      val update = Library.Update.make(old_pending, pending)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   325
      val delete = update.delete.map(old_pending(_).id.toString)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   326
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   327
      if (update.deletes)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   328
        db.execute_statement(Pending.table.delete(Pending.id.where_member(delete)))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   329
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   330
      if (update.inserts) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   331
        db.execute_batch_statement(Pending.table.insert(), batch =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   332
          for (name <- update.insert) yield { (stmt: SQL.Statement) =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   333
            val task = pending(name)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   334
            stmt.string(1) = task.kind
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   335
            stmt.string(2) = task.id.toString
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   336
            stmt.date(3) = task.submit_date
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   337
            stmt.string(4) = task.priority.toString
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   338
            stmt.string(5) = task.isabelle_rev
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   339
            stmt.string(6) = task.components.mkString(",")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   340
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   341
            def get[A](f: User_Build => A): Option[A] =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   342
              task.build_config match {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   343
                case user_build: User_Build => Some(f(user_build))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   344
                case _ => None
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   345
              }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   346
80251
6ae378791c52 read prefs properly;
Fabian Huch <huch@in.tum.de>
parents: 80250
diff changeset
   347
            stmt.string(7) = get(user_build => user_build.prefs.map(_.print).mkString(","))
80246
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   348
            stmt.bool(8) = get(_.requirements)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   349
            stmt.bool(9) = get(_.all_sessions)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   350
            stmt.string(10) = get(_.base_sessions.mkString(","))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   351
            stmt.string(11) = get(_.exclude_session_groups.mkString(","))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   352
            stmt.string(12) = get(_.exclude_sessions.mkString(","))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   353
            stmt.string(13) = get(_.session_groups.mkString(","))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   354
            stmt.string(14) = get(_.sessions.mkString(","))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   355
            stmt.bool(15) = get(_.build_heap)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   356
            stmt.bool(16) = get(_.clean_build)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   357
            stmt.bool(17) = get(_.export_files)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   358
            stmt.bool(18) = get(_.fresh_build)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   359
            stmt.bool(19) = get(_.presentation)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   360
          })
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   361
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   362
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   363
      update
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   364
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   365
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   366
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   367
    /* running */
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   368
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   369
    object Running {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   370
      val id = SQL.Column.string("id").make_primary_key
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   371
      val kind = SQL.Column.string("kind")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   372
      val number = SQL.Column.long("number")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   373
      val isabelle_rev = SQL.Column.string("isabelle_rev")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   374
      val components = SQL.Column.string("components")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   375
      val start_date = SQL.Column.date("start_date")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   376
      val cancelled = SQL.Column.bool("cancelled")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   377
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   378
      val table =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   379
        make_table(List(id, kind, number, isabelle_rev, components, start_date, cancelled),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   380
        name = "running")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   381
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   382
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   383
    def pull_running(db: SQL.Database): Build_Manager.State.Running =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   384
      db.execute_query_statement(Running.table.select(), Map.from[String, Job], get =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   385
        { res =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   386
          val id = res.string(Running.id)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   387
          val kind = res.string(Running.kind)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   388
          val number = res.long(Running.number)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   389
          val isabelle_rev = res.string(Running.isabelle_rev)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   390
          val components = space_explode(',', res.string(Running.components)).map(Component.parse)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   391
          val start_date = res.date(Running.start_date)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   392
          val cancelled = res.bool(Running.cancelled)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   393
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   394
          val job =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   395
            Job(UUID.make(id), kind, number, isabelle_rev, components, start_date, cancelled)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   396
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   397
          job.name -> job
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   398
        })
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   399
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   400
    def update_running(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   401
      db: SQL.Database,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   402
      old_running: Build_Manager.State.Running,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   403
      running: Build_Manager.State.Running
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   404
    ): Library.Update = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   405
      val update = Library.Update.make(old_running, running)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   406
      val delete = update.delete.map(old_running(_).id.toString)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   407
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   408
      if (update.deletes)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   409
        db.execute_statement(Running.table.delete(Running.id.where_member(delete)))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   410
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   411
      if (update.inserts) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   412
        db.execute_batch_statement(Running.table.insert(), batch =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   413
          for (name <- update.insert) yield { (stmt: SQL.Statement) =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   414
            val job = running(name)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   415
            stmt.string(1) = job.id.toString
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   416
            stmt.string(2) = job.kind
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   417
            stmt.long(3) = job.number
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   418
            stmt.string(4) = job.isabelle_rev
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   419
            stmt.string(5) = job.components.mkString(",")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   420
            stmt.date(6) = job.start_date
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   421
            stmt.bool(7) = job.cancelled
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   422
          })
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   423
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   424
      update
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   425
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   426
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   427
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   428
    /* finished */
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   429
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   430
    object Finished {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   431
      val kind = SQL.Column.string("kind")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   432
      val number = SQL.Column.long("number")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   433
      val status = SQL.Column.string("status")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   434
      val id = SQL.Column.string("id")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   435
      val date = SQL.Column.date("date")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   436
      val serial = SQL.Column.long("serial").make_primary_key
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   437
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   438
      val table = make_table(List(kind, number, status, id, date, serial), name = "finished")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   439
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   440
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   441
    def read_finished_serial(db: SQL.Database): Long =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   442
      db.execute_query_statementO[Long](
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   443
        Finished.table.select(List(Finished.serial.max)),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   444
        _.long(Finished.serial)).getOrElse(0L)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   445
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   446
    def pull_finished(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   447
      db: SQL.Database,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   448
      finished: Build_Manager.State.Finished
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   449
    ): Build_Manager.State.Finished = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   450
      val max_serial0 = Build_Manager.State.max_serial(finished.values.map(_.serial))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   451
      val max_serial1 = read_finished_serial(db)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   452
      val missing = (max_serial0 + 1) to max_serial1
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   453
      finished ++ db.execute_query_statement(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   454
        Finished.table.select(sql = Finished.serial.where_member_long(missing)),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   455
        Map.from[String, Result], get =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   456
        { res =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   457
          val kind = res.string(Finished.kind)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   458
          val number = res.long(Finished.number)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   459
          val status = Status.valueOf(res.string(Finished.status))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   460
          val id = res.get_string(Finished.id).map(UUID.make)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   461
          val date = res.date(Finished.date)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   462
          val serial = res.long(Finished.serial)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   463
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   464
          val result = Result(kind, number, status, id, date, serial)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   465
          result.name -> result
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   466
        }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   467
      )
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   468
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   469
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   470
    def push_finished(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   471
      db: SQL.Database,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   472
      finished: Build_Manager.State.Finished
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   473
    ): Build_Manager.State.Finished = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   474
      val (insert0, old) = finished.partition(_._2.serial == 0L)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   475
      val max_serial = Build_Manager.State.max_serial(finished.map(_._2.serial))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   476
      val insert =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   477
        for (((_, result), n) <- insert0.zipWithIndex)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   478
        yield result.copy(serial = max_serial + 1 + n)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   479
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   480
      if (insert.nonEmpty)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   481
        db.execute_batch_statement(Finished.table.insert(), batch =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   482
          for (result <- insert) yield { (stmt: SQL.Statement) =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   483
            stmt.string(1) = result.kind
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   484
            stmt.long(2) = result.number
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   485
            stmt.string(3) = result.status.toString
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   486
            stmt.string(4) = result.id.map(_.toString)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   487
            stmt.date(5) = result.date
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   488
            stmt.long(6) = result.serial
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   489
          })
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   490
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   491
      old ++ insert.map(result => result.serial.toString -> result)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   492
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   493
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   494
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   495
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   496
  /* running build manager processes */
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   497
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   498
  abstract class Loop_Process[A](name: String, store: Store, progress: Progress)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   499
    extends Runnable {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   500
    val options = store.options
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   501
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   502
    private val _database =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   503
      try { store.open_database() }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   504
      catch { case exn: Throwable => close(); throw exn }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   505
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   506
    def close(): Unit = Option(_database).foreach(_.close())
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   507
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   508
    protected var _state = State()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   509
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   510
    protected def synchronized_database[A](label: String)(body: => A): A = synchronized {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   511
      Build_Manager.private_data.transaction_lock(_database, label = name + "." + label) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   512
        val old_state = Build_Manager.private_data.pull_state(_database, _state)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   513
        _state = old_state
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   514
        val res = body
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   515
        _state = Build_Manager.private_data.push_state(_database, old_state, _state)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   516
        res
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   517
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   518
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   519
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   520
    protected def delay = options.seconds("build_manager_delay")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   521
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   522
    def init: A
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   523
    def loop_body(a: A): A
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   524
    def stopped(a: A): Boolean = progress.stopped
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   525
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   526
    private val interrupted = Synchronized(false)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   527
    private def sleep(time_limit: Time): Unit =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   528
      interrupted.timed_access(_ => Some(time_limit), b => if (b) Some((), false) else None)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   529
    def interrupt(): Unit = interrupted.change(_ => true)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   530
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   531
    @tailrec private def loop(a: A): Unit =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   532
      if (!stopped(a)) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   533
        val start = Time.now()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   534
        val a1 = loop_body(a)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   535
        if (!stopped(a)) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   536
          sleep(start + delay)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   537
          loop(a1)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   538
        }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   539
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   540
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   541
    override def run(): Unit = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   542
      progress.echo("Started " + name)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   543
      loop(init)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   544
      close()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   545
      progress.echo("Stopped " + name)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   546
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   547
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   548
    def echo(msg: String) = progress.echo(name + ": " + msg)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   549
    def echo_error_message(msg: String) = progress.echo_error_message(name + ": " + msg)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   550
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   551
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   552
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   553
  /* build runner */
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   554
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   555
  object Runner {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   556
    object State {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   557
      def empty: State = new State(Map.empty, Map.empty)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   558
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   559
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   560
    class State private(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   561
      processes: Map[String, Future[Bash.Process]],
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   562
      results: Map[String, Future[Process_Result]]
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   563
    ) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   564
      def is_empty = processes.isEmpty && results.isEmpty
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   565
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   566
      def init(build_config: Build_Config, job: Job, context: Context): State = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   567
        val process = Future.fork(context.process(build_config))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   568
        val result =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   569
          Future.fork(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   570
            process.join_result match {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   571
              case Exn.Res(res) => context.run(res)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   572
              case Exn.Exn(_) => Process_Result(Process_Result.RC.interrupt)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   573
            })
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   574
        new State(processes + (job.name -> process), results + (job.name -> result))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   575
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   576
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   577
      def running: List[String] = processes.keys.toList
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   578
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   579
      def update: (State, Map[String, Process_Result]) = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   580
        val finished =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   581
          for ((name, future) <- results if future.is_finished) yield name -> future.join
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   582
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   583
        val processes1 = processes.filterNot((name, _) => finished.contains(name))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   584
        val results1 = results.filterNot((name, _) => finished.contains(name))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   585
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   586
        (new State(processes1, results1), finished)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   587
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   588
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   589
      def cancel(cancelled: List[String]): State = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   590
        for (name <- cancelled) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   591
          val process = processes(name)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   592
          if (process.is_finished) process.join.interrupt()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   593
          else process.cancel()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   594
        }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   595
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   596
        new State(processes.filterNot((name, _) => cancelled.contains(name)), results)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   597
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   598
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   599
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   600
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   601
  class Runner(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   602
    store: Store,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   603
    build_hosts: List[Build_Cluster.Host],
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   604
    isabelle_repository: Mercurial.Repository,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   605
    sync_dirs: List[Sync.Dir],
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   606
    progress: Progress
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   607
  ) extends Loop_Process[Runner.State]("Runner", store, progress) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   608
    val rsync_context = Rsync.Context()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   609
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   610
    private def sync(repository: Mercurial.Repository, rev: String, target: Path): String = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   611
      repository.pull()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   612
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   613
      if (rev.nonEmpty) repository.sync(rsync_context, target, rev = rev)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   614
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   615
      Exn.capture(repository.id(File.read(target + Mercurial.Hg_Sync.PATH_ID))) match {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   616
        case Exn.Res(res) => res
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   617
        case Exn.Exn(exn) => ""
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   618
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   619
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   620
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   621
    private def start_next(): Option[(Build_Config, Job)] =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   622
      synchronized_database("start_job") {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   623
        _state.next.headOption.flatMap { task =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   624
          progress.echo("Initializing " + task.name)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   625
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   626
          _state = _state.remove_pending(task.name)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   627
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   628
          val context = Context(store, task, build_hosts)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   629
          val number = _state.next_number(task.kind)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   630
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   631
          Exn.capture {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   632
            val isabelle_rev =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   633
              sync(isabelle_repository, task.isabelle_rev, context.isabelle_dir)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   634
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   635
            val components =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   636
              for (component <- task.components)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   637
              yield sync_dirs.find(_.name == component.name) match {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   638
                case Some(sync_dir) =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   639
                  val target = context.isabelle_dir + sync_dir.target
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   640
                  component.copy(rev = sync(sync_dir.hg, component.rev, target))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   641
                case None =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   642
                  if (component.rev.isEmpty) component
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   643
                  else error("Unknown component " + component)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   644
              }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   645
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   646
            Job(task.id, task.kind, number, isabelle_rev, components)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   647
          } match {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   648
            case Exn.Res(job) =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   649
              _state = _state.add_running(job)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   650
              val context1 = context.move(Context(store, job))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   651
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   652
              val msg = "Starting " + job.name
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   653
              echo(msg + " (id " + job.id + ")")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   654
              context1.progress.echo(msg)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   655
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   656
              Some(task.build_config, job)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   657
            case Exn.Exn(exn) =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   658
              val result = Result(task.kind, number, Status.aborted)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   659
              val context1 = Context(store, result)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   660
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   661
              val msg = "Failed to start job: " + exn.getMessage
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   662
              echo_error_message(msg)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   663
              context1.progress.echo_error_message(msg)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   664
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   665
              context.remove()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   666
              _state = _state.add_finished(result)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   667
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   668
              None
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   669
          }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   670
        }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   671
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   672
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   673
    private def stop_cancelled(state: Runner.State): Runner.State =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   674
      synchronized_database("stop_cancelled") {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   675
        val cancelled = for (name <- state.running if _state.running(name).cancelled) yield name
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   676
        state.cancel(cancelled)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   677
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   678
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   679
    private def finish_job(name: String, process_result: Process_Result): Unit =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   680
      synchronized_database("finish_job") {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   681
        val job = _state.running(name)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   682
        val context = Context(store, job, build_hosts)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   683
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   684
        val result = Result(job.kind, job.number, Status.from_result(process_result), Some(job.id))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   685
        context.copy_results(Context(store, result))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   686
        context.remove()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   687
        echo("Finished job " + job.id + " with status code " + process_result.rc)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   688
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   689
        _state = _state
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   690
          .remove_running(job.name)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   691
          .add_finished(result)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   692
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   693
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   694
    override def stopped(state: Runner.State): Boolean = progress.stopped && state.is_empty
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   695
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   696
    def init: Runner.State = Runner.State.empty
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   697
    def loop_body(state: Runner.State): Runner.State = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   698
      if (state.is_empty && !progress.stopped) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   699
        start_next() match {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   700
          case None => state
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   701
          case Some((build_config, job)) =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   702
            state.init(build_config, job, Context(store, job, build_hosts))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   703
        }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   704
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   705
      else {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   706
        val (state1, results) = stop_cancelled(state).update
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   707
        results.foreach(finish_job)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   708
        state1
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   709
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   710
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   711
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   712
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   713
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   714
  /* repository poller */
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   715
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   716
  object Poller {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   717
    case class State(ids: List[String], next: Future[List[String]])
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   718
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   719
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   720
  class Poller(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   721
    ci_jobs: List[String],
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   722
    store: Store,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   723
    isabelle_repository: Mercurial.Repository,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   724
    sync_dirs: List[Sync.Dir],
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   725
    progress: Progress
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   726
  ) extends Loop_Process[Poller.State]("Poller", store, progress) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   727
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   728
    override def delay = options.seconds("build_manager_poll_delay")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   729
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   730
    private def ids: List[String] =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   731
      isabelle_repository.id("default") :: sync_dirs.map(_.hg.id("default"))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   732
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   733
    private def poll: Future[List[String]] = Future.fork {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   734
      Par_List.map((repo: Mercurial.Repository) => repo.pull(),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   735
        isabelle_repository :: sync_dirs.map(_.hg))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   736
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   737
      ids
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   738
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   739
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   740
    val init: Poller.State = Poller.State(ids, poll)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   741
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   742
    def ci_task(name: String): Task =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   743
      Task(CI_Build(name, sync_dirs.map(dir => Component(dir.name, "default"))),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   744
        priority = Priority.low, isabelle_rev = "default")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   745
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   746
    private def add_task(): Unit = synchronized_database("add_task") {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   747
      for (name <- ci_jobs if !_state.pending.values.exists(_.kind == name)) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   748
        _state = _state.add_pending(ci_task(name))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   749
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   750
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   751
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   752
    def loop_body(state: Poller.State): Poller.State =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   753
      if (!state.next.is_finished) state
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   754
      else {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   755
        state.next.join_result match {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   756
          case Exn.Exn(exn) =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   757
            echo_error_message("Could not reach repository: " + exn.getMessage)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   758
            Poller.State(state.ids, poll)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   759
          case Exn.Res(ids1) =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   760
            if (state.ids != ids1) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   761
              echo("Found new revisions: " + ids1)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   762
              add_task()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   763
            }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   764
            Poller.State(ids1, poll)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   765
        }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   766
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   767
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   768
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   769
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   770
  /* web server */
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   771
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   772
  object Web_Server {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   773
    object Page {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   774
      val HOME = Path.basic("home")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   775
      val OVERVIEW = Path.basic("overview")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   776
      val BUILD = Path.basic("build")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   777
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   778
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   779
    object API {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   780
      val BUILD_CANCEL = Path.explode("api/build/cancel")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   781
      val CSS = Path.explode("api/isabelle.css")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   782
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   783
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   784
    object Cache {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   785
      def empty: Cache = new Cache()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   786
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   787
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   788
    class Cache private(keep: Time = Time.minutes(1)) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   789
      var logs: Map[String, (Time, String)] = Map.empty
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   790
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   791
      def update(store: Store, state: State): Unit = synchronized {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   792
        logs =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   793
          for {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   794
            (name, (time, log)) <- logs
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   795
            if time + keep > Time.now()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   796
          } yield name -> (time, Context(store, state.get(name).get).log)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   797
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   798
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   799
      def lookup(store: Store, elem: T): String = synchronized {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   800
        logs.get(elem.name) match {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   801
          case Some((_, log)) =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   802
            logs += elem.name -> (Time.now(), log)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   803
          case None =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   804
            logs += elem.name -> (Time.now(), Context(store, elem).log)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   805
        }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   806
        logs(elem.name)._2
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   807
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   808
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   809
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   810
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   811
  class Web_Server(port: Int, paths: Web_App.Paths, store: Store, progress: Progress)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   812
    extends Loop_Process[Unit]("Web_Server", store, progress) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   813
    import Web_App.*
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   814
    import Web_Server.*
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   815
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   816
    val cache = Cache.empty
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   817
    val Id = new Properties.String(Markup.ID)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   818
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   819
    enum Model {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   820
      case Error extends Model
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   821
      case Cancelled extends Model
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   822
      case Home(state: State) extends Model
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   823
      case Overview(kind: String, state: State) extends Model
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   824
      case Build(elem: T, state: State, public: Boolean = true) extends Model
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   825
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   826
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   827
    object View {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   828
      import HTML.*
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   829
      import More_HTML.*
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   830
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   831
      def render_if(cond: Boolean, body: XML.Body): XML.Body = if (cond) body else Nil
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   832
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   833
      def frontend_link(url: Url, xml: XML.Body): XML.Elem =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   834
        link(url.toString, xml) + ("target" -> "_parent")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   835
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   836
      def link_kind(kind: String): XML.Elem =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   837
        frontend_link(paths.frontend_url(Page.OVERVIEW, Markup.Kind(kind)), text(kind))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   838
      def link_build(name: String, number: Long): XML.Elem =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   839
        frontend_link(paths.frontend_url(Page.BUILD, Markup.Name(name)), text("#" + number))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   840
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   841
      def render_home(state: State): XML.Body = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   842
        def render_kind(kind: String): XML.Elem = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   843
          val running = state.get_running(kind).sortBy(_.number).reverse
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   844
          val finished = state.get_finished(kind).sortBy(_.number).reverse
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   845
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   846
          def render_previous(finished: List[Result]): XML.Body = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   847
            val (failed, rest) = finished.span(_.status != Status.ok)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   848
            val first_failed = failed.lastOption.map(result =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   849
              par(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   850
                text("first failure: ") :::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   851
                link_build(result.name, result.number) ::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   852
                text(" on " + result.date)))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   853
            val last_success = rest.headOption.map(result =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   854
              par(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   855
                text("last success: ") ::: link_build(result.name, result.number) ::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   856
                text(" on " + result.date)))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   857
            first_failed.toList ::: last_success.toList
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   858
          }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   859
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   860
          def render_job(job: Job): XML.Body =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   861
            par(link_build(job.name, job.number) :: text(": running since " + job.start_date)) ::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   862
            render_if(finished.headOption.exists(_.status != Status.ok), render_previous(finished))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   863
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   864
          def render_result(result: Result): XML.Body =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   865
            par(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   866
              link_build(result.name, result.number) ::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   867
              text(" (" + result.status.toString + ") on " + result.date)) ::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   868
            render_if(result.status != Status.ok, render_previous(finished.tail))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   869
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   870
          fieldset(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   871
            XML.elem("legend", List(link_kind(kind))) ::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   872
            (if (running.nonEmpty) render_job(running.head)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   873
            else if (finished.nonEmpty) render_result(finished.head)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   874
            else Nil))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   875
        }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   876
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   877
        chapter("Dashboard") ::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   878
          text("Queue: " + state.pending.size + " tasks waiting") :::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   879
          section("Builds") :: text("Total: " + state.num_builds + " builds") :::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   880
          state.kinds.map(render_kind)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   881
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   882
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   883
      def render_overview(kind: String, state: State): XML.Body = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   884
        def render_job(job: Job): XML.Body =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   885
          List(par(link_build(job.name, job.number) :: text(" running since " + job.start_date)))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   886
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   887
        def render_result(result: Result): XML.Body =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   888
          List(par(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   889
            link_build(result.name, result.number) ::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   890
            text(" (" + result.status + ") on " + result.date)))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   891
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   892
        chapter(kind) ::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   893
          itemize(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   894
            state.get_running(kind).sortBy(_.number).reverse.map(render_job) :::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   895
            state.get_finished(kind).sortBy(_.number).reverse.map(render_result)) :: Nil
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   896
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   897
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   898
      private val ID = Params.key(Markup.ID)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   899
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   900
      def render_build(elem: T, state: State, public: Boolean): XML.Body = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   901
        def render_cancel(id: UUID.T): XML.Body =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   902
          render_if(!public, List(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   903
            submit_form("", List(hidden(ID, id.toString),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   904
              api_button(paths.api_route(API.BUILD_CANCEL), "cancel build")))))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   905
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   906
        def render_rev(isabelle_rev: String, components: List[Component]): XML.Body =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   907
          for {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   908
            component <- Component("Isabelle", isabelle_rev) :: components
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   909
            if component.rev.nonEmpty
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   910
          } yield par(text(component.toString))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   911
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   912
        chapter("Build " + elem.name) :: (elem match {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   913
          case task: Task =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   914
            par(text("Task from " + task.submit_date + ". ")) ::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   915
            render_rev(task.isabelle_rev, task.components) :::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   916
            render_cancel(task.id)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   917
          case job: Job =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   918
            par(text("Start: " + job.start_date)) ::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   919
            par(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   920
              if (job.cancelled) text("Cancelling...")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   921
              else text("Running...") ::: render_cancel(job.id)) ::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   922
            render_rev(job.isabelle_rev, job.components) :::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   923
            source(cache.lookup(store, job)) :: Nil
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   924
          case result: Result =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   925
            par(text("Date: " + result.date)) ::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   926
            par(text("Status: " + result.status)) ::
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   927
            source(cache.lookup(store, result)) :: Nil
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   928
        })
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   929
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   930
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   931
      def render_cancelled: XML.Body =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   932
        List(chapter("Build Cancelled"), frontend_link(paths.frontend_url(Page.HOME), text("Home")))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   933
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   934
      def parse_id(params: Params.Data): Option[UUID.T] =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   935
        for {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   936
          id <- params.get(ID)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   937
          uuid <- UUID.unapply(id)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   938
        } yield uuid
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   939
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   940
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   941
    private val server = new Server[Model](paths, port, progress = progress) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   942
      /* control */
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   943
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   944
      def overview: Some[Model.Home] = Some(Model.Home(_state))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   945
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   946
      def get_overview(props: Properties.T): Option[Model.Overview] =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   947
        props match {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   948
          case Markup.Kind(kind) => Some(Model.Overview(kind, _state))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   949
          case _ => None
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   950
        }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   951
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   952
      def get_build(props: Properties.T): Option[Model.Build] =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   953
        props match {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   954
          case Markup.Name(name) =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   955
            val state = _state
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   956
            state.get(name).map(Model.Build(_, state))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   957
          case Id(UUID(id)) =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   958
            val state = _state
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   959
            state.get(id).map(Model.Build(_, state, public = false))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   960
          case _ => None
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   961
        }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   962
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   963
      def cancel_build(params: Params.Data): Option[Model] =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   964
        for {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   965
          id <- View.parse_id(params)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   966
          model <-
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   967
            synchronized_database("cancel_build") {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   968
              _state.get(id).map {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   969
                case task: Task =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   970
                  _state = _state.remove_pending(task.name)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   971
                  Model.Cancelled
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   972
                case job: Job =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   973
                  val job1 = job.copy(cancelled = true)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   974
                  _state = _state
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   975
                    .remove_running(job.name)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   976
                    .add_running(job1)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   977
                  Model.Build(job1, _state, public = false)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   978
                case result: Result => Model.Build(result, _state, public = false)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   979
              }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   980
            }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   981
        } yield model
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   982
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   983
      def render(model: Model): XML.Body =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   984
        HTML.title("Isabelle Build Manager") :: (
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   985
          model match {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   986
            case Model.Error => HTML.text("invalid request")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   987
            case Model.Home(state) => View.render_home(state)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   988
            case Model.Overview(kind, state) => View.render_overview(kind, state)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   989
            case Model.Build(elem, state, public) => View.render_build(elem, state, public)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   990
            case Model.Cancelled => View.render_cancelled
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   991
          })
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   992
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   993
      val error_model: Model = Model.Error
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   994
      val endpoints = List(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   995
        Get(Page.HOME, "home", _ => overview),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   996
        Get(Page.OVERVIEW, "overview", get_overview),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   997
        Get(Page.BUILD, "build", get_build),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   998
        Post(API.BUILD_CANCEL, "cancel build", cancel_build),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
   999
        Get_File(API.CSS, "css", _ => Some(HTML.isabelle_css)))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1000
      val head = List(HTML.style_file(paths.api_route(API.CSS)))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1001
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1002
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1003
    def init: Unit = server.start()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1004
    def loop_body(u: Unit): Unit = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1005
      if (progress.stopped) server.stop()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1006
      else synchronized_database("iterate") { cache.update(store, _state) }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1007
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1008
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1009
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1010
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1011
  /* context */
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1012
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1013
  object Context {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1014
    def apply(store: Store, elem: T, build_hosts: List[Build_Cluster.Host] = Nil): Context =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1015
      new Context(store, store.dir(elem), build_hosts)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1016
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1017
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1018
  class Context private(store: Store, val dir: Path, val build_hosts: List[Build_Cluster.Host]) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1019
    def isabelle_dir: Path = dir + Path.basic("isabelle")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1020
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1021
    private val log_file = dir + Path.basic("log")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1022
    val progress = new File_Progress(log_file, verbose = true)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1023
    def log: String =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1024
      Exn.capture(File.read(log_file)) match {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1025
        case Exn.Exn(_) => ""
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1026
        case Exn.Res(res) => res
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1027
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1028
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1029
    def move(other: Context): Context = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1030
      Isabelle_System.make_directory(other.dir.dir)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1031
      Isabelle_System.move_file(dir, other.dir)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1032
      other
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1033
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1034
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1035
    def copy_results(other: Context): Context = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1036
      Isabelle_System.make_directory(other.dir)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1037
      Isabelle_System.copy_file(log_file, other.log_file)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1038
      other
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1039
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1040
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1041
    def remove(): Unit = Isabelle_System.rm_tree(dir)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1042
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1043
    lazy val ssh = store.open_ssh()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1044
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1045
    def process(build_config: Build_Config): Bash.Process = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1046
      val isabelle = Other_Isabelle(isabelle_dir, store.identifier, ssh, progress)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1047
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1048
      val init_components =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1049
        for {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1050
          dir <- build_config.components
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1051
          target = isabelle_dir + Sync.DIRS + Path.basic(dir.name)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1052
          if Components.is_component_dir(target)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1053
        } yield "init_component " + quote(target.absolute.implode)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1054
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1055
      isabelle.init(other_settings = isabelle.init_components() ::: init_components,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1056
        fresh = build_config.fresh_build, echo = true)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1057
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1058
      val cmd = build_config.command(build_hosts)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1059
      progress.echo("isabelle" + cmd)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1060
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1061
      val script = File.bash_path(Isabelle_Tool.exe(isabelle.isabelle_home)) + cmd
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1062
      ssh.bash_process(isabelle.bash_context(script), settings = false)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1063
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1064
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1065
    def run(process: Bash.Process): Process_Result = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1066
      val process_result =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1067
        process.result(progress_stdout = progress.echo(_), progress_stderr = progress.echo(_))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1068
      ssh.close()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1069
      process_result
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1070
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1071
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1072
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1073
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1074
  /* build manager store */
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1075
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1076
  case class Store(options: Options) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1077
    val base_dir = Path.explode(options.string("build_manager_dir"))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1078
    val identifier = options.string("build_manager_identifier")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1079
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1080
    def dir(elem: T): Path = base_dir + (
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1081
      elem match {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1082
        case task: Task => Path.make(List("pending", task.id.toString))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1083
        case job: Job => Path.make(List("running", job.kind, job.number.toString))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1084
        case result: Result => Path.make(List("finished", result.kind, result.number.toString))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1085
      })
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1086
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1087
    def open_ssh(): SSH.Session =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1088
      SSH.open_session(options,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1089
        host = options.string("build_manager_ssh_host"),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1090
        port = options.int("build_manager_ssh_port"),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1091
        user = options.string("build_manager_ssh_user"))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1092
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1093
    def open_database(server: SSH.Server = SSH.no_server): PostgreSQL.Database =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1094
      PostgreSQL.open_database_server(options, server = server,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1095
        user = options.string("build_manager_database_user"),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1096
        password = options.string("build_manager_database_password"),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1097
        database = options.string("build_manager_database_name"),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1098
        host = options.string("build_manager_database_host"),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1099
        port = options.int("build_manager_database_port"),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1100
        ssh_host = options.string("build_manager_database_ssh_host"),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1101
        ssh_port = options.int("build_manager_database_ssh_port"),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1102
        ssh_user = options.string("build_manager_database_ssh_user"))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1103
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1104
    def open_postgresql_server(): SSH.Server =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1105
      PostgreSQL.open_server(options,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1106
        host = options.string("build_manager_database_host"),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1107
        port = options.int("build_manager_database_port"),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1108
        ssh_host = options.string("build_manager_ssh_host"),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1109
        ssh_port = options.int("build_manager_ssh_port"),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1110
        ssh_user = options.string("build_manager_ssh_user"))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1111
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1112
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1113
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1114
  /* build manager */
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1115
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1116
  def build_manager(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1117
    build_hosts: List[Build_Cluster.Host],
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1118
    options: Options,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1119
    port: Int,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1120
    sync_dirs: List[Sync.Dir] = Nil,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1121
    progress: Progress = new Progress
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1122
  ): Unit = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1123
    val store = Store(options)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1124
    val isabelle_repository = Mercurial.self_repository()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1125
    val ci_jobs = space_explode(',', options.string("build_manager_ci_jobs"))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1126
    val url = Url(options.string("build_manager_address"))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1127
    val paths = Web_App.Paths(url, Path.current, true, Web_Server.Page.HOME)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1128
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1129
    using(store.open_database())(db =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1130
      Build_Manager.private_data.transaction_lock(db,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1131
        create = true, label = "Build_Manager.build_manager") {})
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1132
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1133
    val processes = List(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1134
      new Runner(store, build_hosts, isabelle_repository, sync_dirs, progress),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1135
      new Poller(ci_jobs, store, isabelle_repository, sync_dirs, progress),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1136
      new Web_Server(port, paths, store, progress))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1137
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1138
    val threads = processes.map(Isabelle_Thread.create(_))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1139
    POSIX_Interrupt.handler {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1140
      progress.stop()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1141
      processes.foreach(_.interrupt())
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1142
    } {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1143
      threads.foreach(_.start())
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1144
      threads.foreach(_.join())
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1145
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1146
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1147
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1148
  def build_task(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1149
    options: Options,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1150
    store: Store,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1151
    afp_root: Option[Path] = None,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1152
    base_sessions: List[String] = Nil,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1153
    presentation: Boolean = false,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1154
    requirements: Boolean = false,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1155
    exclude_session_groups: List[String] = Nil,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1156
    all_sessions: Boolean = false,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1157
    build_heap: Boolean = false,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1158
    clean_build: Boolean = false,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1159
    export_files: Boolean = false,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1160
    fresh_build: Boolean = false,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1161
    session_groups: List[String] = Nil,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1162
    sessions: List[String] = Nil,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1163
    prefs: List[Options.Spec] = Nil,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1164
    exclude_sessions: List[String] = Nil,
80250
8ae6f4e8cc2a allow explicit Isabelle rev in build task (e.g., for older Isabelle versions);
Fabian Huch <huch@in.tum.de>
parents: 80246
diff changeset
  1165
    rev: String = "",
80246
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1166
    progress: Progress = new Progress
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1167
  ): UUID.T = {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1168
    val id = UUID.random()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1169
    val afp_rev = if (afp_root.nonEmpty) Some("") else None
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1170
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1171
    val build_config = User_Build(afp_rev, prefs, requirements, all_sessions, base_sessions,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1172
      exclude_session_groups, exclude_sessions, session_groups, sessions, build_heap, clean_build,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1173
      export_files, fresh_build, presentation)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1174
    val task = Task(build_config, id, Date.now(), Priority.high)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1175
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1176
    val context = Context(store, task)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1177
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1178
    progress.interrupt_handler {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1179
      using(store.open_ssh()) { ssh =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1180
        val rsync_context = Rsync.Context(ssh = ssh, chmod = "g+rwx")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1181
        progress.echo("Transferring repositories...")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1182
        Sync.sync(store.options, rsync_context, context.isabelle_dir, preserve_jars = true,
80250
8ae6f4e8cc2a allow explicit Isabelle rev in build task (e.g., for older Isabelle versions);
Fabian Huch <huch@in.tum.de>
parents: 80246
diff changeset
  1183
          dirs = Sync.afp_dirs(afp_root), rev = rev)
80246
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1184
        ssh.execute("chmod g+rwx " + File.bash_path(context.dir))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1185
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1186
        if (progress.stopped) {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1187
          progress.echo("Cancelling submission...")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1188
          ssh.rm_tree(context.dir)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1189
        } else {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1190
          using(store.open_postgresql_server()) { server =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1191
            using(store.open_database(server = server)) { db =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1192
              Build_Manager.private_data.transaction_lock(db, label = "Build_Manager.build_task") {
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1193
                val old_state = Build_Manager.private_data.pull_state(db, State())
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1194
                val state = old_state.add_pending(task)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1195
                Build_Manager.private_data.push_state(db, old_state, state)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1196
              }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1197
            }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1198
          }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1199
          val address = options.string("build_manager_address") + "/build?id=" + task.id
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1200
          progress.echo("Submitted task. Private url: " + address)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1201
        }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1202
      }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1203
    }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1204
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1205
    id
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1206
  }
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1207
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1208
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1209
  /* Isabelle tool wrapper */
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1210
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1211
  private def show_options(relevant_options: List[String], options: Options): String =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1212
    cat_lines(relevant_options.flatMap(options.get).map(_.print))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1213
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1214
  private val notable_server_options =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1215
    List(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1216
      "build_manager_dir",
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1217
      "build_manager_address",
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1218
      "build_manager_ssh_host",
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1219
      "build_manager_ci_jobs")
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1220
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1221
  val isabelle_tool = Isabelle_Tool("build_manager", "run build manager", Scala_Project.here,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1222
    { args =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1223
      var afp_root: Option[Path] = None
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1224
      val dirs = new mutable.ListBuffer[Path]
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1225
      val build_hosts = new mutable.ListBuffer[Build_Cluster.Host]
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1226
      var options = Options.init()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1227
      var port = 8080
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1228
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1229
      val getopts = Getopts("""
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1230
Usage: isabelle build_manager [OPTIONS]
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1231
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1232
  Options are:
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1233
    -A ROOT      include AFP with given root directory (":" for """ + AFP.BASE.implode + """)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1234
    -D DIR       include extra component in given directory
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1235
    -H HOSTS     additional cluster host specifications of the form
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1236
                 NAMES:PARAMETERS (separated by commas)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1237
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1238
    -p PORT      explicit web server port
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1239
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1240
  Run Isabelle build manager. Notable system options:
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1241
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1242
""" + Library.indent_lines(2, show_options(notable_server_options, options)) + "\n",
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1243
        "A:" -> (arg => afp_root = Some(if (arg == ":") AFP.BASE else Path.explode(arg))),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1244
        "D:" -> (arg => dirs += Path.explode(arg)),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1245
        "H:" -> (arg => build_hosts ++= Build_Cluster.Host.parse(Registry.global, arg)),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1246
        "o:" -> (arg => options = options + arg),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1247
        "p:" -> (arg => port = Value.Int.parse(arg)))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1248
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1249
      val more_args = getopts(args)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1250
      if (more_args.nonEmpty) getopts.usage()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1251
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1252
      val progress = new Console_Progress()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1253
      val sync_dirs =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1254
        Sync.afp_dirs(afp_root) ::: dirs.toList.map(dir => Sync.Dir(dir.file_name, dir))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1255
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1256
      sync_dirs.foreach(_.check())
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1257
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1258
      build_manager(build_hosts = build_hosts.toList, options = options, port = port,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1259
        sync_dirs = sync_dirs, progress = progress)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1260
    })
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1261
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1262
  val isabelle_tool1 = Isabelle_Tool("build_task", "submit build task for build manager",
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1263
    Scala_Project.here,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1264
    { args =>
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1265
      var afp_root: Option[Path] = None
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1266
      val base_sessions = new mutable.ListBuffer[String]
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1267
      var presentation = false
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1268
      var requirements = false
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1269
      val exclude_session_groups = new mutable.ListBuffer[String]
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1270
      var all_sessions = false
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1271
      var build_heap = false
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1272
      var clean_build = false
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1273
      var export_files = false
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1274
      var fresh_build = false
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1275
      val session_groups = new mutable.ListBuffer[String]
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1276
      var options = Options.init(specs = Options.Spec.ISABELLE_BUILD_OPTIONS)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1277
      var prefs: List[Options.Spec] = Nil
80250
8ae6f4e8cc2a allow explicit Isabelle rev in build task (e.g., for older Isabelle versions);
Fabian Huch <huch@in.tum.de>
parents: 80246
diff changeset
  1278
      var rev = ""
80246
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1279
      val exclude_sessions = new mutable.ListBuffer[String]
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1280
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1281
      val getopts = Getopts("""
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1282
Usage: isabelle build_task [OPTIONS] [SESSIONS ...]
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1283
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1284
  Options are:
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1285
    -A ROOT      include AFP with given root directory (":" for """ + AFP.BASE.implode + """)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1286
    -B NAME      include session NAME and all descendants
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1287
    -P           enable HTML/PDF presentation
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1288
    -R           refer to requirements of selected sessions
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1289
    -X NAME      exclude sessions from group NAME and all descendants
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1290
    -a           select all sessions
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1291
    -b           build heap images
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1292
    -c           clean build
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1293
    -e           export files from session specification into file-system
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1294
    -f           fresh build
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1295
    -g NAME      select session group NAME
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1296
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1297
    -p OPTIONS   comma-separated preferences for build process
80250
8ae6f4e8cc2a allow explicit Isabelle rev in build task (e.g., for older Isabelle versions);
Fabian Huch <huch@in.tum.de>
parents: 80246
diff changeset
  1298
    -r REV       explicit revision (default: state of working directory)
80246
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1299
    -x NAME      exclude session NAME and all descendants
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1300
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1301
  Submit build task on SSH server. Notable system options:
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1302
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1303
""" + Library.indent_lines(2, show_options(List("build_manager_ssh_user"), options)) + "\n",
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1304
        "A:" -> (arg => afp_root = Some(if (arg == ":") AFP.BASE else Path.explode(arg))),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1305
        "B:" -> (arg => base_sessions += arg),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1306
        "P" -> (_ => presentation = true),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1307
        "R" -> (_ => requirements = true),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1308
        "X:" -> (arg => exclude_session_groups += arg),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1309
        "a" -> (_ => all_sessions = true),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1310
        "b" -> (_ => build_heap = true),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1311
        "c" -> (_ => clean_build = true),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1312
        "e" -> (_ => export_files = true),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1313
        "f" -> (_ => fresh_build = true),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1314
        "g:" -> (arg => session_groups += arg),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1315
        "o:" -> (arg => options = options + arg),
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1316
        "p:" -> (arg => prefs = Options.Spec.parse(arg)),
80250
8ae6f4e8cc2a allow explicit Isabelle rev in build task (e.g., for older Isabelle versions);
Fabian Huch <huch@in.tum.de>
parents: 80246
diff changeset
  1317
        "r:" -> (arg => rev = arg),
80246
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1318
        "x:" -> (arg => exclude_sessions += arg))
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1319
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1320
      val sessions = getopts(args)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1321
      val store = Store(options)
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1322
      val progress = new Console_Progress()
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1323
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1324
      build_task(options, store = store, afp_root = afp_root, base_sessions =
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1325
        base_sessions.toList, presentation = presentation, requirements = requirements,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1326
        exclude_session_groups = exclude_session_groups.toList, all_sessions = all_sessions,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1327
        build_heap = build_heap, clean_build = clean_build, export_files = export_files,
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1328
        fresh_build = fresh_build, session_groups = session_groups.toList, sessions = sessions,
80250
8ae6f4e8cc2a allow explicit Isabelle rev in build task (e.g., for older Isabelle versions);
Fabian Huch <huch@in.tum.de>
parents: 80246
diff changeset
  1329
        prefs = prefs, rev = rev, exclude_sessions = exclude_sessions.toList, progress = progress)
80246
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1330
    })
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1331
}
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1332
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1333
class Build_Manager_Tools extends Isabelle_Scala_Tools(
245dd5f82462 add build manager module;
Fabian Huch <huch@in.tum.de>
parents:
diff changeset
  1334
  Build_Manager.isabelle_tool, Build_Manager.isabelle_tool1)