src/Pure/Admin/isabelle_cronjob.scala
author wenzelm
Tue Oct 25 14:06:43 2016 +0200 (2016-10-25)
changeset 64397 6e9c22c494c5
parent 64351 e54578e699b5
child 64399 c46e26512e0f
permissions -rw-r--r--
more informative error (stderr);
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@64219
    20
  val current_log = main_dir + Path.explode("run/main.log")  // owned by log service
wenzelm@64219
    21
  val cumulative_log = main_dir + Path.explode("log/main.log")  // owned by log service
wenzelm@64153
    22
wenzelm@64231
    23
  val isabelle_repos = main_dir + Path.explode("isabelle")
wenzelm@64231
    24
  val isabelle_repos_test = main_dir + Path.explode("isabelle-test")
wenzelm@64231
    25
  val afp_repos = main_dir + Path.explode("AFP")
wenzelm@64153
    26
wenzelm@64236
    27
  val isabelle_dev_source = "http://isabelle.in.tum.de/repos/isabelle"
wenzelm@64236
    28
  val isabelle_release_source = "http://bitbucket.org/isabelle_project/isabelle-release"
wenzelm@64236
    29
  val afp_source = "https://bitbucket.org/isa-afp/afp-devel"
wenzelm@64236
    30
wenzelm@64218
    31
  val release_snapshot = Path.explode("~/html-data/release_snapshot")
wenzelm@64215
    32
wenzelm@64153
    33
wenzelm@64192
    34
wenzelm@64192
    35
  /** particular tasks **/
wenzelm@64192
    36
wenzelm@64194
    37
  /* identify Isabelle + AFP repository snapshots */
wenzelm@64192
    38
wenzelm@64194
    39
  private val isabelle_identify =
wenzelm@64192
    40
    Logger_Task("isabelle_identify", logger =>
wenzelm@64192
    41
      {
wenzelm@64232
    42
        val isabelle_id = Mercurial.repository(isabelle_repos).id()
wenzelm@64252
    43
        val afp_id = Mercurial.setup_repository(afp_source, afp_repos).id()
wenzelm@64192
    44
wenzelm@64195
    45
        File.write(logger.log_dir + Build_Log.log_filename("isabelle_identify", logger.start_date),
wenzelm@64192
    46
          terminate_lines(
wenzelm@64192
    47
            List("isabelle_identify: " + Build_Log.print_date(logger.start_date),
wenzelm@64192
    48
              "",
wenzelm@64192
    49
              "Isabelle version: " + isabelle_id,
wenzelm@64192
    50
              "AFP version: " + afp_id)))
wenzelm@64192
    51
      })
wenzelm@64192
    52
wenzelm@64192
    53
wenzelm@64194
    54
  /* integrity test of build_history vs. build_history_base */
wenzelm@64193
    55
wenzelm@64194
    56
  private val build_history_base =
wenzelm@64193
    57
    Logger_Task("build_history_base", logger =>
wenzelm@64193
    58
      {
wenzelm@64251
    59
        val hg =
wenzelm@64251
    60
          Mercurial.setup_repository(
wenzelm@64251
    61
            File.standard_path(isabelle_repos), isabelle_repos_test)
wenzelm@64194
    62
        for {
wenzelm@64194
    63
          (result, log_path) <-
wenzelm@64251
    64
            Build_History.build_history(
wenzelm@64251
    65
              hg, rev = "build_history_base", fresh = true, build_args = List("HOL"))
wenzelm@64194
    66
        } {
wenzelm@64194
    67
          result.check
wenzelm@64195
    68
          File.copy(log_path, logger.log_dir + log_path.base)
wenzelm@64194
    69
        }
wenzelm@64193
    70
      })
wenzelm@64193
    71
wenzelm@64193
    72
wenzelm@64215
    73
  /* build release from repository snapshot */
wenzelm@64215
    74
wenzelm@64215
    75
  private val build_release =
