src/Pure/Admin/isabelle_cronjob.scala
author wenzelm
Fri Oct 14 21:35:02 2016 +0200 (2016-10-14)
changeset 64215 123e6dcd3852
parent 64199 f38d39c57959
child 64218 f318cb6ba511
permissions -rw-r--r--
cronjob: build release from repository snapshot;
wenzelm@64148
     1
/*  Title:      Pure/Admin/isabelle_cronjob.scala
wenzelm@64148
     2
    Author:     Makarius
wenzelm@64148
     3
wenzelm@64148
     4
Main entry point for administrative cronjob at TUM.
wenzelm@64148
     5
*/
wenzelm@64148
     6
wenzelm@64148
     7
package isabelle
wenzelm@64148
     8
wenzelm@64148
     9
wenzelm@64170
    10
import scala.annotation.tailrec
wenzelm@64170
    11
import scala.collection.mutable
wenzelm@64170
    12
wenzelm@64170
    13
wenzelm@64148
    14
object Isabelle_Cronjob
wenzelm@64148
    15
{
wenzelm@64194
    16
  /* file-system state: owned by main cronjob */
wenzelm@64153
    17
wenzelm@64153
    18
  val main_dir = Path.explode("~/cronjob")
wenzelm@64194
    19
  val main_state_file = main_dir + Path.explode("run/main.state")
wenzelm@64194
    20
  val main_log = main_dir + Path.explode("log/main.log")  // owned by log service
wenzelm@64153
    21
wenzelm@64194
    22
  val isabelle_repos = main_dir + Path.explode("isabelle-build_history")
wenzelm@64194
    23
  val afp_repos = main_dir + Path.explode("AFP-build_history")
wenzelm@64153
    24
wenzelm@64215
    25
  val release_snapshot = main_dir + Path.explode("release_snapshot")
wenzelm@64215
    26
wenzelm@64153
    27
wenzelm@64192
    28
wenzelm@64192
    29
  /** particular tasks **/
wenzelm@64192
    30
wenzelm@64194
    31
  /* identify Isabelle + AFP repository snapshots */
wenzelm@64192
    32
wenzelm@64194
    33
  private val isabelle_identify =
wenzelm@64192
    34
    Logger_Task("isabelle_identify", logger =>
wenzelm@64192
    35
      {
wenzelm@64198
    36
        val isabelle_id = Mercurial.repository(isabelle_repos).pull_id()
wenzelm@64198
    37
        val afp_id = Mercurial.repository(afp_repos).pull_id()
wenzelm@64192
    38
wenzelm@64195
    39
        File.write(logger.log_dir + Build_Log.log_filename("isabelle_identify", logger.start_date),
wenzelm@64192
    40
          terminate_lines(
wenzelm@64192
    41
            List("isabelle_identify: " + Build_Log.print_date(logger.start_date),
wenzelm@64192
    42
              "",
wenzelm@64192
    43
              "Isabelle version: " + isabelle_id,
wenzelm@64192
    44
              "AFP version: " + afp_id)))
wenzelm@64192
    45
      })
wenzelm@64192
    46
wenzelm@64192
    47
wenzelm@64194
    48
  /* integrity test of build_history vs. build_history_base */
wenzelm@64193
    49
wenzelm@64194
    50
  private val build_history_base =
wenzelm@64193
    51
    Logger_Task("build_history_base", logger =>
wenzelm@64193
    52
      {
wenzelm@64194
    53
        for {
wenzelm@64194
    54
          (result, log_path) <-
wenzelm@64194
    55
            Build_History.build_history(Mercurial.repository(isabelle_repos),
wenzelm@64194
    56
              rev = "build_history_base", fresh = true, build_args = List("FOL"))
wenzelm@64194
    57
        } {
wenzelm@64194
    58
          result.check
wenzelm@64195
    59
          File.copy(log_path, logger.log_dir + log_path.base)
wenzelm@64194
    60
        }
wenzelm@64193
    61
      })
wenzelm@64193
    62
wenzelm@64193
    63
wenzelm@64215
    64
  /* build release from repository snapshot */
wenzelm@64215
    65
wenzelm@64215
    66
  private val build_release =
wenzelm@64215
    67
    Logger_Task("build_release", logger =>
wenzelm@64215
    68
      Isabelle_System.with_tmp_dir("isadist")(tmp_dir =>
wenzelm@64215
    69
        {
wenzelm@64215
    70
          val base_dir = File.path(tmp_dir)
wenzelm@64215
    71
wenzelm@64215
    72
          val new_snapshot = release_snapshot.ext("new")
wenzelm@64215
    73
          val old_snapshot = release_snapshot.ext("old")
wenzelm@64215
    74
wenzelm@64215
    75
          Isabelle_System.rm_tree(new_snapshot)
wenzelm@64215
    76
          Isabelle_System.rm_tree(old_snapshot)
wenzelm@64215
    77
wenzelm@64215
    78
          Build_Release.build_release(base_dir, parallel_jobs = 4,
wenzelm@64215
    79
            remote_mac = "macbroy30", website = Some(new_snapshot))
wenzelm@64215
    80
wenzelm@64215
    81
          if (release_snapshot.is_dir) File.mv(release_snapshot, old_snapshot)
wenzelm@64215
    82
          File.mv(new_snapshot, release_snapshot)
wenzelm@64215
    83
          Isabelle_System.rm_tree(old_snapshot)
wenzelm@64215
    84
        }))
wenzelm@64215
    85
wenzelm@64215
    86
wenzelm@64192
    87
wenzelm@64192
    88
  /** task logging **/
wenzelm@64171
    89
wenzelm@64193
    90
  sealed case class Logger_Task(name: String = "", body: Logger => Unit)
wenzelm@64154
    91
wenzelm@64171
    92
  class Log_Service private[Isabelle_Cronjob](progress: Progress)
wenzelm@64171
    93
  {
wenzelm@64171
    94
    private val thread: Consumer_Thread[String] =
wenzelm@64171
    95
      Consumer_Thread.fork("cronjob: logger", daemon = true)(
wenzelm@64171
    96
        consume = (text: String) =>
wenzelm@64171
    97
          {
wenzelm@64171
    98
            File.append(main_log, text + "\n")   // critical
wenzelm@64171
    99
            progress.echo(text)
wenzelm@64171
   100
            true
wenzelm@64171
   101
          })
wenzelm@64171
   102
wenzelm@64171
   103
    def shutdown() { thread.shutdown() }
wenzelm@64171
   104
wenzelm@64171
   105
    val hostname = Isabelle_System.hostname()
wenzelm@64171
   106
wenzelm@64171
   107
    def log(date: Date, task_name: String, msg: String): Unit =
wenzelm@64193
   108
      if (task_name != "")
wenzelm@64193
   109
        thread.send(
wenzelm@64193
   110
          "[" + Build_Log.print_date(date) + ", " + hostname + ", " + task_name + "]: " + msg)
wenzelm@64171
   111
wenzelm@64171
   112
    def start_logger(start_date: Date, task_name: String): Logger =
wenzelm@64171
   113
      new Logger(this, start_date, task_name)
wenzelm@64154
   114
wenzelm@64171
   115
    def run_task(start_date: Date, task: Logger_Task)
wenzelm@64171
   116
    {
wenzelm@64171
   117
      val logger = start_logger(start_date, task.name)
wenzelm@64171
   118
      val res = Exn.capture { task.body(logger) }
wenzelm@64171
   119
      val end_date = Date.now()
wenzelm@64171
   120
      val err =
wenzelm@64171
   121
        res match {
wenzelm@64171
   122
          case Exn.Res(_) => None
wenzelm@64171
   123
          case Exn.Exn(exn) => Some(Exn.message(exn))
wenzelm@64171
   124
        }
wenzelm@64171
   125
      logger.log_end(end_date, err)
wenzelm@64171
   126
    }
wenzelm@64171
   127
wenzelm@64171
   128
    def fork_task(start_date: Date, task: Logger_Task): Task =
wenzelm@64171
   129
      new Task(task.name, run_task(start_date, task))
wenzelm@64171
   130
  }
wenzelm@64171
   131
wenzelm@64171
   132
  class Logger private[Isabelle_Cronjob](
wenzelm@64171
   133
    val log_service: Log_Service, val start_date: Date, val task_name: String)
wenzelm@64162
   134
  {
wenzelm@64171
   135
    def log(date: Date, msg: String): Unit = log_service.log(date, task_name, msg)
wenzelm@64171
   136
wenzelm@64171
   137
    def log_end(end_date: Date, err: Option[String])
wenzelm@64171
   138
    {
wenzelm@64171
   139
      val elapsed_time = end_date.time - start_date.time
wenzelm@64171
   140
      val msg =
wenzelm@64171
   141
        (if (err.isEmpty) "finished" else "ERROR " + err.get) +
wenzelm@64197
   142
        (if (elapsed_time.seconds < 3.0) "" else " (" + elapsed_time.message_hms + " elapsed time)")
wenzelm@64171
   143
      log(end_date, msg)
wenzelm@64171
   144
    }
wenzelm@64171
   145
wenzelm@64195
   146
    val log_dir: Path = main_dir + Build_Log.log_subdir(start_date)
wenzelm@64195
   147
wenzelm@64195
   148
    Isabelle_System.mkdirs(log_dir)
wenzelm@64171
   149
    log(start_date, "started")
wenzelm@64171
   150
  }
wenzelm@64171
   151
wenzelm@64171
   152
  class Task private[Isabelle_Cronjob](name: String, body: => Unit)
wenzelm@64171
   153
  {
wenzelm@64171
   154
    private val future: Future[Unit] = Future.thread("cronjob: " + name) { body }
wenzelm@64171
   155
    def is_finished: Boolean = future.is_finished
wenzelm@64162
   156
  }
wenzelm@64153
   157
wenzelm@64170
   158
wenzelm@64170
   159
wenzelm@64153
   160
  /** cronjob **/
wenzelm@64153
   161
wenzelm@64186
   162
  def init_options(): Options = Options.load(Path.explode("~~/Admin/cronjob/cronjob.options"))
wenzelm@64186
   163
wenzelm@64187
   164
  def cronjob(progress: Progress, exclude_task: Set[String])
wenzelm@64153
   165
  {
wenzelm@64171
   166
    /* soft lock */
wenzelm@64153
   167
wenzelm@64153
   168
    val still_running =
wenzelm@64153
   169
      try { Some(File.read(main_state_file)) }
wenzelm@64153
   170
      catch { case ERROR(_) => None }
wenzelm@64153
   171
wenzelm@64153
   172
    still_running match {
wenzelm@64170
   173
      case None | Some("") =>
wenzelm@64153
   174
      case Some(running) =>
wenzelm@64153
   175
        error("Isabelle cronjob appears to be still running: " + running)
wenzelm@64153
   176
    }
wenzelm@64153
   177
wenzelm@64199
   178
wenzelm@64199
   179
    /* log service */
wenzelm@64199
   180
wenzelm@64171
   181
    val log_service = new Log_Service(progress)
wenzelm@64154
   182
wenzelm@64199
   183
    def run(start_date: Date, task: Logger_Task) { log_service.run_task(start_date, task) }
wenzelm@64199
   184
wenzelm@64199
   185
    def run_now(task: Logger_Task) { run(Date.now(), task) }
wenzelm@64154
   186
wenzelm@64154
   187
wenzelm@64199
   188
    /* structured tasks */
wenzelm@64184
   189
wenzelm@64199
   190
    def SEQ(tasks: Logger_Task*): Logger_Task = Logger_Task(body = _ =>
wenzelm@64193
   191
      for (task <- tasks.iterator if !exclude_task(task.name) || task.name == "")
wenzelm@64199
   192
        run_now(task))
wenzelm@64153
   193
wenzelm@64199
   194
    def PAR(tasks: Logger_Task*): Logger_Task = Logger_Task(body = _ =>
wenzelm@64170
   195
      {
wenzelm@64199
   196
        @tailrec def join(running: List[Task])
wenzelm@64199
   197
        {
wenzelm@64199
   198
          running.partition(_.is_finished) match {
wenzelm@64199
   199
            case (Nil, Nil) =>
wenzelm@64199
   200
            case (Nil, _ :: _) => Thread.sleep(500); join(running)
wenzelm@64199
   201
            case (_ :: _, remaining) => join(remaining)
wenzelm@64199
   202
          }
wenzelm@64170
   203
        }
wenzelm@64199
   204
        val start_date = Date.now()
wenzelm@64199
   205
        val running =
wenzelm@64199
   206
          for (task <- tasks.toList if !exclude_task(task.name))
wenzelm@64199
   207
            yield log_service.fork_task(start_date, task)
wenzelm@64199
   208
        join(running)
wenzelm@64199
   209
      })
wenzelm@64193
   210
wenzelm@64170
   211
wenzelm@64170
   212
    /* main */
wenzelm@64170
   213
wenzelm@64199
   214
    val main_start_date = Date.now()
wenzelm@64199
   215
    File.write(main_state_file, main_start_date + " " + log_service.hostname)
wenzelm@64199
   216
wenzelm@64193
   217
    run(main_start_date,
wenzelm@64193
   218
      Logger_Task("isabelle_cronjob", _ =>
wenzelm@64215
   219
        run_now(SEQ(isabelle_identify, build_history_base, build_release))))
wenzelm@64153
   220
wenzelm@64171
   221
    log_service.shutdown()
wenzelm@64170
   222
wenzelm@64153
   223
    main_state_file.file.delete
wenzelm@64153
   224
  }
wenzelm@64153
   225
wenzelm@64153
   226
wenzelm@64153
   227
wenzelm@64153
   228
  /** command line entry point **/
wenzelm@64153
   229
wenzelm@64148
   230
  def main(args: Array[String])
wenzelm@64148
   231
  {
wenzelm@64148
   232
    Command_Line.tool0 {
wenzelm@64148
   233
      var force = false
wenzelm@64148
   234
      var verbose = false
wenzelm@64187
   235
      var exclude_task = Set.empty[String]
wenzelm@64148
   236
wenzelm@64148
   237
      val getopts = Getopts("""
wenzelm@64148
   238
Usage: Admin/cronjob/main [OPTIONS]
wenzelm@64148
   239
wenzelm@64148
   240
  Options are:
wenzelm@64148
   241
    -f           apply force to do anything
wenzelm@64148
   242
    -v           verbose
wenzelm@64187
   243
    -x NAME      exclude tasks with this name
wenzelm@64148
   244
""",
wenzelm@64148
   245
        "f" -> (_ => force = true),
wenzelm@64187
   246
        "v" -> (_ => verbose = true),
wenzelm@64187
   247
        "x:" -> (arg => exclude_task += arg))
wenzelm@64148
   248
wenzelm@64148
   249
      val more_args = getopts(args)
wenzelm@64148
   250
      if (more_args.nonEmpty) getopts.usage()
wenzelm@64148
   251
wenzelm@64153
   252
      val progress = if (verbose) new Console_Progress() else Ignore_Progress
wenzelm@64148
   253
wenzelm@64187
   254
      if (force) cronjob(progress, exclude_task)
wenzelm@64153
   255
      else error("Need to apply force to do anything")
wenzelm@64148
   256
    }
wenzelm@64148
   257
  }
wenzelm@64148
   258
}