src/Pure/Admin/isabelle_cronjob.scala
author wenzelm
Sat Oct 14 21:50:12 2017 +0200 (22 months ago)
changeset 66864 5cb8ccc46e3e
parent 66861 f6676691ef8a
child 66865 c8b18abf23e1
permissions -rw-r--r--
support for AFP versions;
added AFP tests: non-slow, two partitions;
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@64236
    26
wenzelm@65736
    27
  val jenkins_jobs = "identify" :: Jenkins.build_log_jobs
wenzelm@65666
    28
wenzelm@64153
    29
wenzelm@64192
    30
wenzelm@64192
    31
  /** particular tasks **/
wenzelm@64192
    32
wenzelm@64405
    33
  /* identify Isabelle + AFP repository snapshots and build release */
wenzelm@64192
    34
wenzelm@64405
    35
  private val build_release =
wenzelm@64405
    36
    Logger_Task("build_release", logger =>
wenzelm@66859
    37
      {
wenzelm@66859
    38
        Isabelle_Devel.make_index()
wenzelm@65771
    39
wenzelm@66859
    40
        val rev = Mercurial.repository(isabelle_repos).id()
wenzelm@66859
    41
        val afp_rev = Mercurial.setup_repository(AFP.repos_source, afp_repos).id()
wenzelm@64192
    42
wenzelm@66859
    43
        File.write(logger.log_dir + Build_Log.log_filename("isabelle_identify", logger.start_date),
wenzelm@66859
    44
          Build_Log.Identify.content(logger.start_date, Some(rev), Some(afp_rev)))
wenzelm@64405
    45
wenzelm@66859
    46
        Isabelle_Devel.release_snapshot(rev = rev, afp_rev = afp_rev,
wenzelm@66859
    47
          parallel_jobs = 4, remote_mac = "macbroy31")
wenzelm@66859
    48
      })
wenzelm@64192
    49
wenzelm@64192
    50
wenzelm@64194
    51
  /* integrity test of build_history vs. build_history_base */
wenzelm@64193
    52
wenzelm@64194
    53
  private val build_history_base =
wenzelm@64193
    54
    Logger_Task("build_history_base", logger =>
wenzelm@64193
    55
      {
wenzelm@64251
    56
        val hg =
wenzelm@64251
    57
          Mercurial.setup_repository(
wenzelm@64251
    58
            File.standard_path(isabelle_repos), isabelle_repos_test)
wenzelm@64194
    59
        for {
wenzelm@64194
    60
          (result, log_path) <-
wenzelm@66861
    61
            Build_History.build_history(logger.options, isabelle_repos_test,
wenzelm@66860
    62
              rev = "build_history_base", fresh = true, build_args = List("HOL"))
wenzelm@64194
    63
        } {
wenzelm@64194
    64
          result.check
wenzelm@64482
    65
          File.move(log_path, logger.log_dir + log_path.base)
wenzelm@64194
    66
        }
wenzelm@64193
    67
      })
wenzelm@64193
    68
wenzelm@64193
    69
wenzelm@64231
    70
  /* remote build_history */
wenzelm@64231
    71
wenzelm@66864
    72
  sealed case class Item(
wenzelm@66864
    73
    known: Boolean,
wenzelm@66864
    74
    isabelle_version: String,
wenzelm@66864
    75
    afp_version: Option[String],
wenzelm@66864
    76
    pull_date: Date)
wenzelm@65783
    77
  {
wenzelm@65783
    78
    def unknown: Boolean = !known
wenzelm@66864
    79
    def versions: (String, Option[String]) = (isabelle_version, afp_version)
wenzelm@66864
    80
wenzelm@66864
    81
    def known_versions(rev: String, afp_rev: Option[String]): Boolean =
wenzelm@66864
    82
      known && rev != "" && isabelle_version == rev &&
wenzelm@66864
    83
      (afp_rev.isEmpty || afp_rev.get != "" && afp_version == afp_rev.get)
wenzelm@65783
    84
  }
wenzelm@65783
    85
