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