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