wenzelm@66864
    86
  def recent_items(db: SQL.Database,
wenzelm@66864
    87
    days: Int, rev: String, afp_rev: Option[String], sql: SQL.Source): List[Item] =
wenzelm@65807
    88
  {
wenzelm@65807
    89
    val select =
wenzelm@66864
    90
      Build_Log.Data.select_recent_versions(
wenzelm@66864
    91
        days = days, rev = rev, afp_rev = afp_rev, sql = "WHERE " + sql)
wenzelm@65807
    92
wenzelm@65807
    93
    db.using_statement(select)(stmt =>
wenzelm@65807
    94
      stmt.execute_query().iterator(res =>
wenzelm@65807
    95
      {
wenzelm@65807
    96
        val known = res.bool(Build_Log.Data.known)
wenzelm@65807
    97
        val isabelle_version = res.string(Build_Log.Prop.isabelle_version)
wenzelm@66864
    98
        val afp_version =
wenzelm@66864
    99
          if (afp_rev.isEmpty) None
wenzelm@66864
   100
          else proper_string(res.string(Build_Log.Prop.afp_version))
wenzelm@65807
   101
        val pull_date = res.date(Build_Log.Data.pull_date)
wenzelm@66864
   102
        Item(known, isabelle_version, afp_version, pull_date)
wenzelm@65807
   103
      }).toList)
wenzelm@65807
   104
  }
wenzelm@65807
   105
wenzelm@65807
   106
  def unknown_runs(items: List[Item]): List[List[Item]] =
wenzelm@65807
   107
  {
wenzelm@65807
   108
    val (run, rest) = Library.take_prefix[Item](_.unknown, items.dropWhile(_.known))
wenzelm@65807
   109
    if (run.nonEmpty) run :: unknown_runs(rest) else Nil
wenzelm@65807
   110
  }
wenzelm@65807
   111
wenzelm@64409
   112
  sealed case class Remote_Build(
wenzelm@65764
   113
    description: String,
wenzelm@64231
   114
    host: String,
wenzelm@64231
   115
    user: String = "",
wenzelm@65594
   116
    port: Int = 0,
wenzelm@64294
   117
    shared_home: Boolean = true,
wenzelm@65820
   118
    historic: Boolean = false,
wenzelm@65789
   119
    history: Int = 0,
wenzelm@65820
   120
    history_base: String = "build_history_base",
wenzelm@64231
   121
    options: String = "",
wenzelm@65732
   122
    args: String = "",
wenzelm@66864
   123
    afp: Boolean = false,
wenzelm@65732
   124
    detect: SQL.Source = "")
