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