Skip to content

Commit 59fbc85

Browse files
authored
Make tasks cancellable (#1957)
1 parent 33c480f commit 59fbc85

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

libs/labelbox/src/labelbox/client.py

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

23902390
task._user = user
23912391
return task
2392+
2393+
def cancel_task(self, task_id: str) -> bool:
2394+
"""
2395+
Cancels a task with the given ID.
2396+
2397+
Args:
2398+
task_id (str): The ID of the task to cancel.
2399+
2400+
Returns:
2401+
bool: True if the task was successfully cancelled.
2402+
2403+
Raises:
2404+
LabelboxError: If the task could not be cancelled.
2405+
"""
2406+
mutation_str = """
2407+
mutation CancelTaskPyApi($id: ID!) {
2408+
cancelBulkOperationJob(id: $id) {
2409+
success
2410+
}
2411+
}
2412+
"""
2413+
res = self.execute(mutation_str, {"id": task_id})
2414+
return res["cancelBulkOperationJob"]["success"]

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

Lines changed: 19 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,22 @@ 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+
# Verify the task was cancelled
136+
cancelled_task = client.get_task_by_id(export_task.uid)
137+
assert cancelled_task.status in ["CANCELING", "CANCELED"]

0 commit comments

Comments
 (0)