wenzelm@65732
   125
  {
wenzelm@65783
   126
    def sql: SQL.Source =
wenzelm@65783
   127
      Build_Log.Prop.build_engine + " = " + SQL.string(Build_History.engine) + " AND " +
wenzelm@65783
   128
      Build_Log.Prop.build_host + " = " + SQL.string(host) +
wenzelm@65783
   129
      (if (detect == "") "" else " AND " + SQL.enclose(detect))
wenzelm@65783
   130
wenzelm@65747
   131
    def profile: Build_Status.Profile =
wenzelm@65798
   132
      Build_Status.Profile(description, history, sql)
wenzelm@65783
   133
wenzelm@65820
   134
    def history_base_filter(hg: Mercurial.Repository): Set[String] =
wenzelm@65820
   135
    {
wenzelm@65820
   136
      val rev0 = hg.id(history_base)
wenzelm@65820
   137
      val graph = hg.graph()
wenzelm@65820
   138
      (rev0 :: graph.all_succs(List(rev0))).toSet
wenzelm@65820
   139
    }
wenzelm@65820
   140
wenzelm@66864
   141
    def pick(
wenzelm@66864
   142
      options: Options,
wenzelm@66864
   143
      rev: String = "",
wenzelm@66864
   144
      filter: String => Boolean = (_: String) => true): Option[(String, Option[String])] =
wenzelm@65747
   145
    {
wenzelm@66864
   146
      val afp_rev = if (afp) Some(Mercurial.repository(afp_repos).id()) else None
wenzelm@66864
   147
wenzelm@65783
   148
      val store = Build_Log.store(options)
wenzelm@65783
   149
      using(store.open_database())(db =>
wenzelm@65783
   150
      {
wenzelm@66864
   151
        def pick_days(days: Int, gap: Int): Option[(String, Option[String])] =
wenzelm@65810
   152
        {
wenzelm@65820
   153
          val items =
wenzelm@66864
   154
            recent_items(db, days, rev, afp_rev, sql).
wenzelm@65820
   155
              filter(item => filter(item.isabelle_version))
wenzelm@66007
   156
          def runs = unknown_runs(items).filter(run => run.length >= gap)
wenzelm@65783
   157
wenzelm@66864
   158
          if (historic || items.exists(_.known_versions(rev, afp_rev))) {
wenzelm@65810
   159
            val longest_run =
wenzelm@65810
   160
              (List.empty[Item] /: runs)({ case (item1, item2) =>
wenzelm@65810
   161
                if (item1.length >= item2.length) item1 else item2
wenzelm@65810
   162
              })
wenzelm@65810
   163
            if (longest_run.isEmpty) None
wenzelm@66864
   164
            else Some(longest_run(longest_run.length / 2).versions)
wenzelm@65810
   165
          }
wenzelm@66864
   166
          else if (rev != "") Some((rev, afp_rev))
wenzelm@66864
   167
          else runs.flatten.headOption.map(_.versions)
wenzelm@65783
   168
        }
wenzelm@65810
   169
wenzelm@66007
   170
        pick_days(options.int("build_log_history") max history, 2) orElse
wenzelm@66007
   171
        pick_days(200, 5) orElse
wenzelm@66007
   172
        pick_days(2000, 1)
wenzelm@65783
   173
      })
wenzelm@65747
   174
    }
wenzelm@65732
   175
  }
wenzelm@64231
   176
wenzelm@65806
   177
  val remote_builds_old: List[Remote_Build] =
wenzelm@65806
   178
    List(
wenzelm@66762
   179
      Remote_Build("Poly/ML 5.7 Linux", "lxbroy8",
wenzelm@66762
   180
        history_base = "37074e22e8be",
wenzelm@66762
   181
        options = "-m32 -B -M1x2,2 -t polyml-5.7 -i 'init_component /home/isabelle/contrib/polyml-5.7'",
wenzelm@66762
   182
        args = "-N -g timing",
wenzelm@66762
   183
        detect = Build_Log.Prop.build_tags + " = " + SQL.string("polyml-5.7") + " AND " +
wenzelm@66762
   184
          Build_Log.Settings.ML_OPTIONS + " <> " + SQL.string("-H 500")),
wenzelm@66762
   185
      Remote_Build("Poly/ML 5.7 Mac OS X", "macbroy2",
wenzelm@66762
   186
        history_base = "37074e22e8be",
wenzelm@66762
   187
        options = "-m32 -B -M1x4,4 -t polyml-5.7 -i 'init_component /home/isabelle/contrib/polyml-5.7'",
wenzelm@66762
   188
        args = "-a",
wenzelm@66762
   189
        detect = Build_Log.Prop.build_tags + " = " + SQL.string("polyml-5.7")),
wenzelm@65806
   190
      Remote_Build("Poly/ML test", "lxbroy8",
wenzelm@65843
   191
        options = "-m32 -B -M1x2,2 -t polyml-test -i 'init_component /home/isabelle/contrib/polyml-5.7-20170217'",
wenzelm@65806
   192
        args = "-N -g timing",
wenzelm@65806
   193
        detect = Build_Log.Prop.build_tags + " = " + SQL.string("polyml-test")),
wenzelm@65806
   194
      Remote_Build("Mac OS X 10.8 Mountain Lion", "macbroy30", options = "-m32 -M2", args = "-a",
wenzelm@65806
   195
        detect = Build_Log.Prop.build_start + " < date '2017-03-03'"))
wenzelm@65806
   196
wenzelm@65806
   197
wenzelm@65806
   198
  val remote_builds: List[List[Remote_Build]] =
