forked from GoogleCloudPlatform/professional-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_successful_one_time_jobs.py
executable file
·143 lines (105 loc) · 4.23 KB
/
remove_successful_one_time_jobs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
A tool for removing successful one-time jobs.
"""
import json
import logging
import os
import time
from constants.status import STATUS, sts_operation_status_to_table_status
from lib.services import Services
logger = logging.getLogger(__name__)
logger.setLevel(os.environ.get("LOGLEVEL", "INFO").upper())
def determine_if_all_operations_were_successful(job_name: str,
services: Services):
"""
Determines if all operations for a given job were successful.
"""
job_filter = json.dumps({
"project_id": services.bigquery.project,
"job_names": [job_name]
})
request = services.sts.transferOperations().list(
name='transferOperations', filter=job_filter, pageSize=256)
while request is not None:
response = request.execute()
if not response:
break
for operation in response['operations']:
operation_status = operation['metadata']['status']
status = sts_operation_status_to_table_status(operation_status)
if status != STATUS.DONE:
return False
request = services.sts.transferOperations().list_next(
previous_request=request, previous_response=response)
return True
def delete_all_successful_one_time_jobs(services: Services):
"""
Removes all successful one-time jobs.
"""
logger.info('Removing all successful jobs...')
job_filter = json.dumps({"project_id": services.bigquery.project})
request = services.sts.transferJobs().list(filter=job_filter, pageSize=256)
count = 0
while request is not None:
response = request.execute()
if not response:
break
for job in response['transferJobs']:
name = job['name']
schedule = job['schedule']
# determine if one-time operation
for key in ['year', 'month', 'day']:
if schedule['scheduleStartDate'][key] != \
schedule['scheduleEndDate'][key]:
logger.info(f'Skipping non one-time job `{name}`')
continue
if not determine_if_all_operations_were_successful(name, services):
logger.info(
f'Skipping job with non-complete or failed ops: `{name}`')
continue
logger.info(f'Removing `{name}`...')
operation_request_body = {
'projectId': services.bigquery.project,
'transferJob': {
'status': 'DELETED'
}
}
operation_request = services.sts.transferJobs().patch(
jobName=name, body=operation_request_body)
operation_request.execute()
logger.info(f'...removed `{name}`.')
count += 1
# https://cloud.google.com/storage-transfer/quotas
# - Maximum requests per 100 seconds per user: 1000
# - Maximum requests per 100 seconds per project: 1000
# Taking the time between each call into account,
# we can perform an exact count-based delay
if count % 1000 == 0:
logger.debug('Sleeping a bit to avoid quota limit...')
time.sleep(100)
logger.debug('...waking up.')
request = services.sts.transferJobs().list_next(
previous_request=request, previous_response=response)
logger.info(f'...removed all successful jobs. Count: {count}')
def main():
"""
The main function.
"""
services = Services()
delete_all_successful_one_time_jobs(services)
if __name__ == "__main__":
main()