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