Skip to content

Commit d218629

Browse files
author
Takashi Matsuo
committed
[datalabeling] fix: clean up old datasets before the test
fixes GoogleCloudPlatform#3703
1 parent 2c3bc65 commit d218629

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

datalabeling/manage_dataset_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import backoff
2020
from google.api_core.exceptions import DeadlineExceeded
21+
from google.api_core.exceptions import RetryError
2122
import pytest
2223

2324
import manage_dataset
@@ -40,6 +41,14 @@ def dataset():
4041

4142
@pytest.fixture(scope='module')
4243
def cleaner():
44+
# First delete old datasets.
45+
try:
46+
testing_lib.delete_old_datasets(PROJECT_ID)
47+
# We see occational RetryError while deleting old datasets.
48+
# We can just ignore it and move on.
49+
except RetryError as e:
50+
print("delete_old_datasets failed: detail {}".format(e))
51+
4352
resource_names = []
4453

4554
yield resource_names

datalabeling/testing_lib.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
# limitations under the License.
1414

1515
import os
16+
import time
1617

1718
import backoff
1819
from google.api_core.client_options import ClientOptions
1920
from google.api_core.exceptions import DeadlineExceeded
21+
from google.api_core.exceptions import FailedPrecondition
2022
from google.cloud import datalabeling_v1beta1 as datalabeling
2123

2224
import create_annotation_spec_set as annotation_spec_set_sample
@@ -48,6 +50,27 @@ def delete_dataset(name):
4850
return dataset_sample.delete_dataset(name)
4951

5052

53+
def delete_old_datasets(project_id):
54+
client = create_client()
55+
formatted_project_name = client.project_path(project_id)
56+
57+
response = client.list_datasets(formatted_project_name)
58+
# It will delete datasets created more than 2 hours ago
59+
cutoff_time = time.time() - 7200
60+
for element in response:
61+
if element.create_time.seconds < cutoff_time:
62+
print("Deleting {}".format(element.name))
63+
try:
64+
dataset_sample.delete_dataset(element.name)
65+
except FailedPrecondition as e:
66+
# We're always getting FailedPrecondition with 400
67+
# resource conflict. I don't know why.
68+
print("Deleting {} failed.".format(element.name))
69+
print("Detail: {}".format(e))
70+
# To avoid quota error
71+
time.sleep(1)
72+
73+
5174
@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=RETRY_DEADLINE)
5275
def create_annotation_spec_set(project_id):
5376
return annotation_spec_set_sample.create_annotation_spec_set(project_id)

0 commit comments

Comments
 (0)