wenzelm@64215
    76
    Logger_Task("build_release", logger =>
wenzelm@64220
    77
      Isabelle_System.with_tmp_dir("isadist")(base_dir =>
wenzelm@64215
    78
        {
wenzelm@64215
    79
          val new_snapshot = release_snapshot.ext("new")
wenzelm@64215
    80
          val old_snapshot = release_snapshot.ext("old")
wenzelm@64215
    81
wenzelm@64215
    82
          Isabelle_System.rm_tree(new_snapshot)
wenzelm@64215
    83
          Isabelle_System.rm_tree(old_snapshot)
wenzelm@64215
    84
wenzelm@64215
    85
          Build_Release.build_release(base_dir, parallel_jobs = 4,
wenzelm@64215
    86
            remote_mac = "macbroy30", website = Some(new_snapshot))
wenzelm@64215
    87
wenzelm@64215
    88
          if (release_snapshot.is_dir) File.mv(release_snapshot, old_snapshot)
wenzelm@64215
    89
          File.mv(new_snapshot, release_snapshot)
wenzelm@64215
    90
          Isabelle_System.rm_tree(old_snapshot)
wenzelm@64215
    91
        }))
wenzelm@64215
    92
wenzelm@64215
    93
wenzelm@64231
    94
  /* remote build_history */
wenzelm@64231
    95
wenzelm@64231
    96
  private sealed case class Remote_Build(
wenzelm@64231
    97
    host: String,
wenzelm@64231
    98
    user: String = "",
wenzelm@64231
    99
    port: Int = SSH.default_port,
wenzelm@64294
   100
    shared_home: Boolean = true,
wenzelm@64231
   101
    options: String = "",
wenzelm@64351
   102
    args: String = "")
wenzelm@64231
   103
wenzelm@64231
   104
  private val remote_builds =
wenzelm@64231
   105
    List(
wenzelm@64350
   106
      List(Remote_Build("lxbroy10", options = "-m32 -B -M1x4,2,4,6 -N", args = "-g timing")),
wenzelm@64351
   107
      List(
wenzelm@64351
   108
        Remote_Build("macbroy2", options = "-m32 -M8", args = "-a"),
wenzelm@64351
   109
        Remote_Build("macbroy2", options = "-m32 -M8 -t quick_and_dirty", args = "-a -o quick_and_dirty"),
wenzelm@64351
   110
        Remote_Build("macbroy2", options = "-m32 -M8 -t skip_proofs", args = "-a -o skip_proofs")),
wenzelm@64351
   111
      List(Remote_Build("macbroy30", options = "-m32 -M2", args = "-a")),
wenzelm@64351
   112
      List(Remote_Build("macbroy31", options = "-m32 -M2", args = "-a")),
wenzelm@64351
   113
      List(
wenzelm@64351
   114
        Remote_Build("vmnipkow9", shared_home = false, options = "-m32 -M4", args = "-a"),
wenzelm@64351
   115
        Remote_Build("vmnipkow9", shared_home = false, options = "-m64 -M4", args = "-a")))
wenzelm@64231
   116
wenzelm@64231
   117
  private def remote_build_history(rev: String, r: Remote_Build): Logger_Task =
