Skip to content

Commit f7dcfee

Browse files
committed
Add task cancel method
1 parent 3daa25b commit f7dcfee

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

libs/labelbox/src/labelbox/client.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,3 +2339,26 @@ def get_task_by_id(self, task_id: str) -> Union[Task, DataUpsertTask]:
23392339

23402340
task._user = user
23412341
return task
2342+
2343+
def cancel_task(self, task_id: str) -> bool:
2344+
"""
2345+
Cancels a task with the given ID.
2346+
2347+
Args:
2348+
task_id (str): The ID of the task to cancel.
2349+
2350+
Returns:
2351+
bool: True if the task was successfully cancelled.
2352+
2353+
Raises:
2354+
LabelboxError: If the task could not be cancelled.
2355+
"""
2356+
mutation_str = """
2357+
mutation CancelTaskPyApi($id: ID!) {
2358+
cancelBulkOperationJob(id: $id) {
2359+
success
2360+
}
2361+
}
2362+
"""
2363+
res = self.execute(mutation_str, {"id": task_id})
2364+
return res["cancelBulkOperationJob"]["success"]

libs/labelbox/tests/data/export/streamable/test_export_data_rows_streamable.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import time
22

3-
43
from labelbox import DataRow, ExportTask, StreamType
54

65

@@ -117,3 +116,25 @@ def test_with_invalid_id(self, client):
117116
assert (
118117
export_task.get_total_lines(stream_type=StreamType.RESULT) is None
119118
)
119+
120+
def test_cancel_export_task(
121+
self, client, data_row, wait_for_data_row_processing
122+
):
123+
data_row = wait_for_data_row_processing(client, data_row)
124+
time.sleep(7) # temp fix for ES indexing delay
125+
export_task = DataRow.export(
126+
client=client,
127+
data_rows=[data_row],
128+
task_name="TestExportDataRow:test_cancel_export_task",
129+
)
130+
131+
# Cancel the task before it completes
132+
success = client.cancel_task(export_task.uid)
133+
assert success is True
134+
135+
# Wait a bit for the cancellation to take effect
136+
time.sleep(2)
137+
138+
# Verify the task was cancelled
139+
cancelled_task = client.get_task_by_id(export_task.uid)
140+
assert cancelled_task.status == "CANCELLED"

0 commit comments

Comments
 (0)