-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtaskManagerLib.py
More file actions
374 lines (312 loc) · 13.3 KB
/
taskManagerLib.py
File metadata and controls
374 lines (312 loc) · 13.3 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
"""
Monitor and manage tasks programatically
geeViz.taskManagerLib facilitates the monitoring and deleting of GEE export tasks.
"""
"""
Copyright 2026 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
# ------------------------------------------------------------------------------
def now():
"""Get the current time as a formatted string.
Returns:
str: Current time in "%Y-%m-%d %H:%M:%S" format.
Example:
>>> now()
'2025-06-01 12:34:56'
"""
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
def trackTasks():
"""Track and print the number of ready and running tasks every 10 seconds, up to 1000 times.
Example:
>>> trackTasks()
2 tasks ready 2025-06-01 12:34:56
1 tasks running 2025-06-01 12:34:56
Running names:
['ExportImage', '0:01:23']
...
"""
x = 1
while x < 1000:
tasks = ee.data.getTaskList() # Get all current tasks
ready = [i for i in tasks if i["state"] == "READY"] # Tasks waiting to run
running = [i for i in tasks if i["state"] == "RUNNING"] # Tasks currently running
running_names = [
[
str(i["description"]),
str(timedelta(seconds=int(((time.time() * 1000) - int(i["start_timestamp_ms"])) / 1000))),
]
for i in running
] # List of running task descriptions and their run times
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) # Wait 10 seconds before next check
x += 1
def trackTasks2(credential_name=None, id_list=None, task_count=1):
"""Track tasks for a specific credential or list of task IDs, printing status every 5 seconds.
Args:
credential_name (str, optional): Name of the credential to print.
id_list (list, optional): List of task descriptions to filter.
task_count (int, optional): Initial task count to start tracking.
Example:
>>> trackTasks2(credential_name='user', id_list=['ExportImage'], task_count=1)
user
1 tasks ready 2025-06-01 12:34:56
0 tasks running 2025-06-01 12:34:56
Running names:
...
"""
while task_count > 0:
tasks = ee.data.getTaskList()
if id_list != None:
tasks = [i for i in tasks if i["description"] in id_list] # Filter by provided IDs
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) # Wait 5 seconds before next check
task_count = len(ready) + len(running) # Continue until no ready or running tasks
def getTasks(id_list=None):
"""Get a dictionary of tasks grouped by their state.
Args:
id_list (list, optional): List of task descriptions to filter.
Returns:
dict: Dictionary with keys 'ready', 'running', 'failed', 'completed'.
Example:
>>> getTasks()
{'ready': ['ExportImage'], 'running': [], 'failed': [], 'completed': ['ExportTable']}
"""
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
def failedTasks():
"""Print all failed tasks and their run times.
Example:
>>> failedTasks()
Failed names:
{'id': 'ABCD', 'state': 'FAILED', ...}
...
"""
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
# ------------------------------------------------------------------------------
def batchCancel():
"""Cancel all tasks that are either ready or running.
Example:
>>> batchCancel()
Cancelling: ExportImage
READY: ExportImage
...
"""
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"])
def cancelByName(nameIdentifier):
"""Cancel all tasks whose description contains the given identifier.
Args:
nameIdentifier (str): Substring to search for in task descriptions.
Example:
>>> cancelByName('ExportImage')
Cancelling ExportImage
...
"""
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
# ------------------------------------------------------------------------------
def timeTaskList(starttime, endtime):
"""Get list of tasks started within a specified time interval.
Args:
starttime (datetime): UTC datetime object for interval start.
endtime (datetime): UTC datetime object for interval end.
Returns:
list: List of task dicts started within the interval.
Example:
>>> from datetime import datetime, timedelta
>>> start = datetime.utcnow() - timedelta(hours=1)
>>> end = datetime.utcnow()
>>> timeTaskList(start, end)
[{'id': 'ABCD', ...}, ...]
"""
tasks = ee.data.getTaskList()
epoch = datetime.utcfromtimestamp(0)
starttime = (starttime - epoch).total_seconds() * 1000.0 # Convert to ms since epoch
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
def jobCompletionTracker(starttime, endtime, check_interval):
"""Track completion of all jobs started within a time interval.
Args:
starttime (datetime): UTC datetime object for interval start.
endtime (datetime): UTC datetime object for interval end.
check_interval (int): Seconds between status checks.
Returns:
list: Lists of ready, running, completed, failed, cancelled, and all tasks.
Example:
>>> from datetime import datetime, timedelta
>>> start = datetime.utcnow() - timedelta(hours=1)
>>> end = datetime.utcnow()
>>> jobCompletionTracker(start, end, 10)
...
"""
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
# ------------------------------------------------------------------------------
def nameTaskList(nameIdentifier):
"""Get list of tasks with specified name/description prefix.
Args:
nameIdentifier (str): Substring to search for in task descriptions.
Returns:
list: List of matching task dicts.
Example:
>>> nameTaskList('ExportImage')
[{'id': 'ABCD', ...}, ...]
"""
tasks = ee.data.getTaskList()
# Find tasks whose description matches the identifier and are ready or running
thisList = [i for i in tasks if re.findall(nameIdentifier, i["description"]) and (i["state"] == "READY" or i["state"] == "RUNNING")]
return thisList
def getEECUS(output_table_name, nameFind=None, overwrite=False):
"""Create a CSV table of successful exports, including run time, EECU usage, and output size.
Args:
output_table_name (str): Path to output CSV file.
nameFind (str, optional): Substring to filter task descriptions.
overwrite (bool, optional): Overwrite existing file if True.
Example:
>>> getEECUS('output.csv', nameFind='ExportImage', overwrite=True)
Only output extension allowed is .csv. Changing extension to .csv
...
"""
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 # Convert bytes to megabytes
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")