wenzelm@65732
   199
  {
wenzelm@64231
   200
    List(
wenzelm@66762
   201
      List(Remote_Build("Poly/ML 5.7.1 Linux", "lxbroy8",
wenzelm@65820
   202
        history_base = "37074e22e8be",
wenzelm@66762
   203
        options = "-m32 -B -M1x2,2 -t polyml-5.7.1-pre1 -i 'init_component /home/isabelle/contrib/polyml-test-e7a662f8f9c4'",
wenzelm@65732
   204
        args = "-N -g timing",
wenzelm@66762
   205
        detect = Build_Log.Prop.build_tags + " = " + SQL.string("polyml-5.7.1-pre1"))),
wenzelm@65764
   206
      List(Remote_Build("Linux A", "lxbroy9",
wenzelm@65732
   207
        options = "-m32 -B -M1x2,2", args = "-N -g timing")),
wenzelm@65820
   208
      List(Remote_Build("Linux B", "lxbroy10", historic = true, history = 90,
wenzelm@65732
   209
        options = "-m32 -B -M1x4,2,4,6", args = "-N -g timing")),
wenzelm@64351
   210
      List(
wenzelm@65902
   211
        Remote_Build("Mac OS X 10.9 Mavericks", "macbroy2",
wenzelm@65906
   212
          options = "-m32 -M8" +
wenzelm@65906
   213
            " -e ISABELLE_GHC=ghc -e ISABELLE_MLTON=mlton -e ISABELLE_OCAML=ocaml" +
wenzelm@65906
   214
            " -e ISABELLE_OCAMLC=ocamlc -e ISABELLE_SMLNJ=/mnt/nfsbroy/home/smlnj/bin/sml",
wenzelm@65902
   215
          args = "-a",
wenzelm@66856
   216
          detect = Build_Log.Prop.build_tags.undefined),
wenzelm@65768
   217
        Remote_Build("Mac OS X 10.9 Mavericks, quick_and_dirty", "macbroy2",
wenzelm@65732
   218
          options = "-m32 -M8 -t quick_and_dirty", args = "-a -o quick_and_dirty",
wenzelm@65732
   219
          detect = Build_Log.Prop.build_tags + " = " + SQL.string("quick_and_dirty")),
wenzelm@65768
   220
        Remote_Build("Mac OS X 10.9 Mavericks, skip_proofs", "macbroy2",
wenzelm@65732
   221
          options = "-m32 -M8 -t skip_proofs", args = "-a -o skip_proofs",
wenzelm@66009
   222
          detect = Build_Log.Prop.build_tags + " = " + SQL.string("skip_proofs")),
wenzelm@66762
   223
        Remote_Build("Poly/ML 5.7.1 Mac OS X", "macbroy2",
wenzelm@66009
   224
          history_base = "37074e22e8be",
wenzelm@66762
   225
          options = "-m32 -B -M1x4,4 -t polyml-5.7.1-pre1 -i 'init_component /home/isabelle/contrib/polyml-test-e7a662f8f9c4'",
wenzelm@66009
   226
          args = "-a",
wenzelm@66762
   227
          detect = Build_Log.Prop.build_tags + " = " + SQL.string("polyml-5.7.1-pre1"))),
wenzelm@65768
   228
      List(
wenzelm@65768
   229
        Remote_Build("Mac OS X 10.12 Sierra", "macbroy30", options = "-m32 -M2", args = "-a",
wenzelm@65768
   230
          detect = Build_Log.Prop.build_start + " > date '2017-03-03'")),
wenzelm@65768
   231
      List(Remote_Build("Mac OS X 10.10 Yosemite", "macbroy31", options = "-m32 -M2", args = "-a")),
wenzelm@64351
   232
      List(
wenzelm@66008
   233
        Remote_Build("Windows", "vmnipkow9", historic = true, history = 90, shared_home = false,
wenzelm@65906
   234
          options = "-m32 -M4" +
wenzelm@65906
   235
            " -e ISABELLE_OCAML=ocaml -e ISABELLE_OCAMLC=ocamlc" +
wenzelm@65906
   236
            " -e ISABELLE_GHC=/usr/local/ghc-8.0.2/bin/ghc" +
wenzelm@65906
   237
            " -e ISABELLE_SMLNJ=/usr/local/smlnj-110.81/bin/sml",
wenzelm@65902
   238
          args = "-a",
wenzelm@65732
   239
          detect = Build_Log.Settings.ML_PLATFORM + " = " + SQL.string("x86-windows")),
wenzelm@66008
   240
        Remote_Build("Windows", "vmnipkow9", historic = true, history = 90, shared_home = false,
wenzelm@65906
   241
          options = "-m64 -M4" +
wenzelm@65906
   242
            " -e ISABELLE_OCAML=ocaml -e ISABELLE_OCAMLC=ocamlc" +
wenzelm@65906
   243
            " -e ISABELLE_GHC=/usr/local/ghc-8.0.2/bin/ghc" +
wenzelm@65906
   244
            " -e ISABELLE_SMLNJ=/usr/local/smlnj-110.81/bin/sml",
wenzelm@65902
   245
          args = "-a",
wenzelm@66864
   246
          detect = Build_Log.Settings.ML_PLATFORM + " = " + SQL.string("x86_64-windows")))
wenzelm@66864
   247
    ) :::
wenzelm@66864
   248
    {
wenzelm@66864
   249
      for { (host, n) <- List("lxbroy6" -> 1, "lxbroy7" -> 2) }
wenzelm@66864
   250
      yield {
wenzelm@66864
   251
        List(Remote_Build("AFP " + n, host = host,
wenzelm@66864
   252
          options = "-m32 -M1x2 -t AFP -P" + n,
wenzelm@66864
   253
          args = "-N -X slow",
wenzelm@66864
   254
          afp = true,
wenzelm@66864
   255
          detect = Build_Log.Prop.build_tags + " = " + SQL.string("AFP")))
wenzelm@66864
   256
      }
wenzelm@66864
   257
    }
wenzelm@65732
   258
  }
