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