Wait for groups to be created before getting uuids
* jeepyb/cmd/manage_projects.py: When it is necessary to create a group
in gerrit, check that the group appears in ls-groups before getting the
group UUID from the database. Otherwise there is a race with
create-group and updating the databse that can result in the UUID not
being present in the DB.
Change-Id: I7733e5550e493ec72d9afe022f1d1426d3f15f83
diff --git a/jeepyb/cmd/manage_projects.py b/jeepyb/cmd/manage_projects.py
index 9828f6b..fa8e055 100644
--- a/jeepyb/cmd/manage_projects.py
+++ b/jeepyb/cmd/manage_projects.py
@@ -53,6 +53,7 @@
import shlex
import subprocess
import tempfile
+import time
import yaml
import gerritlib.gerrit
@@ -168,7 +169,7 @@
return True
-def _get_group_uuid(gerrit, group):
+def _get_group_uuid(group):
cursor = jeepyb.gerritdb.connect().cursor()
query = "SELECT group_uuid FROM account_groups WHERE name = %s"
cursor.execute(query, group)
@@ -178,12 +179,22 @@
return None
+def _wait_for_group(gerrit, group):
+ """Wait for up to 10 seconds for the group to be created."""
+ for x in range(10):
+ groups = gerrit.listGroups()
+ if group in groups:
+ break
+ time.sleep(1)
+
+
def get_group_uuid(gerrit, group):
- uuid = _get_group_uuid(gerrit, group)
+ uuid = _get_group_uuid(group)
if uuid:
return uuid
gerrit.createGroup(group)
- uuid = _get_group_uuid(gerrit, group)
+ _wait_for_group(gerrit, group)
+ uuid = _get_group_uuid(group)
if uuid:
return uuid
return None