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