src/Pure/Admin/isabelle_cronjob.scala
author wenzelm
Tue, 11 Oct 2016 21:48:56 +0200
changeset 64153 769791954872
parent 64148 bbf43b7c4d0d
child 64154 e5cf40a54b1e
permissions -rw-r--r--
some timing and logging, similar to old isatest.log;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64148
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Admin/isabelle_cronjob.scala
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
     3
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
     4
Main entry point for administrative cronjob at TUM.
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
     5
*/
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
     6
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
     7
package isabelle
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
     8
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
     9
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    10
object Isabelle_Cronjob
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    11
{
64153
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    12
  /** file-system state: owned by main cronjob **/
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    13
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    14
  val main_dir = Path.explode("~/cronjob")
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    15
  val run_dir = main_dir + Path.explode("run")
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    16
  val log_dir = main_dir + Path.explode("log")
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    17
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    18
  val main_state_file = run_dir + Path.explode("main.state")
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    19
  val main_log = log_dir + Path.explode("main.log")
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    20
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    21
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    22
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    23
  /** cronjob **/
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    24
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    25
  def cronjob(progress: Progress)
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    26
  {
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    27
    /* log */
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    28
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    29
    val hostname = Isabelle_System.hostname()
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    30
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    31
    def log(date: Date, msg: String)
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    32
    {
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    33
      val text = "[" + Build_Log.Log_File.Date_Format(date) + " " + hostname + "]: " + msg
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    34
      File.append(main_log, text + "\n")
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    35
      progress.echo(text)
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    36
    }
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    37
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    38
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    39
    /* start */
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    40
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    41
    val start_date = Date.now()
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    42
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    43
    val still_running =
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    44
      try { Some(File.read(main_state_file)) }
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    45
      catch { case ERROR(_) => None }
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    46
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    47
    still_running match {
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    48
      case Some(running) =>
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    49
        error("Isabelle cronjob appears to be still running: " + running)
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    50
      case None =>
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    51
        File.write(main_state_file, start_date + " " + hostname)
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    52
        log(start_date, "start cronjob")
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    53
    }
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    54
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    55
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    56
    /* end */
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    57
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    58
    val end_date = Date.now()
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    59
    val elapsed_time = end_date.time - start_date.time
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    60
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    61
    log(end_date, "end cronjob, elapsed time " + elapsed_time.message_hms)
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    62
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    63
    main_state_file.file.delete
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    64
  }
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    65
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    66
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    67
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    68
  /** command line entry point **/
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    69
64148
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    70
  def main(args: Array[String])
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    71
  {
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    72
    Command_Line.tool0 {
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    73
      var force = false
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    74
      var verbose = false
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    75
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    76
      val getopts = Getopts("""
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    77
Usage: Admin/cronjob/main [OPTIONS]
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    78
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    79
  Options are:
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    80
    -f           apply force to do anything
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    81
    -v           verbose
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    82
""",
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    83
        "f" -> (_ => force = true),
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    84
        "v" -> (_ => verbose = true))
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    85
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    86
      val more_args = getopts(args)
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    87
      if (more_args.nonEmpty) getopts.usage()
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    88
64153
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    89
      val progress = if (verbose) new Console_Progress() else Ignore_Progress
64148
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    90
64153
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    91
      if (force) cronjob(progress)
769791954872 some timing and logging, similar to old isatest.log;
wenzelm
parents: 64148
diff changeset
    92
      else error("Need to apply force to do anything")
64148
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    93
    }
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    94
  }
bbf43b7c4d0d basic setup for Isabelle cronjob;
wenzelm
parents:
diff changeset
    95
}