+def ShowJobs(opts, args):
+ """Show detailed information about jobs.
+
+ @param opts: the command line options selected by the user
+ @type args: list
+ @param args: should contain the job IDs to be queried
+ @rtype: int
+ @return: the desired exit code
+
+ """
+ def format_msg(level, text):
+ """Display the text indented."""
+ ToStdout("%s%s", " " * level, text)
+
+ def result_helper(value):
+ """Format a result field in a nice way."""
+ if isinstance(value, (tuple, list)):
+ return "[%s]" % utils.CommaJoin(value)
+ else:
+ return str(value)
+
+ selected_fields = [
+ "id", "status", "ops", "opresult", "opstatus", "oplog",
+ "opstart", "opexec", "opend", "received_ts", "start_ts", "end_ts",
+ ]
+
+ result = GetClient().QueryJobs(args, selected_fields)
+
+ first = True
+
+ for idx, entry in enumerate(result):
+ if not first:
+ format_msg(0, "")
+ else:
+ first = False
+
+ if entry is None:
+ if idx <= len(args):
+ format_msg(0, "Job ID %s not found" % args[idx])
+ else:
+ # this should not happen, when we don't pass args it will be a
+ # valid job returned
+ format_msg(0, "Job ID requested as argument %s not found" % (idx + 1))
+ continue
+
+ (job_id, status, ops, opresult, opstatus, oplog,
+ opstart, opexec, opend, recv_ts, start_ts, end_ts) = entry
+ format_msg(0, "Job ID: %s" % job_id)
+ if status in _USER_JOB_STATUS:
+ status = _USER_JOB_STATUS[status]
+ else:
+ raise errors.ProgrammerError("Unknown job status code '%s'" % status)
+
+ format_msg(1, "Status: %s" % status)
+
+ if recv_ts is not None:
+ format_msg(1, "Received: %s" % FormatTimestamp(recv_ts))
+ else:
+ format_msg(1, "Missing received timestamp (%s)" % str(recv_ts))
+
+ if start_ts is not None:
+ if recv_ts is not None:
+ d1 = start_ts[0] - recv_ts[0] + (start_ts[1] - recv_ts[1]) / 1000000.0
+ delta = " (delta %.6fs)" % d1
+ else:
+ delta = ""
+ format_msg(1, "Processing start: %s%s" %
+ (FormatTimestamp(start_ts), delta))
+ else:
+ format_msg(1, "Processing start: unknown (%s)" % str(start_ts))
+
+ if end_ts is not None:
+ if start_ts is not None:
+ d2 = end_ts[0] - start_ts[0] + (end_ts[1] - start_ts[1]) / 1000000.0
+ delta = " (delta %.6fs)" % d2
+ else:
+ delta = ""
+ format_msg(1, "Processing end: %s%s" %
+ (FormatTimestamp(end_ts), delta))
+ else:
+ format_msg(1, "Processing end: unknown (%s)" % str(end_ts))
+
+ if end_ts is not None and recv_ts is not None:
+ d3 = end_ts[0] - recv_ts[0] + (end_ts[1] - recv_ts[1]) / 1000000.0
+ format_msg(1, "Total processing time: %.6f seconds" % d3)
+ else:
+ format_msg(1, "Total processing time: N/A")
+ format_msg(1, "Opcodes:")
+ for (opcode, result, status, log, s_ts, x_ts, e_ts) in \
+ zip(ops, opresult, opstatus, oplog, opstart, opexec, opend):
+ format_msg(2, "%s" % opcode["OP_ID"])
+ format_msg(3, "Status: %s" % status)
+ if isinstance(s_ts, (tuple, list)):
+ format_msg(3, "Processing start: %s" % FormatTimestamp(s_ts))
+ else:
+ format_msg(3, "No processing start time")
+ if isinstance(x_ts, (tuple, list)):
+ format_msg(3, "Execution start: %s" % FormatTimestamp(x_ts))
+ else:
+ format_msg(3, "No execution start time")
+ if isinstance(e_ts, (tuple, list)):
+ format_msg(3, "Processing end: %s" % FormatTimestamp(e_ts))
+ else:
+ format_msg(3, "No processing end time")
+ format_msg(3, "Input fields:")
+ for key, val in opcode.iteritems():
+ if key == "OP_ID":
+ continue
+ if isinstance(val, (tuple, list)):
+ val = ",".join([str(item) for item in val])
+ format_msg(4, "%s: %s" % (key, val))
+ if result is None:
+ format_msg(3, "No output data")
+ elif isinstance(result, (tuple, list)):
+ if not result:
+ format_msg(3, "Result: empty sequence")
+ else:
+ format_msg(3, "Result:")
+ for elem in result:
+ format_msg(4, result_helper(elem))
+ elif isinstance(result, dict):
+ if not result:
+ format_msg(3, "Result: empty dictionary")
+ else:
+ for key, val in result.iteritems():
+ format_msg(4, "%s: %s" % (key, result_helper(val)))
+ else:
+ format_msg(3, "Result: %s" % result)
+ format_msg(3, "Execution log:")
+ for serial, log_ts, log_type, log_msg in log:
+ time_txt = FormatTimestamp(log_ts)
+ encoded = FormatLogMessage(log_type, log_msg)
+ format_msg(4, "%s:%s:%s %s" % (serial, time_txt, log_type, encoded))
+ return 0
+
+
+def WatchJob(opts, args):
+ """Follow a job and print its output as it arrives.
+
+ @param opts: the command line options selected by the user
+ @type args: list
+ @param args: Contains the job ID
+ @rtype: int
+ @return: the desired exit code
+
+ """
+ job_id = args[0]
+
+ msg = ("Output from job %s follows" % job_id)
+ ToStdout(msg)
+ ToStdout("-" * len(msg))
+
+ retcode = 0
+ try:
+ cli.PollJob(job_id)
+ except errors.GenericError, err:
+ (retcode, job_result) = cli.FormatError(err)
+ ToStderr("Job %s failed: %s", job_id, job_result)
+
+ return retcode
+
+