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