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