wenzelm@64294
   118
  {
wenzelm@64294
   119
    val task_name = "build_history-" + r.host
wenzelm@64294
   120
    Logger_Task(task_name, logger =>
wenzelm@64231
   121
      {
wenzelm@64231
   122
        using(logger.ssh_context.open_session(host = r.host, user = r.user, port = r.port))(
wenzelm@64259
   123
          ssh =>
wenzelm@64231
   124
            {
wenzelm@64348
   125
              val self_update = !r.shared_home
wenzelm@64348
   126
              val push_isabelle_home = self_update && Mercurial.is_repository(Path.explode("~~"))
wenzelm@64348
   127
wenzelm@64346
   128
              def progress_result(log_name: String, bytes: Bytes): Unit =
wenzelm@64346
   129
                Bytes.write(logger.log_dir + Path.explode(log_name), bytes)
wenzelm@64346
   130
wenzelm@64346
   131
              Build_History.remote_build_history(ssh,
wenzelm@64346
   132
                isabelle_repos,
wenzelm@64346
   133
                isabelle_repos.ext(r.host),
wenzelm@64346
   134
                isabelle_repos_source = isabelle_dev_source,
wenzelm@64348
   135
                self_update = self_update,
wenzelm@64348
   136
                push_isabelle_home = push_isabelle_home,
wenzelm@64346
   137
                progress_result = progress_result _,
wenzelm@64346
   138
                options =
wenzelm@64346
   139
                  r.options + " -f -r " + Bash.string(rev) + " -N " + Bash.string(task_name),
wenzelm@64346
   140
                args = "-o timeout=10800 " + r.args)
wenzelm@64231
   141
            })
wenzelm@64231
   142
      })
wenzelm@64294
   143
  }
wenzelm@64231
   144
wenzelm@64231
   145
wenzelm@64192
   146
wenzelm@64192
   147
  /** task logging **/
wenzelm@64171
   148
wenzelm@64193
   149
  sealed case class Logger_Task(name: String = "", body: Logger => Unit)
wenzelm@64154
   150
wenzelm@64257
   151
  class Log_Service private[Isabelle_Cronjob](progress: Progress, val ssh_context: SSH.Context)
wenzelm@64171
   152
  {
wenzelm@64219
   153
    current_log.file.delete
wenzelm@64219
   154
wenzelm@64171
   155
    private val thread: Consumer_Thread[String] =
wenzelm@64171
   156
      Consumer_Thread.fork("cronjob: logger", daemon = true)(
wenzelm@64171
   157
        consume = (text: String) =>
wenzelm@64219
   158
          { // critical
wenzelm@64219
   159
            File.append(current_log, text + "\n")
wenzelm@64219
   160
            File.append(cumulative_log, text + "\n")
wenzelm@64171
   161
            progress.echo(text)
wenzelm@64171
   162
            true
wenzelm@64171
   163
          })
wenzelm@64171
   164
wenzelm@64171
   165
    def shutdown() { thread.shutdown() }
wenzelm@64171
   166
wenzelm@64171
   167
    val hostname = Isabelle_System.hostname()
wenzelm@64171
   168
wenzelm@64171
   169
    def log(date: Date, task_name: String, msg: String): Unit =
wenzelm@64193
   170
      if (task_name != "")
wenzelm@64193
   171
        thread.send(
wenzelm@64193
   172
          "[" + Build_Log.print_date(date) + ", " + hostname + ", " + task_name + "]: " + msg)
wenzelm@64171
   173
wenzelm@64171
   174
    def start_logger(start_date: Date, task_name: String): Logger =
wenzelm@64171
   175
      new Logger(this, start_date, task_name)
wenzelm@64154
   176
wenzelm@64171
   177
    def run_task(start_date: Date, task: Logger_Task)
wenzelm@64171
   178
    {
wenzelm@64171
   179
      val logger = start_logger(start_date, task.name)
wenzelm@64171
   180
      val res = Exn.capture { task.body(logger) }
wenzelm@64171
   181
      val end_date = Date.now()
wenzelm@64171
   182
      val err =
wenzelm@64171
   183
        res match {
wenzelm@64171
   184
          case Exn.Res(_) => None
wenzelm@64295
   185
          case Exn.Exn(exn) =>
wenzelm@64397
   186
            exn.printStackTrace()
wenzelm@64295
   187
            val first_line = Library.split_lines(Exn.message(exn)).headOption getOrElse "exception"
wenzelm@64295
   188
            Some(first_line)
wenzelm@64171
   189
        }
wenzelm@64171
   190
      logger.log_end(end_date, err)
wenzelm@64171
   191
    }
wenzelm@64171
   192
wenzelm@64171
   193
    def fork_task(start_date: Date, task: Logger_Task): Task =
wenzelm@64171
   194
      new Task(task.name, run_task(start_date, task))
wenzelm@64171
   195
  }
