-
Notifications
You must be signed in to change notification settings - Fork 9
/
taskManagerLib.py
255 lines (220 loc) · 10 KB
/
taskManagerLib.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"""
Monitor and manage tasks programatically
geeViz.taskManagerLib facilitates the monitoring and deleting of GEE export tasks.
"""
"""
Copyright 2024 Ian Housman & Leah Campbell
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.
"""
###############################################################################
# TASKMANAGERLIB.PY
################################################################################
# Functions for GEE task management
import geeViz.geeView as gv
import ee, time, re, os
from datetime import datetime, timedelta
# ------------------------------------------------------------------------------
# Standard Task Tracking
# ------------------------------------------------------------------------------
# Standard task tracker - prints number of ready and running tasks each 10 seconds
def now():
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
def trackTasks():
x = 1
while x < 1000:
tasks = ee.data.getTaskList()
ready = [i for i in tasks if i["state"] == "READY"]
running = [i for i in tasks if i["state"] == "RUNNING"]
running_names = [
[
str(i["description"]),
str(timedelta(seconds=int(((time.time() * 1000) - int(i["start_timestamp_ms"])) / 1000))),
]
for i in running
]
now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(len(ready), "tasks ready", now)
print(len(running), "tasks running", now)
print("Running names:")
for rn in running_names:
print(rn)
print()
print()
time.sleep(10)
x += 1
def trackTasks2(credential_name=None, id_list=None, task_count=1):
while task_count > 0:
tasks = ee.data.getTaskList()
if id_list != None:
tasks = [i for i in tasks if i["description"] in id_list]
ready = [i for i in tasks if i["state"] == "READY"]
running = [i for i in tasks if i["state"] == "RUNNING"]
running_names = [
[
str(i["description"]),
str(timedelta(seconds=int(((time.time() * 1000) - int(i["start_timestamp_ms"])) / 1000))),
]
for i in running
]
now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
if credential_name != None:
print(credential_name)
print(len(ready), "tasks ready", now)
print(len(running), "tasks running", now)
print("Running names:")
for rn in running_names:
print(rn)
print()
print()
time.sleep(5)
task_count = len(ready) + len(running)
# Get tasks in an easy-to-use format
def getTasks(id_list=None):
tasks = ee.data.getTaskList()
if id_list != None:
tasks = [i for i in tasks if i["description"] in id_list]
out = {}
out["ready"] = [i["description"] for i in tasks if i["state"] == "READY"]
out["running"] = [i["description"] for i in tasks if i["state"] == "RUNNING"]
out["failed"] = [i["description"] for i in tasks if i["state"] == "FAILED"]
out["completed"] = [i["description"] for i in tasks if i["state"] == "COMPLETED"]
return out
# Standard task tracker - prints number of ready and running tasks each 10 seconds
def failedTasks():
tasks = ee.data.getTaskList()
failed = [i for i in tasks if i["state"] == "FAILED"]
failed_names = [
[
str(i["description"]),
str(timedelta(seconds=int(((time.time() * 1000) - int(i["start_timestamp_ms"])) / 1000))),
]
for i in failed
]
now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print("Failed names:")
for rn in failed:
print(rn)
# ------------------------------------------------------------------------------
# Cancel Tasks
# ------------------------------------------------------------------------------
# Cancels all running tasks
def batchCancel():
tasks = ee.data.getTaskList()
cancelledTasks = []
for ind, i in enumerate(tasks):
if i["state"] == "READY" or i["state"] == "RUNNING":
cancelledTasks.append(ind)
print("Cancelling:", i["description"])
ee.data.cancelTask(i["id"])
# ee.data.cancelOperation(ee._cloud_api_utils.convert_task_id_to_operation_name(i['id'])) # this was the workaround for a bug with cancelTask in earthengine-api v0.1.225
tasks2 = ee.data.getTaskList()
for ind in cancelledTasks:
print(tasks2[ind]["state"] + ": " + tasks2[ind]["description"])
# Cancels all running tasks with identifier in their name
def cancelByName(nameIdentifier):
cancelList = nameTaskList(nameIdentifier)
if cancelList:
for task in cancelList:
print("Cancelling " + task["description"])
ee.data.cancelTask(task["id"])
else:
print("No Tasks to Cancel")
# ------------------------------------------------------------------------------
# Task Tracking Using Time Interval
# ------------------------------------------------------------------------------
# Get list of tasks started within a specified time interval
# Starttime and endtime must be python datetimes, in UTC, e.g. datetime.datetime.utcnow()
def timeTaskList(starttime, endtime):
tasks = ee.data.getTaskList()
epoch = datetime.utcfromtimestamp(0)
starttime = (starttime - epoch).total_seconds() * 1000.0
endtime = (endtime - epoch).total_seconds() * 1000.0
thisList = [i for i in tasks if (i["creation_timestamp_ms"] >= starttime and i["creation_timestamp_ms"] <= endtime)]
return thisList
# Using a start and end time, track the jobs that were started within that time interval
# Print status updates every minute
# Only move on when no 'ready' or 'running' jobs remain, i.e. all have completed or failed
# Then print status of all tasks
# Starttime and endtime must be python datetimes, in UTC, e.g. datetime.datetime.utcnow()
# check_interval = seconds between status checks
def jobCompletionTracker(starttime, endtime, check_interval):
timeStart = datetime.utcnow()
thisTasklist = timeTaskList(starttime, endtime)
for i in thisTasklist:
print(i["description"], i["state"])
currentJobs = 1
while currentJobs > 0:
time.sleep(check_interval)
thisTasklist = timeTaskList(starttime, endtime)
ready = [i for i in thisTasklist if (i["state"] == "READY")]
running = [i for i in thisTasklist if (i["state"] == "RUNNING")]
completed = [i for i in thisTasklist if (i["state"] == "COMPLETED")]
failed = [i for i in thisTasklist if (i["state"] == "FAILED")]
cancelled = [i for i in thisTasklist if (i["state"] == "CANCELLED")]
currentJobs = len(ready) + len(running)
print(" ")
print(datetime.now(), ":")
print(len(ready), " tasks ready")
# print(len(running), ' tasks running')
running_names = [
[
str(i["description"]),
str(timedelta(seconds=int(((time.time() * 1000) - int(i["start_timestamp_ms"])) / 1000))),
]
for i in running
]
for rn in running_names:
print(rn)
timeEnd = datetime.utcnow()
print("Process Time:")
print(timeEnd - timeStart)
return [ready, running, completed, failed, cancelled, thisTasklist]
# ------------------------------------------------------------------------------
# Task Tracking Using Task Name / Description
# ------------------------------------------------------------------------------
# Get list of tasks with specified name/description prefix (e.g. "description" passed to GEE export function)
def nameTaskList(nameIdentifier):
tasks = ee.data.getTaskList()
thisList = [i for i in tasks if re.findall(nameIdentifier, i["description"]) and (i["state"] == "READY" or i["state"] == "RUNNING")]
return thisList
# Function to create a table of successful exports, how long it took, and the EECUs used
def getEECUS(output_table_name, nameFind=None, overwrite=False):
if os.path.splitext(output_table_name)[1] != ".csv":
print("Only output extension allowed is .csv. Changing extension to .csv")
output_table_name = os.path.splitext(output_table_name)[0] + ".csv"
if not os.path.exists(output_table_name) or overwrite:
operations = ee.data.listOperations()
print(ee.data.getTaskList())
completed = [i for i in operations if i["metadata"]["state"] == "SUCCEEDED"]
if nameFind != None:
completed = [i for i in completed if i["metadata"]["description"].find(nameFind) > -1]
output_table_name = os.path.splitext(output_table_name)[0] + ".csv"
out_lines = "Name,Run Time, EECU Seconds,Size (megaBytes)\n"
for task in completed:
try:
uri = task["metadata"]["destinationUris"][0].split("?asset=")[1]
info = ee.data.getAsset(uri)
size = float(info["sizeBytes"]) * 1e-6
except:
size = "NA"
startTime = datetime.fromisoformat(task["metadata"]["startTime"][:-1])
endTime = datetime.fromisoformat(task["metadata"]["endTime"][:-1])
out_lines += "{},{},{},{}\n".format(
task["metadata"]["description"],
endTime - startTime,
task["metadata"]["batchEecuUsageSeconds"],
size,
)
o = open(output_table_name, "w")
o.write(out_lines)
o.close()
else:
print("Output table already exists and overwite is set to False")