|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 | 15 | import os
|
| 16 | +import time |
16 | 17 |
|
17 | 18 | import backoff
|
18 | 19 | from google.api_core.client_options import ClientOptions
|
19 | 20 | from google.api_core.exceptions import DeadlineExceeded
|
| 21 | +from google.api_core.exceptions import FailedPrecondition |
20 | 22 | from google.cloud import datalabeling_v1beta1 as datalabeling
|
21 | 23 |
|
22 | 24 | import create_annotation_spec_set as annotation_spec_set_sample
|
@@ -48,6 +50,27 @@ def delete_dataset(name):
|
48 | 50 | return dataset_sample.delete_dataset(name)
|
49 | 51 |
|
50 | 52 |
|
| 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 | + |
51 | 74 | @backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=RETRY_DEADLINE)
|
52 | 75 | def create_annotation_spec_set(project_id):
|
53 | 76 | return annotation_spec_set_sample.create_annotation_spec_set(project_id)
|
|
0 commit comments