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