wenzelm@64231
   259
wenzelm@66864
   260
  private def remote_build_history(
wenzelm@66864
   261
    rev: String, afp_rev: Option[String], i: Int, r: Remote_Build): Logger_Task =
wenzelm@64294
   262
  {
wenzelm@64294
   263
    val task_name = "build_history-" + r.host
wenzelm@64294
   264
    Logger_Task(task_name, logger =>
wenzelm@64231
   265
      {
wenzelm@64231
   266
        using(logger.ssh_context.open_session(host = r.host, user = r.user, port = r.port))(
wenzelm@64259
   267
          ssh =>
wenzelm@64231
   268
            {
wenzelm@64348
   269
              val self_update = !r.shared_home
wenzelm@64348
   270
              val push_isabelle_home = self_update && Mercurial.is_repository(Path.explode("~~"))
wenzelm@64348
   271
wenzelm@65929
   272
              val results =
wenzelm@64468
   273
                Build_History.remote_build_history(ssh,
wenzelm@64468
   274
                  isabelle_repos,
wenzelm@64468
   275
                  isabelle_repos.ext(r.host),
wenzelm@65917
   276
                  isabelle_identifier = "cronjob_build_history",
wenzelm@64468
   277
                  self_update = self_update,
wenzelm@64468
   278
                  push_isabelle_home = push_isabelle_home,
wenzelm@66106
   279
                  rev = rev,
wenzelm@66864
   280
                  afp_rev = afp_rev,
wenzelm@64468
   281
                  options =
wenzelm@65949
   282
                    " -N " + Bash.string(task_name) + (if (i < 0) "" else "_" + (i + 1).toString) +
wenzelm@65887
   283
                    " -f " + r.options,
wenzelm@64468
   284
                  args = "-o timeout=10800 " + r.args)
wenzelm@64346
   285
wenzelm@64481
   286
              for ((log_name, bytes) <- results) {
wenzelm@64481
   287
                logger.log(Date.now(), log_name)
wenzelm@64468
   288
                Bytes.write(logger.log_dir + Path.explode(log_name), bytes)
wenzelm@64481
   289
              }
wenzelm@64231
   290
            })
wenzelm@64231
   291
      })
wenzelm@64294
   292
  }