wenzelm@64171
   196
wenzelm@64171
   197
  class Logger private[Isabelle_Cronjob](
wenzelm@64171
   198
    val log_service: Log_Service, val start_date: Date, val task_name: String)
wenzelm@64162
   199
  {
wenzelm@64257
   200
    def ssh_context: SSH.Context = log_service.ssh_context
wenzelm@64231
   201
wenzelm@64171
   202
    def log(date: Date, msg: String): Unit = log_service.log(date, task_name, msg)
wenzelm@64171
   203
wenzelm@64171
   204
    def log_end(end_date: Date, err: Option[String])
wenzelm@64171
   205
    {
wenzelm@64171
   206
      val elapsed_time = end_date.time - start_date.time
wenzelm@64171
   207
      val msg =
wenzelm@64171
   208
        (if (err.isEmpty) "finished" else "ERROR " + err.get) +
wenzelm@64197
   209
        (if (elapsed_time.seconds < 3.0) "" else " (" + elapsed_time.message_hms + " elapsed time)")
wenzelm@64171
   210
      log(end_date, msg)
wenzelm@64171
   211
    }
wenzelm@64171
   212
wenzelm@64195
   213
    val log_dir: Path = main_dir + Build_Log.log_subdir(start_date)
wenzelm@64195
   214
wenzelm@64195
   215
    Isabelle_System.mkdirs(log_dir)
wenzelm@64171
   216
    log(start_date, "started")
wenzelm@64171
   217
  }
wenzelm@64171
   218
wenzelm@64171
   219
  class Task private[Isabelle_Cronjob](name: String, body: => Unit)
wenzelm@64171
   220
  {
wenzelm@64171
   221
    private val future: Future[Unit] = Future.thread("cronjob: " + name) { body }
wenzelm@64171
   222
    def is_finished: Boolean = future.is_finished
wenzelm@64162
   223
  }
wenzelm@64153
   224
wenzelm@64170
   225
wenzelm@64170
   226
wenzelm@64153
   227
  /** cronjob **/
wenzelm@64153
   228
wenzelm@64187
   229
  def cronjob(progress: Progress, exclude_task: Set[String])
