parallel_emerge: add try_count field to emerge events

BUG=chromium:703431
TEST=none

Change-Id: Ied20176a6c294b430d29c6aa8df42c8a6726f995
Reviewed-on: https://chromium-review.googlesource.com/487872
Commit-Ready: Chris Ching <chingcodes@chromium.org>
Tested-by: Chris Ching <chingcodes@chromium.org>
Reviewed-by: David Riley <davidriley@chromium.org>
diff --git a/scripts/parallel_emerge.py b/scripts/parallel_emerge.py
index f6b5ba3..f364529 100644
--- a/scripts/parallel_emerge.py
+++ b/scripts/parallel_emerge.py
@@ -851,10 +851,10 @@
 
   __slots__ = ["done", "filename", "last_notify_timestamp", "last_output_seek",
                "last_output_timestamp", "pkgname", "retcode", "start_timestamp",
-               "target", "fetch_only", "unpack_only"]
+               "target", "try_count", "fetch_only", "unpack_only"]
 
   def __init__(self, target, pkgname, done, filename, start_timestamp,
-               retcode=None, fetch_only=False, unpack_only=False):
+               retcode=None, fetch_only=False, try_count=0, unpack_only=False):
 
     # The full name of the target we're building (e.g.
     # virtual/target-os-1-r60)
@@ -886,6 +886,9 @@
     # The return code of our job, if the job is actually finished.
     self.retcode = retcode
 
+    # Number of tries for this job
+    self.try_count = try_count
+
     # Was this just a fetch job?
     self.fetch_only = fetch_only
 
@@ -918,12 +921,12 @@
   signal.signal(signal.SIGTERM, ExitHandler)
 
 
-def EmergeProcess(output, target, *args, **kwargs):
+def EmergeProcess(output, job_state, *args, **kwargs):
   """Merge a package in a subprocess.
 
   Args:
     output: Temporary file to write output.
-    target: The package we'll be processing (for display purposes).
+    job_state: Stored state of package
     *args: Arguments to pass to Scheduler constructor.
     **kwargs: Keyword arguments to pass to Scheduler constructor.
 
@@ -931,11 +934,17 @@
     The exit code returned by the subprocess.
   """
 
+  target = job_state.target
+
+  job_state.try_count += 1
+
   cpv = portage_util.SplitCPV(target)
+
   event = cros_event.newEvent(task_name="EmergePackage",
                               name=cpv.package,
                               category=cpv.category,
-                              version=cpv.version)
+                              version=cpv.version,
+                              try_count=job_state.try_count)
   pid = os.fork()
   if pid == 0:
     try:
@@ -973,8 +982,8 @@
 
       # Actually do the merge.
       with event:
-        retval = scheduler.merge()
-        if retval != 0:
+        job_state.retcode = scheduler.merge()
+        if job_state.retcode != 0:
           event.fail(message="non-zero value returned")
 
     # We catch all exceptions here (including SystemExit, KeyboardInterrupt,
@@ -983,12 +992,12 @@
     # pylint: disable=W0702
     except:
       traceback.print_exc(file=output)
-      retval = 1
+      job_state.retcode = 1
     sys.stdout.flush()
     sys.stderr.flush()
     output.flush()
     # pylint: disable=W0212
-    os._exit(retval)
+    os._exit(job_state.retcode)
   else:
     # Return the exit code of the subprocess.
     return os.waitpid(pid, 0)[1]
@@ -1112,27 +1121,28 @@
                          fetch_only=fetch_only, unpack_only=unpack_only)
     job_queue.put(job)
     if "--pretend" in opts:
-      retcode = 0
+      job.retcode = 0
     else:
       try:
         emerge.scheduler_graph.mergelist = install_list
         if unpack_only:
-          retcode = UnpackPackage(pkg_state)
+          job.retcode = UnpackPackage(pkg_state)
         else:
-          retcode = EmergeProcess(output, target, settings, trees, mtimedb,
-                                  opts, spinner, favorites=emerge.favorites,
-                                  graph_config=emerge.scheduler_graph)
+          job.retcode = EmergeProcess(output, job, settings, trees, mtimedb,
+                                      opts, spinner,
+                                      favorites=emerge.favorites,
+                                      graph_config=emerge.scheduler_graph)
       except Exception:
         traceback.print_exc(file=output)
-        retcode = 1
+        job.retcode = 1
       output.close()
 
     if KILLED.is_set():
       return
 
     job = EmergeJobState(target, pkgname, True, output.name, start_timestamp,
-                         retcode, fetch_only=fetch_only,
-                         unpack_only=unpack_only)
+                         job.retcode, fetch_only=fetch_only,
+                         try_count=job.try_count, unpack_only=unpack_only)
     job_queue.put(job)
 
     # Set the title back to idle as the multiprocess pool won't destroy us;