wenzelm@64231
   293
wenzelm@65747
   294
  val build_status_profiles: List[Build_Status.Profile] =
wenzelm@65768
   295
    (remote_builds_old :: remote_builds).flatten.map(_.profile)
wenzelm@65747
   296
wenzelm@65746
   297
wenzelm@64192
   298
wenzelm@64192
   299
  /** task logging **/
wenzelm@64171
   300
wenzelm@64193
   301
  sealed case class Logger_Task(name: String = "", body: Logger => Unit)
wenzelm@64154
   302
wenzelm@64257
   303
  class Log_Service private[Isabelle_Cronjob](progress: Progress, val ssh_context: SSH.Context)
wenzelm@64171
   304
  {
wenzelm@64219
   305
    current_log.file.delete
wenzelm@64219
   306
wenzelm@64171
   307
    private val thread: Consumer_Thread[String] =
wenzelm@64171
   308
      Consumer_Thread.fork("cronjob: logger", daemon = true)(
wenzelm@64171
   309
        consume = (text: String) =>
wenzelm@64219
   310
          { // critical
wenzelm@64219
   311
            File.append(current_log, text + "\n")
wenzelm@64219
   312
            File.append(cumulative_log, text + "\n")
wenzelm@64171
   313
            progress.echo(text)
wenzelm@64171
   314
            true
wenzelm@64171
   315
          })
wenzelm@64171
   316
wenzelm@64171
   317
    def shutdown() { thread.shutdown() }
wenzelm@64171
   318
wenzelm@64171
   319
    val hostname = Isabelle_System.hostname()
wenzelm@64171
   320
wenzelm@64171
   321
    def log(date: Date, task_name: String, msg: String): Unit =
wenzelm@64193
   322
      if (task_name != "")
wenzelm@64193
   323
        thread.send(
wenzelm@64193
   324
          "[" + Build_Log.print_date(date) + ", " + hostname + ", " + task_name + "]: " + msg)
wenzelm@64171
   325
wenzelm@64171
   326
    def start_logger(start_date: Date, task_name: String): Logger =
wenzelm@64171
   327
      new Logger(this, start_date, task_name)
wenzelm@64154
   328
wenzelm@64171
   329
    def run_task(start_date: Date, task: Logger_Task)
wenzelm@64171
   330
    {
wenzelm@64171
   331
      val logger = start_logger(start_date, task.name)
wenzelm@64171
   332
      val res = Exn.capture { task.body(logger) }
wenzelm@64171
   333
      val end_date = Date.now()
wenzelm@64171
   334
      val err =
wenzelm@64171
   335
        res match {
wenzelm@64171
   336
          case Exn.Res(_) => None
wenzelm@64295
   337
          case Exn.Exn(exn) =>
wenzelm@64410
   338
            System.err.println("Exception trace for " + quote(task.name) + ":")
wenzelm@64397
   339
            exn.printStackTrace()
wenzelm@64295
   340
            val first_line = Library.split_lines(Exn.message(exn)).headOption getOrElse "exception"
wenzelm@64295
   341
            Some(first_line)
wenzelm@64171
   342
        }
wenzelm@64171
   343
      logger.log_end(end_date, err)
wenzelm@64171
   344
    }
wenzelm@64171
   345
wenzelm@64171
   346
    def fork_task(start_date: Date, task: Logger_Task): Task =
wenzelm@64171
   347
      new Task(task.name, run_task(start_date, task))
wenzelm@64171
   348
  }
wenzelm@64171
   349
wenzelm@64171
   350
  class Logger private[Isabelle_Cronjob](
wenzelm@64171
   351
    val log_service: Log_Service, val start_date: Date, val task_name: String)