wenzelm@64153
   230
  {
wenzelm@64171
   231
    /* soft lock */
wenzelm@64153
   232
wenzelm@64153
   233
    val still_running =
wenzelm@64153
   234
      try { Some(File.read(main_state_file)) }
wenzelm@64153
   235
      catch { case ERROR(_) => None }
wenzelm@64153
   236
wenzelm@64153
   237
    still_running match {
wenzelm@64170
   238
      case None | Some("") =>
wenzelm@64153
   239
      case Some(running) =>
wenzelm@64153
   240
        error("Isabelle cronjob appears to be still running: " + running)
wenzelm@64153
   241
    }
wenzelm@64153
   242
wenzelm@64199
   243
wenzelm@64199
   244
    /* log service */
wenzelm@64199
   245
wenzelm@64257
   246
    val log_service = new Log_Service(progress, SSH.init_context(Options.init()))
wenzelm@64154
   247
wenzelm@64199
   248
    def run(start_date: Date, task: Logger_Task) { log_service.run_task(start_date, task) }
wenzelm@64199
   249
wenzelm@64199
   250
    def run_now(task: Logger_Task) { run(Date.now(), task) }
wenzelm@64154
   251
wenzelm@64154
   252
wenzelm@64199
   253
    /* structured tasks */
wenzelm@64184
   254
wenzelm@64350
   255
    def SEQ(tasks: List[Logger_Task]): Logger_Task = Logger_Task(body = _ =>
wenzelm@64193
   256
      for (task <- tasks.iterator if !exclude_task(task.name) || task.name == "")
wenzelm@64199
   257
        run_now(task))
wenzelm@64153
   258
wenzelm@64350
   259
    def PAR(tasks: List[Logger_Task]): Logger_Task = Logger_Task(body = _ =>
wenzelm@64170
   260
      {
wenzelm@64199
   261
        @tailrec def join(running: List[Task])
wenzelm@64199
   262
        {
wenzelm@64199
   263
          running.partition(_.is_finished) match {
wenzelm@64199
   264
            case (Nil, Nil) =>
wenzelm@64199
   265
            case (Nil, _ :: _) => Thread.sleep(500); join(running)
wenzelm@64199
   266
            case (_ :: _, remaining) => join(remaining)
wenzelm@64199
   267
          }
wenzelm@64170
   268
        }
wenzelm@64199
   269
        val start_date = Date.now()
wenzelm@64199
   270
        val running =
wenzelm@64350
   271
          for (task <- tasks if !exclude_task(task.name))
wenzelm@64199
   272
            yield log_service.fork_task(start_date, task)
wenzelm@64199
   273
        join(running)
wenzelm@64199
   274
      })
wenzelm@64193
   275
wenzelm@64170
   276
wenzelm@64170
   277
    /* main */
wenzelm@64170
   278
wenzelm@64199
   279
    val main_start_date = Date.now()
wenzelm@64199
   280
    File.write(main_state_file, main_start_date + " " + log_service.hostname)
wenzelm@64199
   281
wenzelm@64232
   282
    val rev = Mercurial.repository(isabelle_repos).id()
wenzelm@64231
   283
wenzelm@64193
   284
    run(main_start_date,
wenzelm@64193
   285
      Logger_Task("isabelle_cronjob", _ =>
wenzelm@64231
   286
        run_now(
wenzelm@64350
   287
          SEQ(List(isabelle_identify, build_history_base, build_release,
wenzelm@64350
   288
            PAR(remote_builds.map(seq => SEQ(seq.map(remote_build_history(rev, _))))))))))
wenzelm@64153
   289
wenzelm@64171
   290
    log_service.shutdown()
wenzelm@64170
   291
wenzelm@64153
   292
    main_state_file.file.delete
wenzelm@64153
   293
  }
wenzelm@64153
   294
wenzelm@64153
   295
wenzelm@64153
   296
wenzelm@64153
   297
  /** command line entry point **/
wenzelm@64153
   298
wenzelm@64148
   299
  def main(args: Array[String])
wenzelm@64148
   300
  {
wenzelm@64148
   301
    Command_Line.tool0 {
wenzelm@64148
   302
      var force = false
wenzelm@64148
   303
      var verbose = false
wenzelm@64187
   304
      var exclude_task = Set.empty[String]
wenzelm@64148
   305
wenzelm@64148
   306
      val getopts = Getopts("""
wenzelm@64148
   307
Usage: Admin/cronjob/main [OPTIONS]
wenzelm@64148
   308
wenzelm@64148
   309
  Options are:
wenzelm@64148
   310
    -f           apply force to do anything
wenzelm@64148
   311
    -v           verbose
wenzelm@64187
   312
    -x NAME      exclude tasks with this name
wenzelm@64148
   313
""",
wenzelm@64148
   314
        "f" -> (_ => force = true),
wenzelm@64187
   315
        "v" -> (_ => verbose = true),
wenzelm@64187
   316
        "x:" -> (arg => exclude_task += arg))
wenzelm@64148
   317
wenzelm@64148
   318
      val more_args = getopts(args)
wenzelm@64148
   319
      if (more_args.nonEmpty) getopts.usage()
wenzelm@64148
   320
wenzelm@64153
   321
      val progress = if (verbose) new Console_Progress() else Ignore_Progress
wenzelm@64148
   322
wenzelm@64187
   323
      if (force) cronjob(progress, exclude_task)
wenzelm@64153
   324
      else error("Need to apply force to do anything")
wenzelm@64148
   325
    }
wenzelm@64148
   326
  }
wenzelm@64148
   327
}