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