wenzelm@64162
   352
  {
wenzelm@64257
   353
    def ssh_context: SSH.Context = log_service.ssh_context
wenzelm@65640
   354
    def options: Options = ssh_context.options
wenzelm@64231
   355
wenzelm@64171
   356
    def log(date: Date, msg: String): Unit = log_service.log(date, task_name, msg)
wenzelm@64171
   357
wenzelm@64171
   358
    def log_end(end_date: Date, err: Option[String])
wenzelm@64171
   359
    {
wenzelm@64171
   360
      val elapsed_time = end_date.time - start_date.time
wenzelm@64171
   361
      val msg =
wenzelm@64171
   362
        (if (err.isEmpty) "finished" else "ERROR " + err.get) +
wenzelm@64197
   363
        (if (elapsed_time.seconds < 3.0) "" else " (" + elapsed_time.message_hms + " elapsed time)")
wenzelm@64171
   364
      log(end_date, msg)
wenzelm@64171
   365
    }
wenzelm@64171
   366
wenzelm@64195
   367
    val log_dir: Path = main_dir + Build_Log.log_subdir(start_date)
wenzelm@64195
   368
wenzelm@64195
   369
    Isabelle_System.mkdirs(log_dir)
wenzelm@64171
   370
    log(start_date, "started")
wenzelm@64171
   371
  }
wenzelm@64171
   372
wenzelm@64171
   373
  class Task private[Isabelle_Cronjob](name: String, body: => Unit)
wenzelm@64171
   374
  {
wenzelm@64171
   375
    private val future: Future[Unit] = Future.thread("cronjob: " + name) { body }
wenzelm@64171
   376
    def is_finished: Boolean = future.is_finished
wenzelm@64162
   377
  }
wenzelm@64153
   378
wenzelm@64170
   379
wenzelm@64170
   380
wenzelm@64153
   381
  /** cronjob **/
wenzelm@64153
   382
wenzelm@64187
   383
  def cronjob(progress: Progress, exclude_task: Set[String])
wenzelm@64153
   384
  {
wenzelm@64171
   385
    /* soft lock */
wenzelm@64153
   386
wenzelm@64153
   387
    val still_running =
wenzelm@64153
   388
      try { Some(File.read(main_state_file)) }
wenzelm@64153
   389
      catch { case ERROR(_) => None }
wenzelm@64153
   390
wenzelm@64153
   391
    still_running match {
wenzelm@64170
   392
      case None | Some("") =>
wenzelm@64153
   393
      case Some(running) =>
wenzelm@64153
   394
        error("Isabelle cronjob appears to be still running: " + running)
wenzelm@64153
   395
    }
wenzelm@64153
   396
wenzelm@64199
   397
wenzelm@64199
   398
    /* log service */
wenzelm@64199
   399
wenzelm@64257
   400
    val log_service = new Log_Service(progress, SSH.init_context(Options.init()))
wenzelm@64154
   401
wenzelm@64199
   402
    def run(start_date: Date, task: Logger_Task) { log_service.run_task(start_date, task) }
wenzelm@64199
   403
wenzelm@64199
   404
    def run_now(task: Logger_Task) { run(Date.now(), task) }
wenzelm@64154
   405
wenzelm@64154
   406
wenzelm@64199
   407
    /* structured tasks */
wenzelm@64184
   408
wenzelm@64350
   409
    def SEQ(tasks: List[Logger_Task]): Logger_Task = Logger_Task(body = _ =>
wenzelm@64193
   410
      for (task <- tasks.iterator if !exclude_task(task.name) || task.name == "")
wenzelm@64199
   411
        run_now(task))
wenzelm@64153
   412
wenzelm@64350
   413
    def PAR(tasks: List[Logger_Task]): Logger_Task = Logger_Task(body = _ =>
wenzelm@64170
   414
      {
wenzelm@64199
   415
        @tailrec def join(running: List[Task])
wenzelm@64199
   416
        {
wenzelm@64199
   417
          running.partition(_.is_finished) match {
wenzelm@64199
   418
            case (Nil, Nil) =>
wenzelm@64199
   419
            case (Nil, _ :: _) => Thread.sleep(500); join(running)
wenzelm@64199
   420
            case (_ :: _, remaining) => join(remaining)
wenzelm@64199
   421
          }
wenzelm@64170
   422
        }
wenzelm@64199
   423
        val start_date = Date.now()
wenzelm@64199
   424
        val running =
wenzelm@64350
   425
          for (task <- tasks if !exclude_task(task.name))
wenzelm@64199
   426
            yield log_service.fork_task(start_date, task)
wenzelm@64199
   427
        join(running)
wenzelm@64199
   428
      })
wenzelm@64193
   429
wenzelm@64170
   430
wenzelm@64170
   431
    /* main */
wenzelm@64170
   432
wenzelm@64199
   433
    val main_start_date = Date.now()
wenzelm@64199
   434
    File.write(main_state_file, main_start_date + " " + log_service.hostname)
wenzelm@64199
   435
wenzelm@65820
   436
    val hg = Mercurial.repository(isabelle_repos)
wenzelm@65820
   437
    val rev = hg.id()
wenzelm@64231
   438
wenzelm@64193
   439
    run(main_start_date,
wenzelm@65783
   440
      Logger_Task("isabelle_cronjob", logger =>
wenzelm@64231
   441
        run_now(
wenzelm@64405
   442
          SEQ(List(build_release, build_history_base,
wenzelm@65783
   443
            PAR(remote_builds.map(seq =>
wenzelm@65915
   444
              SEQ(
wenzelm@65915
   445
                for {
wenzelm@65915
   446
                  (r, i) <- (if (seq.length <= 1) seq.map((_, -1)) else seq.zipWithIndex)
wenzelm@66864
   447
                  (isabelle_rev, afp_rev) <- r.pick(logger.options, rev, r.history_base_filter(hg))
wenzelm@66864
   448
                } yield remote_build_history(isabelle_rev, afp_rev, i, r)))),
wenzelm@65666
   449
            Logger_Task("jenkins_logs", _ => Jenkins.download_logs(jenkins_jobs, main_dir)),
wenzelm@65770
   450
            Logger_Task("build_log_database",
wenzelm@65770
   451
              logger => Isabelle_Devel.build_log_database(logger.options)),
wenzelm@65770
   452
            Logger_Task("build_status",
wenzelm@65793
   453
              logger => Isabelle_Devel.build_status(logger.options)))))))
wenzelm@64153
   454
wenzelm@64171
   455
    log_service.shutdown()
wenzelm@64170
   456
wenzelm@64153
   457
    main_state_file.file.delete
wenzelm@64153
   458
  }
