src/Pure/Build/build_job.scala
author wenzelm
Thu, 06 Jun 2024 12:53:02 +0200
changeset 80268 979f3893aa37
parent 80224 db92e0b6a11a
child 80270 1d4300506338
permissions -rw-r--r--
more informative ZBox;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
79502
c7a98469c0e7 clarified directories;
wenzelm
parents: 78985
diff changeset
     1
/*  Title:      Pure/Build/build_job.scala
72662
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     3
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     4
Build job running prover process, with rudimentary PIDE session.
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     5
*/
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     6
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     8
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
     9
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
    10
import scala.collection.mutable
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
    11
5c08ad7adf77 clarified modules;
wenzelm
parents:
diff changeset
    12
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
    13
trait Build_Job {
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77485
diff changeset
    14
  def cancel(): Unit = ()
77298
39c7d79ace34 clarified signature defaults;
wenzelm
parents: 77297
diff changeset
    15
  def is_finished: Boolean = false
79887
17220dc05991 revert most parts of 0e79fa88cab6: somewhat ambitious attempt to move towards "editing" builds via added/canceled workers;
wenzelm
parents: 79718
diff changeset
    16
  def join: Build_Job.Result = Build_Job.no_result
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
    17
}
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
    18
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74755
diff changeset
    19
object Build_Job {
78530
d637e60427db tuned signature;
wenzelm
parents: 78529
diff changeset
    20
  sealed case class Result(process_result: Process_Result, output_shasum: SHA1.Shasum)
79887
17220dc05991 revert most parts of 0e79fa88cab6: somewhat ambitious attempt to move towards "editing" builds via added/canceled workers;
wenzelm
parents: 79718
diff changeset
    21
  val no_result: Result = Result(Process_Result.undefined, SHA1.no_shasum)
78530
d637e60427db tuned signature;
wenzelm
parents: 78529
diff changeset
    22
d637e60427db tuned signature;
wenzelm
parents: 78529
diff changeset
    23
77396
f184fbac99bc clarified modules;
wenzelm
parents: 77372
diff changeset
    24
  /* build session */
f184fbac99bc clarified modules;
wenzelm
parents: 77372
diff changeset
    25
77474
5ecaf881b563 clarified signature;
wenzelm
parents: 77473
diff changeset
    26
  def start_session(
78421
fd24f380b588 clarified modules;
wenzelm
parents: 78379
diff changeset
    27
    build_context: Build.Context,
78237
c2c59de57df9 clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents: 78213
diff changeset
    28
    session_context: Session_Context,
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
    29
    progress: Progress,
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
    30
    log: Logger,
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
    31
    server: SSH.Server,
77474
5ecaf881b563 clarified signature;
wenzelm
parents: 77473
diff changeset
    32
    session_background: Sessions.Background,
78237
c2c59de57df9 clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents: 78213
diff changeset
    33
    sources_shasum: SHA1.Shasum,
77474
5ecaf881b563 clarified signature;
wenzelm
parents: 77473
diff changeset
    34
    input_shasum: SHA1.Shasum,
78237
c2c59de57df9 clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents: 78213
diff changeset
    35
    node_info: Host.Node_Info,
c2c59de57df9 clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents: 78213
diff changeset
    36
    store_heap: Boolean
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
    37
  ): Session_Job = {
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
    38
    new Session_Job(build_context, session_context, progress, log, server,
78237
c2c59de57df9 clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents: 78213
diff changeset
    39
      session_background, sources_shasum, input_shasum, node_info, store_heap)
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
    40
  }
77474
5ecaf881b563 clarified signature;
wenzelm
parents: 77473
diff changeset
    41
77442
9969b6aed223 clarified modules (again);
wenzelm
parents: 77414
diff changeset
    42
  object Session_Context {
9969b6aed223 clarified modules (again);
wenzelm
parents: 77414
diff changeset
    43
    def load(
78374
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    44
      database_server: Option[SQL.Database],
77529
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77521
diff changeset
    45
      build_uuid: String,
77444
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77442
diff changeset
    46
      name: String,
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77442
diff changeset
    47
      deps: List[String],
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77442
diff changeset
    48
      ancestors: List[String],
77610
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77587
diff changeset
    49
      session_prefs: String,
77458
403748b23f13 clarified signature: prefer static data;
wenzelm
parents: 77456
diff changeset
    50
      sources_shasum: SHA1.Shasum,
77442
9969b6aed223 clarified modules (again);
wenzelm
parents: 77414
diff changeset
    51
      timeout: Time,
78178
a177f71dc79f clarified modules;
wenzelm
parents: 77652
diff changeset
    52
      store: Store,
78374
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    53
      progress: Progress = new Progress
77442
9969b6aed223 clarified modules (again);
wenzelm
parents: 77414
diff changeset
    54
    ): Session_Context = {
77450
167b5095ba14 tuned signature (again);
wenzelm
parents: 77445
diff changeset
    55
      def default: Session_Context =
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77493
diff changeset
    56
        Session_Context(
77610
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77587
diff changeset
    57
          name, deps, ancestors, session_prefs, sources_shasum, timeout,
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77587
diff changeset
    58
          Time.zero, Bytes.empty, build_uuid)
77444
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77442
diff changeset
    59
78374
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    60
      def read(db: SQL.Database): Session_Context = {
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    61
        def ignore_error(msg: String) = {
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    62
          progress.echo_warning(
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    63
            "Ignoring bad database " + db + " for session " + quote(name) +
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    64
            if_proper(msg, ":\n" + msg))
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    65
          default
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    66
        }
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    67
        try {
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    68
          val command_timings = store.read_command_timings(db, name)
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    69
          val elapsed =
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    70
            store.read_session_timing(db, name) match {
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    71
              case Markup.Elapsed(s) => Time.seconds(s)
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    72
              case _ => Time.zero
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    73
            }
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    74
          new Session_Context(
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    75
            name, deps, ancestors, session_prefs, sources_shasum, timeout,
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    76
            elapsed, command_timings, build_uuid)
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    77
        }
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    78
        catch {
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    79
          case ERROR(msg) => ignore_error(msg)
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    80
          case exn: java.lang.Error => ignore_error(Exn.message(exn))
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    81
          case _: XML.Error => ignore_error("XML.Error")
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    82
        }
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    83
      }
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    84
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    85
      database_server match {
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    86
        case Some(db) => if (store.session_info_exists(db)) read(db) else default
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
    87
        case None => using_option(store.try_open_database(name))(read) getOrElse default
77442
9969b6aed223 clarified modules (again);
wenzelm
parents: 77414
diff changeset
    88
      }
9969b6aed223 clarified modules (again);
wenzelm
parents: 77414
diff changeset
    89
    }
9969b6aed223 clarified modules (again);
wenzelm
parents: 77414
diff changeset
    90
  }
9969b6aed223 clarified modules (again);
wenzelm
parents: 77414
diff changeset
    91
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77493
diff changeset
    92
  sealed case class Session_Context(
f0d9f9196b9b more database content;
wenzelm
parents: 77493
diff changeset
    93
    name: String,
f0d9f9196b9b more database content;
wenzelm
parents: 77493
diff changeset
    94
    deps: List[String],
f0d9f9196b9b more database content;
wenzelm
parents: 77493
diff changeset
    95
    ancestors: List[String],
77610
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77587
diff changeset
    96
    session_prefs: String,
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77493
diff changeset
    97
    sources_shasum: SHA1.Shasum,
f0d9f9196b9b more database content;
wenzelm
parents: 77493
diff changeset
    98
    timeout: Time,
f0d9f9196b9b more database content;
wenzelm
parents: 77493
diff changeset
    99
    old_time: Time,
f0d9f9196b9b more database content;
wenzelm
parents: 77493
diff changeset
   100
    old_command_timings_blob: Bytes,
77529
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77521
diff changeset
   101
    build_uuid: String
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   102
  ) extends Library.Named
77442
9969b6aed223 clarified modules (again);
wenzelm
parents: 77414
diff changeset
   103
77474
5ecaf881b563 clarified signature;
wenzelm
parents: 77473
diff changeset
   104
  class Session_Job private[Build_Job](
78421
fd24f380b588 clarified modules;
wenzelm
parents: 78379
diff changeset
   105
    build_context: Build.Context,
78237
c2c59de57df9 clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents: 78213
diff changeset
   106
    session_context: Session_Context,
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   107
    progress: Progress,
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   108
    log: Logger,
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   109
    server: SSH.Server,
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   110
    session_background: Sessions.Background,
78237
c2c59de57df9 clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents: 78213
diff changeset
   111
    sources_shasum: SHA1.Shasum,
77460
ccca589d7027 tuned signature: support general Build_Job instances;
wenzelm
parents: 77458
diff changeset
   112
    input_shasum: SHA1.Shasum,
78237
c2c59de57df9 clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents: 78213
diff changeset
   113
    node_info: Host.Node_Info,
c2c59de57df9 clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents: 78213
diff changeset
   114
    store_heap: Boolean
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   115
  ) extends Build_Job {
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   116
    def session_name: String = session_background.session_name
77349
5a84de89170d more operations to support management of jobs, e.g. from external database;
wenzelm
parents: 77298
diff changeset
   117
79887
17220dc05991 revert most parts of 0e79fa88cab6: somewhat ambitious attempt to move towards "editing" builds via added/canceled workers;
wenzelm
parents: 79718
diff changeset
   118
    private val future_result: Future[Result] =
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   119
      Future.thread("build", uninterruptible = true) {
78359
cb0a90df4871 prefer asynchronous operations: reduce time spent within synchronized_database("Build_Process.start_job");
wenzelm
parents: 78280
diff changeset
   120
        val info = session_background.sessions_structure(session_name)
78842
eb572f7b6689 prefer extensible next_node_info in build process over process_options in build engine (which needs the final node info anyway);
Fabian Huch <huch@in.tum.de>
parents: 78674
diff changeset
   121
        val options = Host.node_options(info.options, node_info)
78359
cb0a90df4871 prefer asynchronous operations: reduce time spent within synchronized_database("Build_Process.start_job");
wenzelm
parents: 78280
diff changeset
   122
        val store = build_context.store
cb0a90df4871 prefer asynchronous operations: reduce time spent within synchronized_database("Build_Process.start_job");
wenzelm
parents: 78280
diff changeset
   123
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   124
        using_optional(store.maybe_open_database_server(server = server)) { database_server =>
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   125
          store.clean_output(database_server, session_name, session_init = true)
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   126
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   127
          val session_sources =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   128
            Store.Sources.load(session_background.base, cache = store.cache.compress)
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   129
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   130
          val env =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   131
            Isabelle_System.settings(
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   132
              List("ISABELLE_ML_DEBUGGER" -> options.bool("ML_debugger").toString))
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   133
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   134
          val session_heaps =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   135
            session_background.info.parent match {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   136
              case None => Nil
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   137
              case Some(logic) => ML_Process.session_heaps(store, session_background, logic = logic)
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   138
            }
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   139
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   140
          val use_prelude = if (session_heaps.isEmpty) Thy_Header.ml_roots.map(_._1) else Nil
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   141
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   142
          val eval_store =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   143
            if (store_heap) {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   144
              (if (info.theories.nonEmpty) List("ML_Heap.share_common_data ()") else Nil) :::
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   145
              List("ML_Heap.save_child " +
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   146
                ML_Syntax.print_string_bytes(File.platform_path(store.output_heap(session_name))))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   147
            }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   148
            else Nil
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   149
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   150
          def session_blobs(node_name: Document.Node.Name): List[(Command.Blob, Document.Blobs.Item)] =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   151
            session_background.base.theory_load_commands.get(node_name.theory) match {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   152
              case None => Nil
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   153
              case Some(spans) =>
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   154
                val syntax = session_background.base.theory_syntax(node_name)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   155
                val master_dir = Path.explode(node_name.master_dir)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   156
                for (span <- spans; file <- span.loaded_files(syntax).files)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   157
                  yield {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   158
                    val src_path = Path.explode(file)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   159
                    val blob_name = Document.Node.Name(File.symbolic_path(master_dir + src_path))
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   160
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   161
                    val bytes = session_sources(blob_name.node).bytes
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   162
                    val text = bytes.text
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   163
                    val chunk = Symbol.Text_Chunk(text)
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   164
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   165
                    Command.Blob(blob_name, src_path, Some((SHA1.digest(bytes), chunk))) ->
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   166
                      Document.Blobs.Item(bytes, text, chunk, changed = false)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   167
                  }
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   168
            }
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   169
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77485
diff changeset
   170
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   171
          /* session */
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77485
diff changeset
   172
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   173
          val resources =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   174
            new Resources(session_background, log = log,
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   175
              command_timings =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   176
                Properties.uncompress(session_context.old_command_timings_blob, cache = store.cache))
77485
911d3dbf2033 clarified timeout: closer to actual process;
wenzelm
parents: 77484
diff changeset
   177
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   178
          val session =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   179
            new Session(options, resources) {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   180
              override val cache: Term.Cache = store.cache
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   181
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   182
              override def build_blobs_info(node_name: Document.Node.Name): Command.Blobs_Info =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   183
                Command.Blobs_Info.make(session_blobs(node_name))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   184
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   185
              override def build_blobs(node_name: Document.Node.Name): Document.Blobs =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   186
                Document.Blobs.make(session_blobs(node_name))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   187
            }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   188
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   189
          object Build_Session_Errors {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   190
            private val promise: Promise[List[String]] = Future.promise
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   191
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   192
            def result: Exn.Result[List[String]] = promise.join_result
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   193
            def cancel(): Unit = promise.cancel()
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   194
            def apply(errs: List[String]): Unit = {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   195
              try { promise.fulfill(errs) }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   196
              catch { case _: IllegalStateException => }
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   197
            }
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   198
          }
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   199
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   200
          val export_consumer =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   201
            Export.consumer(store.open_database(session_name, output = true, server = server),
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   202
              store.cache, progress = progress)
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   203
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   204
          val stdout = new StringBuilder(1000)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   205
          val stderr = new StringBuilder(1000)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   206
          val command_timings = new mutable.ListBuffer[Properties.T]
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   207
          val theory_timings = new mutable.ListBuffer[Properties.T]
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   208
          val session_timings = new mutable.ListBuffer[Properties.T]
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   209
          val runtime_statistics = new mutable.ListBuffer[Properties.T]
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   210
          val task_statistics = new mutable.ListBuffer[Properties.T]
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   211
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   212
          def fun(
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   213
            name: String,
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   214
            acc: mutable.ListBuffer[Properties.T],
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   215
            unapply: Properties.T => Option[Properties.T]
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   216
          ): (String, Session.Protocol_Function) = {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   217
            name -> ((msg: Prover.Protocol_Output) =>
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   218
              unapply(msg.properties) match {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   219
                case Some(props) => acc += props; true
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   220
                case _ => false
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   221
              })
77485
911d3dbf2033 clarified timeout: closer to actual process;
wenzelm
parents: 77484
diff changeset
   222
          }
911d3dbf2033 clarified timeout: closer to actual process;
wenzelm
parents: 77484
diff changeset
   223
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   224
          session.init_protocol_handler(new Session.Protocol_Handler {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   225
              override def exit(): Unit = Build_Session_Errors.cancel()
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   226
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   227
              private def build_session_finished(msg: Prover.Protocol_Output): Boolean = {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   228
                val (rc, errors) =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   229
                  try {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   230
                    val (rc, errs) = {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   231
                      import XML.Decode._
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   232
                      pair(int, list(x => x))(Symbol.decode_yxml(msg.text))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   233
                    }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   234
                    val errors =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   235
                      for (err <- errs) yield {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   236
                        val prt = Protocol_Message.expose_no_reports(err)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   237
                        Pretty.string_of(prt, metric = Symbol.Metric)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   238
                      }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   239
                    (rc, errors)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   240
                  }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   241
                  catch { case ERROR(err) => (Process_Result.RC.failure, List(err)) }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   242
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   243
                session.protocol_command("Prover.stop", rc.toString)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   244
                Build_Session_Errors(errors)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   245
                true
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   246
              }
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   247
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   248
              private def loading_theory(msg: Prover.Protocol_Output): Boolean =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   249
                msg.properties match {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   250
                  case Markup.Loading_Theory(Markup.Name(name)) =>
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   251
                    progress.theory(Progress.Theory(name, session = session_name))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   252
                    false
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   253
                  case _ => false
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   254
                }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   255
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   256
              private def export_(msg: Prover.Protocol_Output): Boolean =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   257
                msg.properties match {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   258
                  case Protocol.Export(args) =>
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   259
                    export_consumer.make_entry(session_name, args, msg.chunk)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   260
                    true
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   261
                  case _ => false
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   262
                }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   263
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   264
              override val functions: Session.Protocol_Functions =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   265
                List(
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   266
                  Markup.Build_Session_Finished.name -> build_session_finished,
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   267
                  Markup.Loading_Theory.name -> loading_theory,
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   268
                  Markup.EXPORT -> export_,
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   269
                  fun(Markup.Theory_Timing.name, theory_timings, Markup.Theory_Timing.unapply),
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   270
                  fun(Markup.Session_Timing.name, session_timings, Markup.Session_Timing.unapply),
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   271
                  fun(Markup.Task_Statistics.name, task_statistics, Markup.Task_Statistics.unapply))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   272
            })
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   273
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   274
          session.command_timings += Session.Consumer("command_timings") {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   275
            case Session.Command_Timing(props) =>
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   276
              for {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   277
                elapsed <- Markup.Elapsed.unapply(props)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   278
                elapsed_time = Time.seconds(elapsed)
79692
3f307faf9111 tuned whitespace;
wenzelm
parents: 79691
diff changeset
   279
                if elapsed_time.is_relevant &&
3f307faf9111 tuned whitespace;
wenzelm
parents: 79691
diff changeset
   280
                   elapsed_time >= options.seconds("command_timing_threshold")
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   281
              } command_timings += props.filter(Markup.command_timing_property)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   282
          }
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   283
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   284
          session.runtime_statistics += Session.Consumer("ML_statistics") {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   285
            case Session.Runtime_Statistics(props) => runtime_statistics += props
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   286
          }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   287
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   288
          session.finished_theories += Session.Consumer[Document.Snapshot]("finished_theories") {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   289
            case snapshot =>
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   290
              if (!progress.stopped) {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   291
                def export_(name: String, xml: XML.Body, compress: Boolean = true): Unit = {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   292
                  if (!progress.stopped) {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   293
                    val theory_name = snapshot.node_name.theory
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   294
                    val args =
79692
3f307faf9111 tuned whitespace;
wenzelm
parents: 79691
diff changeset
   295
                      Protocol.Export.Args(
3f307faf9111 tuned whitespace;
wenzelm
parents: 79691
diff changeset
   296
                        theory_name = theory_name, name = name, compress = compress)
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   297
                    val body = Bytes(Symbol.encode(YXML.string_of_body(xml)))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   298
                    export_consumer.make_entry(session_name, args, body)
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   299
                  }
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   300
                }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   301
                def export_text(name: String, text: String, compress: Boolean = true): Unit =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   302
                  export_(name, List(XML.Text(text)), compress = compress)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   303
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   304
                for (command <- snapshot.snippet_command) {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   305
                  export_text(Export.DOCUMENT_ID, command.id.toString, compress = false)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   306
                }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   307
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   308
                export_text(Export.FILES,
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   309
                  cat_lines(snapshot.node_files.map(name => File.symbolic_path(name.path))),
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   310
                  compress = false)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   311
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   312
                for ((blob_name, i) <- snapshot.node_files.tail.zipWithIndex) {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   313
                  val xml = snapshot.switch(blob_name).xml_markup()
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   314
                  export_(Export.MARKUP + (i + 1), xml)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   315
                }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   316
                export_(Export.MARKUP, snapshot.xml_markup())
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   317
                export_(Export.MESSAGES, snapshot.messages.map(_._1))
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   318
              }
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   319
          }
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   320
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   321
          session.all_messages += Session.Consumer[Any]("build_session_output") {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   322
            case msg: Prover.Output =>
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   323
              val message = msg.message
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   324
              if (msg.is_system) resources.log(Protocol.message_text(message))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   325
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   326
              if (msg.is_stdout) {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   327
                stdout ++= Symbol.encode(XML.content(message))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   328
              }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   329
              else if (msg.is_stderr) {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   330
                stderr ++= Symbol.encode(XML.content(message))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   331
              }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   332
              else if (msg.is_exit) {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   333
                val err =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   334
                  "Prover terminated" +
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   335
                    (msg.properties match {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   336
                      case Markup.Process_Result(result) => ": " + result.print_rc
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   337
                      case _ => ""
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   338
                    })
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   339
                Build_Session_Errors(List(err))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   340
              }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   341
            case _ =>
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   342
          }
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   343
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   344
          build_context.session_setup(session_name, session)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   345
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   346
          val eval_main = Command_Line.ML_tool("Isabelle_Process.init_build ()" :: eval_store)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   347
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77485
diff changeset
   348
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   349
          /* process */
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   350
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   351
          val process =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   352
            Isabelle_Process.start(options, session, session_background, session_heaps,
80224
db92e0b6a11a clarified signature: prefer symbolic isabelle.Path over physical java.io.File;
wenzelm
parents: 80121
diff changeset
   353
              use_prelude = use_prelude, eval_main = eval_main, cwd = info.dir, env = env)
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   354
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   355
          val timeout_request: Option[Event_Timer.Request] =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   356
            if (info.timeout_ignored) None
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   357
            else Some(Event_Timer.request(Time.now() + info.timeout) { process.terminate() })
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77485
diff changeset
   358
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   359
          val build_errors =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   360
            Isabelle_Thread.interrupt_handler(_ => process.terminate()) {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   361
              Exn.capture { process.await_startup() } match {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   362
                case Exn.Res(_) =>
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   363
                  val resources_yxml = resources.init_session_yxml
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   364
                  val encode_options: XML.Encode.T[Options] =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   365
                    options => session.prover_options(options).encode
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   366
                  val args_yxml =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   367
                    YXML.string_of_body(
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   368
                      {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   369
                        import XML.Encode._
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   370
                        pair(string, list(pair(encode_options, list(pair(string, properties)))))(
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   371
                          (session_name, info.theories))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   372
                      })
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   373
                  session.protocol_command("build_session", resources_yxml, args_yxml)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   374
                  Build_Session_Errors.result
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   375
                case Exn.Exn(exn) => Exn.Res(List(Exn.message(exn)))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   376
              }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   377
            }
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   378
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   379
          val result0 =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   380
            Isabelle_Thread.interrupt_handler(_ => process.terminate()) { process.await_shutdown() }
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   381
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   382
          val was_timeout =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   383
            timeout_request match {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   384
              case None => false
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   385
              case Some(request) => !request.cancel()
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   386
            }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   387
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   388
          session.stop()
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   389
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   390
          val export_errors =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   391
            export_consumer.shutdown(close = true).map(Output.error_message_text)
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   392
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   393
          val (document_output, document_errors) =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   394
            try {
78674
88f47c70187a clarified signature;
wenzelm
parents: 78530
diff changeset
   395
              if (Exn.is_res(build_errors) && result0.ok && info.documents.nonEmpty) {
78379
f6ec57648894 reuse SSH.Server connection for database server;
wenzelm
parents: 78374
diff changeset
   396
                using(Export.open_database_context(store, server = server)) { database_context =>
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   397
                  val documents =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   398
                    using(database_context.open_session(session_background)) {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   399
                      session_context =>
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   400
                        Document_Build.build_documents(
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   401
                          Document_Build.context(session_context, progress = progress),
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   402
                          output_sources = info.document_output,
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   403
                          output_pdf = info.document_output)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   404
                    }
79692
3f307faf9111 tuned whitespace;
wenzelm
parents: 79691
diff changeset
   405
                  using(database_context.open_database(session_name, output = true))(
3f307faf9111 tuned whitespace;
wenzelm
parents: 79691
diff changeset
   406
                    session_database =>
3f307faf9111 tuned whitespace;
wenzelm
parents: 79691
diff changeset
   407
                      documents.foreach(_.write(session_database.db, session_name)))
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   408
                  (documents.flatMap(_.log_lines), Nil)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   409
                }
77484
7ba474a01249 tuned names;
wenzelm
parents: 77477
diff changeset
   410
              }
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   411
              else (Nil, Nil)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   412
            }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   413
            catch {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   414
              case exn: Document_Build.Build_Error => (exn.log_lines, exn.log_errors)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   415
              case Exn.Interrupt.ERROR(msg) => (Nil, List(msg))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   416
            }
77485
911d3dbf2033 clarified timeout: closer to actual process;
wenzelm
parents: 77484
diff changeset
   417
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77485
diff changeset
   418
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   419
          /* process result */
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77485
diff changeset
   420
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   421
          val result1 = {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   422
            val theory_timing =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   423
              theory_timings.iterator.flatMap(
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   424
                {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   425
                  case props @ Markup.Name(name) => Some(name -> props)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   426
                  case _ => None
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   427
                }).toMap
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   428
            val used_theory_timings =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   429
              for { (name, _) <- session_background.base.used_theories }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   430
                yield theory_timing.getOrElse(name.theory, Markup.Name(name.theory))
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77485
diff changeset
   431
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   432
            val more_output =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   433
              Library.trim_line(stdout.toString) ::
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   434
                command_timings.toList.map(Protocol.Command_Timing_Marker.apply) :::
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   435
                used_theory_timings.map(Protocol.Theory_Timing_Marker.apply) :::
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   436
                session_timings.toList.map(Protocol.Session_Timing_Marker.apply) :::
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   437
                runtime_statistics.toList.map(Protocol.ML_Statistics_Marker.apply) :::
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   438
                task_statistics.toList.map(Protocol.Task_Statistics_Marker.apply) :::
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   439
                document_output
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77485
diff changeset
   440
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   441
            result0.output(more_output)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   442
              .error(Library.trim_line(stderr.toString))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   443
              .errors_rc(export_errors ::: document_errors)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   444
          }
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77485
diff changeset
   445
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   446
          val result2 =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   447
            build_errors match {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   448
              case Exn.Res(build_errs) =>
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   449
                val errs = build_errs ::: document_errors
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   450
                if (errs.nonEmpty) {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   451
                  result1.error_rc.output(
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   452
                    errs.flatMap(s => split_lines(Output.error_message_text(s))) :::
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   453
                      errs.map(Protocol.Error_Message_Marker.apply))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   454
                }
79692
3f307faf9111 tuned whitespace;
wenzelm
parents: 79691
diff changeset
   455
                else if (progress.stopped && result1.ok) {
3f307faf9111 tuned whitespace;
wenzelm
parents: 79691
diff changeset
   456
                  result1.copy(rc = Process_Result.RC.interrupt)
3f307faf9111 tuned whitespace;
wenzelm
parents: 79691
diff changeset
   457
                }
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   458
                else result1
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   459
              case Exn.Exn(Exn.Interrupt()) =>
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   460
                if (result1.ok) result1.copy(rc = Process_Result.RC.interrupt)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   461
                else result1
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   462
              case Exn.Exn(exn) => throw exn
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   463
            }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   464
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   465
          val process_result =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   466
            if (result2.ok) result2
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   467
            else if (was_timeout) result2.error(Output.error_message_text("Timeout")).timeout_rc
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   468
            else if (result2.interrupted) result2.error(Output.error_message_text("Interrupt"))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   469
            else result2
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   470
79691
d298c5b65d8e clarified store_session: heap requires process_result.ok, but log_db is always stored;
wenzelm
parents: 79682
diff changeset
   471
          val store_session =
d298c5b65d8e clarified store_session: heap requires process_result.ok, but log_db is always stored;
wenzelm
parents: 79682
diff changeset
   472
            store.output_session(session_name, store_heap = process_result.ok && store_heap)
d298c5b65d8e clarified store_session: heap requires process_result.ok, but log_db is always stored;
wenzelm
parents: 79682
diff changeset
   473
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   474
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   475
          /* output heap */
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77485
diff changeset
   476
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79678
diff changeset
   477
          val output_shasum =
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79678
diff changeset
   478
            store_session.heap match {
79691
d298c5b65d8e clarified store_session: heap requires process_result.ok, but log_db is always stored;
wenzelm
parents: 79682
diff changeset
   479
              case Some(path) => SHA1.shasum(ML_Heap.write_file_digest(path), session_name)
d298c5b65d8e clarified store_session: heap requires process_result.ok, but log_db is always stored;
wenzelm
parents: 79682
diff changeset
   480
              case None => SHA1.no_shasum
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   481
            }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   482
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   483
          val log_lines = process_result.out_lines.filterNot(Protocol_Message.Marker.test)
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77485
diff changeset
   484
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   485
          val build_log =
78985
24e686fe043e clarified Log_File.cache: reuse existing Store.cache / Build_Log.Store.cache;
wenzelm
parents: 78842
diff changeset
   486
            Build_Log.Log_File(session_name, process_result.out_lines, cache = store.cache).
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   487
              parse_session_info(
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   488
                command_timings = true,
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   489
                theory_timings = true,
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   490
                ml_statistics = true,
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   491
                task_statistics = true)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   492
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   493
          // write log file
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   494
          if (process_result.ok) {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   495
            File.write_gzip(store.output_log_gz(session_name), terminate_lines(log_lines))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   496
          }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   497
          else File.write(store.output_log(session_name), terminate_lines(log_lines))
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77485
diff changeset
   498
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   499
          // write database
78374
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
   500
          def write_info(db: SQL.Database): Unit =
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   501
            store.write_session_info(db, session_name, session_sources,
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   502
              build_log =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   503
                if (process_result.timeout) build_log.error("Timeout") else build_log,
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   504
              build =
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   505
                Store.Build_Info(
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   506
                  sources = sources_shasum,
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   507
                  input_heaps = input_shasum,
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   508
                  output_heap = output_shasum,
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   509
                  process_result.rc,
78374
f9f1412ea24e reuse database_server connection;
wenzelm
parents: 78372
diff changeset
   510
                  build_context.build_uuid))
78529
0e79fa88cab6 build_worker is stopped independently from master build_process;
wenzelm
parents: 78435
diff changeset
   511
79887
17220dc05991 revert most parts of 0e79fa88cab6: somewhat ambitious attempt to move towards "editing" builds via added/canceled workers;
wenzelm
parents: 79718
diff changeset
   512
          database_server match {
17220dc05991 revert most parts of 0e79fa88cab6: somewhat ambitious attempt to move towards "editing" builds via added/canceled workers;
wenzelm
parents: 79718
diff changeset
   513
            case Some(db) => write_info(db)
17220dc05991 revert most parts of 0e79fa88cab6: somewhat ambitious attempt to move towards "editing" builds via added/canceled workers;
wenzelm
parents: 79718
diff changeset
   514
            case None => using(store.open_database(session_name, output = true))(write_info)
17220dc05991 revert most parts of 0e79fa88cab6: somewhat ambitious attempt to move towards "editing" builds via added/canceled workers;
wenzelm
parents: 79718
diff changeset
   515
          }
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   516
80121
05cec0a3c63d clarified modules and options (from store);
wenzelm
parents: 80120
diff changeset
   517
          store.in_heaps_database(
05cec0a3c63d clarified modules and options (from store);
wenzelm
parents: 80120
diff changeset
   518
            List(store_session), database_server, server = server, progress = progress)
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79678
diff changeset
   519
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   520
          // messages
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   521
          process_result.err_lines.foreach(progress.echo(_))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   522
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   523
          if (process_result.ok) {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   524
            val props = build_log.session_timing
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   525
            val threads = Markup.Session_Timing.Threads.unapply(props) getOrElse 1
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   526
            val timing = Markup.Timing_Properties.get(props)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   527
            progress.echo(
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   528
              "Timing " + session_name + " (" + threads + " threads, " + timing.message_factor + ")",
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   529
              verbose = true)
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   530
            progress.echo(
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   531
              "Finished " + session_name + " (" + process_result.timing.message_resources + ")")
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   532
          }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   533
          else {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   534
            progress.echo(
79692
3f307faf9111 tuned whitespace;
wenzelm
parents: 79691
diff changeset
   535
              session_name + " FAILED (see also \"isabelle build_log -H Error " +
3f307faf9111 tuned whitespace;
wenzelm
parents: 79691
diff changeset
   536
              session_name + "\")")
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   537
            if (!process_result.interrupted) {
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   538
              val tail = info.options.int("process_output_tail")
79692
3f307faf9111 tuned whitespace;
wenzelm
parents: 79691
diff changeset
   539
              val suffix =
3f307faf9111 tuned whitespace;
wenzelm
parents: 79691
diff changeset
   540
                if (tail == 0) log_lines else log_lines.drop(log_lines.length - tail max 0)
3f307faf9111 tuned whitespace;
wenzelm
parents: 79691
diff changeset
   541
              val prefix =
3f307faf9111 tuned whitespace;
wenzelm
parents: 79691
diff changeset
   542
                if (log_lines.length == suffix.length) Nil else List("...")
78372
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   543
              progress.echo(Library.trim_line(cat_lines(prefix ::: suffix)))
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   544
            }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   545
          }
30d3faa6c245 reuse SSH.Server connection database server;
wenzelm
parents: 78359
diff changeset
   546
79887
17220dc05991 revert most parts of 0e79fa88cab6: somewhat ambitious attempt to move towards "editing" builds via added/canceled workers;
wenzelm
parents: 79718
diff changeset
   547
          Result(process_result.copy(out_lines = log_lines), output_shasum)
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77485
diff changeset
   548
        }
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   549
      }
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   550
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77485
diff changeset
   551
    override def cancel(): Unit = future_result.cancel()
77298
39c7d79ace34 clarified signature defaults;
wenzelm
parents: 77297
diff changeset
   552
    override def is_finished: Boolean = future_result.is_finished
79887
17220dc05991 revert most parts of 0e79fa88cab6: somewhat ambitious attempt to move towards "editing" builds via added/canceled workers;
wenzelm
parents: 79718
diff changeset
   553
    override def join: Result = future_result.join
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77294
diff changeset
   554
  }
72848
d5d0e36eda16 read theory with PIDE markup from session database;
wenzelm
parents: 72844
diff changeset
   555
}