cbuildbot: Set task_num differently on reexec

Thanks to the fix to the ConsumeMessages loop in ts_mon_config, we now properly
send out /chrome/infra/presence/up messages every minute from cbuildbot. This
revealed a pre-existing bug whereby cbuildbot would reexec itself, and the
metrics from the parent and child cbuildbot processes would collide with each
other. This can be avoided by setting ts-mon's task_num differently in the
parent and child process.

BUG=chroimum:739526
TEST=sync_stages_unittest passes

Change-Id: I286710b39b63bd420838325b149fa723d551c00e
Reviewed-on: https://chromium-review.googlesource.com/560632
Commit-Ready: Paul Hobbs <phobbs@google.com>
Tested-by: Paul Hobbs <phobbs@google.com>
Reviewed-by: Aviv Keshet <akeshet@chromium.org>
diff --git a/scripts/cbuildbot.py b/scripts/cbuildbot.py
index b1660a3..f0bfbe5 100644
--- a/scripts/cbuildbot.py
+++ b/scripts/cbuildbot.py
@@ -705,6 +705,10 @@
                    help='Used for compatibility checks w/tryjobs running in '
                         'older chromite instances')
   group.add_option('--sourceroot', type='path', default=constants.SOURCE_ROOT)
+  group.add_option('--ts-mon-task-num', type='int', default=0,
+                   help='The task number of this process. Defaults to 0. '
+                        'This argument is useful for running multiple copies '
+                        'of cbuildbot without their metrics colliding.')
   group.add_remote_option('--test-bootstrap', action='store_true',
                           default=False,
                           help='Causes cbuildbot to bootstrap itself twice, '
@@ -1056,7 +1060,8 @@
 
   if run_type == _ENVIRONMENT_PROD:
     cidb.CIDBConnectionFactory.SetupProdCidb()
-    context = ts_mon_config.SetupTsMonGlobalState('cbuildbot', indirect=True)
+    context = ts_mon_config.SetupTsMonGlobalState(
+        'cbuildbot', indirect=True, task_num=options.ts_mon_task_num)
   elif run_type == _ENVIRONMENT_DEBUG:
     cidb.CIDBConnectionFactory.SetupDebugCidb()
     context = ts_mon_config.TrivialContextManager()