wenzelm@64153
   459
wenzelm@64153
   460
wenzelm@64153
   461
wenzelm@64153
   462
  /** command line entry point **/
wenzelm@64153
   463
wenzelm@64148
   464
  def main(args: Array[String])
wenzelm@64148
   465
  {
wenzelm@64148
   466
    Command_Line.tool0 {
wenzelm@64148
   467
      var force = false
wenzelm@64148
   468
      var verbose = false
wenzelm@64187
   469
      var exclude_task = Set.empty[String]
wenzelm@64148
   470
wenzelm@64148
   471
      val getopts = Getopts("""
wenzelm@64148
   472
Usage: Admin/cronjob/main [OPTIONS]
wenzelm@64148
   473
wenzelm@64148
   474
  Options are:
wenzelm@64148
   475
    -f           apply force to do anything
wenzelm@64148
   476
    -v           verbose
wenzelm@64187
   477
    -x NAME      exclude tasks with this name
wenzelm@64148
   478
""",
wenzelm@64148
   479
        "f" -> (_ => force = true),
wenzelm@64187
   480
        "v" -> (_ => verbose = true),
wenzelm@64187
   481
        "x:" -> (arg => exclude_task += arg))
wenzelm@64148
   482
wenzelm@64148
   483
      val more_args = getopts(args)
wenzelm@64148
   484
      if (more_args.nonEmpty) getopts.usage()
wenzelm@64148
   485
wenzelm@64909
   486
      val progress = if (verbose) new Console_Progress() else No_Progress
wenzelm@64148
   487
wenzelm@64187
   488
      if (force) cronjob(progress, exclude_task)
wenzelm@64153
   489
      else error("Need to apply force to do anything")
wenzelm@64148
   490
    }
wenzelm@64148
   491
  }
wenzelm@64148
   492
}