author | wenzelm |
Thu, 13 Oct 2016 17:34:39 +0200 | |
changeset 64195 | 290b8ba96ecc |
parent 64194 | b5ada7dcceaa |
child 64197 | c43dedbb8118 |
permissions | -rw-r--r-- |
64148 | 1 |
/* Title: Pure/Admin/isabelle_cronjob.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Main entry point for administrative cronjob at TUM. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
64170 | 10 |
import scala.annotation.tailrec |
11 |
import scala.collection.mutable |
|
12 |
||
13 |
||
64148 | 14 |
object Isabelle_Cronjob |
15 |
{ |
|
64194
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
16 |
/* file-system state: owned by main cronjob */ |
64153
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_dir = Path.explode("~/cronjob") |
64194
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
19 |
val main_state_file = main_dir + Path.explode("run/main.state") |
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
20 |
val main_log = main_dir + Path.explode("log/main.log") // owned by log service |
64153
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
21 |
|
64194
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
22 |
val isabelle_repos = main_dir + Path.explode("isabelle-build_history") |
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
23 |
val afp_repos = main_dir + Path.explode("AFP-build_history") |
64153
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 |
|
64192 | 26 |
|
27 |
/** particular tasks **/ |
|
28 |
||
64194
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
29 |
/* identify Isabelle + AFP repository snapshots */ |
64192 | 30 |
|
64194
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
31 |
private def pull_repos(root: Path): String = |
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
32 |
{ |
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
33 |
val hg = Mercurial.repository(root) |
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
34 |
hg.pull(options = "-q") |
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
35 |
hg.identify("tip", options = "-i") |
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
36 |
} |
64192 | 37 |
|
64194
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
38 |
private val isabelle_identify = |
64192 | 39 |
Logger_Task("isabelle_identify", logger => |
40 |
{ |
|
41 |
val isabelle_id = pull_repos(isabelle_repos) |
|
42 |
val afp_id = pull_repos(afp_repos) |
|
43 |
||
64195 | 44 |
File.write(logger.log_dir + Build_Log.log_filename("isabelle_identify", logger.start_date), |
64192 | 45 |
terminate_lines( |
46 |
List("isabelle_identify: " + Build_Log.print_date(logger.start_date), |
|
47 |
"", |
|
48 |
"Isabelle version: " + isabelle_id, |
|
49 |
"AFP version: " + afp_id))) |
|
50 |
}) |
|
51 |
||
52 |
||
64194
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
53 |
/* integrity test of build_history vs. build_history_base */ |
64193 | 54 |
|
64194
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
55 |
private val build_history_base = |
64193 | 56 |
Logger_Task("build_history_base", logger => |
57 |
{ |
|
64194
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
58 |
for { |
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
59 |
(result, log_path) <- |
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
60 |
Build_History.build_history(Mercurial.repository(isabelle_repos), |
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
61 |
rev = "build_history_base", fresh = true, build_args = List("FOL")) |
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
62 |
} { |
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
63 |
result.check |
64195 | 64 |
File.copy(log_path, logger.log_dir + log_path.base) |
64194
b5ada7dcceaa
integrity test of build_history vs. build_history_base;
wenzelm
parents:
64193
diff
changeset
|
65 |
} |
64193 | 66 |
}) |
67 |
||
68 |
||
64192 | 69 |
|
70 |
/** task logging **/ |
|
64171 | 71 |
|
64193 | 72 |
sealed case class Logger_Task(name: String = "", body: Logger => Unit) |
64154 | 73 |
|
64171 | 74 |
class Log_Service private[Isabelle_Cronjob](progress: Progress) |
75 |
{ |
|
76 |
private val thread: Consumer_Thread[String] = |
|
77 |
Consumer_Thread.fork("cronjob: logger", daemon = true)( |
|
78 |
consume = (text: String) => |
|
79 |
{ |
|
80 |
File.append(main_log, text + "\n") // critical |
|
81 |
progress.echo(text) |
|
82 |
true |
|
83 |
}) |
|
84 |
||
85 |
def shutdown() { thread.shutdown() } |
|
86 |
||
87 |
val hostname = Isabelle_System.hostname() |
|
88 |
||
89 |
def log(date: Date, task_name: String, msg: String): Unit = |
|
64193 | 90 |
if (task_name != "") |
91 |
thread.send( |
|
92 |
"[" + Build_Log.print_date(date) + ", " + hostname + ", " + task_name + "]: " + msg) |
|
64171 | 93 |
|
94 |
def start_logger(start_date: Date, task_name: String): Logger = |
|
95 |
new Logger(this, start_date, task_name) |
|
64154 | 96 |
|
64171 | 97 |
def run_task(start_date: Date, task: Logger_Task) |
98 |
{ |
|
99 |
val logger = start_logger(start_date, task.name) |
|
100 |
val res = Exn.capture { task.body(logger) } |
|
101 |
val end_date = Date.now() |
|
102 |
val err = |
|
103 |
res match { |
|
104 |
case Exn.Res(_) => None |
|
105 |
case Exn.Exn(exn) => Some(Exn.message(exn)) |
|
106 |
} |
|
107 |
logger.log_end(end_date, err) |
|
108 |
} |
|
109 |
||
110 |
def fork_task(start_date: Date, task: Logger_Task): Task = |
|
111 |
new Task(task.name, run_task(start_date, task)) |
|
112 |
} |
|
113 |
||
114 |
class Logger private[Isabelle_Cronjob]( |
|
115 |
val log_service: Log_Service, val start_date: Date, val task_name: String) |
|
64162 | 116 |
{ |
64171 | 117 |
def log(date: Date, msg: String): Unit = log_service.log(date, task_name, msg) |
118 |
||
119 |
def log_end(end_date: Date, err: Option[String]) |
|
120 |
{ |
|
121 |
val elapsed_time = end_date.time - start_date.time |
|
122 |
val msg = |
|
123 |
(if (err.isEmpty) "finished" else "ERROR " + err.get) + |
|
124 |
(if (elapsed_time.seconds < 3.0) "" else ", elapsed time " + elapsed_time.message_hms) |
|
125 |
log(end_date, msg) |
|
126 |
} |
|
127 |
||
64195 | 128 |
val log_dir: Path = main_dir + Build_Log.log_subdir(start_date) |
129 |
||
130 |
Isabelle_System.mkdirs(log_dir) |
|
64171 | 131 |
log(start_date, "started") |
132 |
} |
|
133 |
||
134 |
class Task private[Isabelle_Cronjob](name: String, body: => Unit) |
|
135 |
{ |
|
136 |
private val future: Future[Unit] = Future.thread("cronjob: " + name) { body } |
|
137 |
def is_finished: Boolean = future.is_finished |
|
64162 | 138 |
} |
64153
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
139 |
|
64170 | 140 |
|
141 |
||
64153
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
142 |
/** cronjob **/ |
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
143 |
|
64186
49816908ae42
support for separate sub-system options, independent of main Isabelle options;
wenzelm
parents:
64184
diff
changeset
|
144 |
def init_options(): Options = Options.load(Path.explode("~~/Admin/cronjob/cronjob.options")) |
49816908ae42
support for separate sub-system options, independent of main Isabelle options;
wenzelm
parents:
64184
diff
changeset
|
145 |
|
64187 | 146 |
def cronjob(progress: Progress, exclude_task: Set[String]) |
64153
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
147 |
{ |
64171 | 148 |
/* soft lock */ |
64153
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
149 |
|
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
150 |
val still_running = |
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
151 |
try { Some(File.read(main_state_file)) } |
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
152 |
catch { case ERROR(_) => None } |
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
153 |
|
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
154 |
still_running match { |
64170 | 155 |
case None | Some("") => |
64153
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
156 |
case Some(running) => |
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
157 |
error("Isabelle cronjob appears to be still running: " + running) |
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
158 |
} |
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
159 |
|
64171 | 160 |
val main_start_date = Date.now() |
161 |
val log_service = new Log_Service(progress) |
|
64154 | 162 |
|
64171 | 163 |
File.write(main_state_file, main_start_date + " " + log_service.hostname) |
64154 | 164 |
|
165 |
||
64193 | 166 |
/* run tasks */ |
167 |
||
168 |
def run(start_date: Date, task: Logger_Task): Unit = |
|
169 |
log_service.run_task(start_date, task) |
|
64184 | 170 |
|
64193 | 171 |
def run_sequential(tasks: Logger_Task*): Unit = |
172 |
for (task <- tasks.iterator if !exclude_task(task.name) || task.name == "") |
|
173 |
run(Date.now(), task) |
|
64153
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
174 |
|
64193 | 175 |
def run_parallel(tasks: Logger_Task*) |
64170 | 176 |
{ |
64184 | 177 |
@tailrec def join(running: List[Task]) |
64170 | 178 |
{ |
179 |
running.partition(_.is_finished) match { |
|
180 |
case (Nil, Nil) => |
|
64184 | 181 |
case (Nil, _ :: _) => Thread.sleep(500); join(running) |
182 |
case (_ :: _, remaining) => join(remaining) |
|
64170 | 183 |
} |
184 |
} |
|
64187 | 185 |
|
64171 | 186 |
val start_date = Date.now() |
64187 | 187 |
val running = |
64193 | 188 |
for (task <- tasks.toList if !exclude_task(task.name)) |
64187 | 189 |
yield log_service.fork_task(start_date, task) |
190 |
join(running) |
|
64170 | 191 |
} |
64153
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
192 |
|
64193 | 193 |
def SEQ(tasks: Logger_Task*): Logger_Task = |
194 |
Logger_Task(body = _ => run_sequential(tasks:_*)) |
|
195 |
||
196 |
def PAR(tasks: Logger_Task*): Logger_Task = |
|
197 |
Logger_Task(body = _ => run_parallel(tasks:_*)) |
|
198 |
||
64170 | 199 |
|
200 |
/* main */ |
|
201 |
||
64193 | 202 |
run(main_start_date, |
203 |
Logger_Task("isabelle_cronjob", _ => |
|
204 |
run_sequential(isabelle_identify, build_history_base))) |
|
64153
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
205 |
|
64171 | 206 |
log_service.shutdown() |
64170 | 207 |
|
64153
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
208 |
main_state_file.file.delete |
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
209 |
} |
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
210 |
|
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
211 |
|
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
212 |
|
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
213 |
/** command line entry point **/ |
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
214 |
|
64148 | 215 |
def main(args: Array[String]) |
216 |
{ |
|
217 |
Command_Line.tool0 { |
|
218 |
var force = false |
|
219 |
var verbose = false |
|
64187 | 220 |
var exclude_task = Set.empty[String] |
64148 | 221 |
|
222 |
val getopts = Getopts(""" |
|
223 |
Usage: Admin/cronjob/main [OPTIONS] |
|
224 |
||
225 |
Options are: |
|
226 |
-f apply force to do anything |
|
227 |
-v verbose |
|
64187 | 228 |
-x NAME exclude tasks with this name |
64148 | 229 |
""", |
230 |
"f" -> (_ => force = true), |
|
64187 | 231 |
"v" -> (_ => verbose = true), |
232 |
"x:" -> (arg => exclude_task += arg)) |
|
64148 | 233 |
|
234 |
val more_args = getopts(args) |
|
235 |
if (more_args.nonEmpty) getopts.usage() |
|
236 |
||
64153
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
237 |
val progress = if (verbose) new Console_Progress() else Ignore_Progress |
64148 | 238 |
|
64187 | 239 |
if (force) cronjob(progress, exclude_task) |
64153
769791954872
some timing and logging, similar to old isatest.log;
wenzelm
parents:
64148
diff
changeset
|
240 |
else error("Need to apply force to do anything") |
64148 | 241 |
} |
242 |
} |
|
243 |
} |