Skip to content

Commit

Permalink
Merge pull request #328 from sanders41/cancel-default
Browse files Browse the repository at this point in the history
Add default cancel parameters
  • Loading branch information
sanders41 committed Nov 20, 2022
2 parents 8fa64d5 + 7544378 commit 0ee2374
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
7 changes: 7 additions & 0 deletions meilisearch_python_async/task.py
Expand Up @@ -25,6 +25,8 @@ async def cancel_tasks(
) -> TaskInfo:
"""Cancel a list of enqueued or processing tasks.
Defaults to cancelling all tasks.
Args:
uids: A list of task UIDs to cancel.
Expand Down Expand Up @@ -72,6 +74,11 @@ async def cancel_tasks(
parameters["beforeStartedAt"] = str(before_started_at)
if after_finished_at:
parameters["afterFinishedAt"] = str(after_finished_at)

if not parameters:
# Cancel all tasks if no parmaeters provided
parameters["statuses"] = "enqueued,processing"

url = f"tasks/cancel?{urlencode(parameters)}"
response = await http_client.post(url)

Expand Down
20 changes: 18 additions & 2 deletions tests/test_task.py
Expand Up @@ -4,6 +4,18 @@
from meilisearch_python_async.task import cancel_tasks, get_task, get_tasks, wait_for_task


async def test_cancel_every_task(test_client):
task = await cancel_tasks(test_client.http_client, statuses=["enqueued", "processing"])
tasks = await get_tasks(test_client.http_client, types="taskCancelation")

assert task.task_uid is not None
assert task.index_uids is None
assert task.status in {"enqueued", "processing", "succeeded"}
assert task.task_type == "taskCancelation"
assert tasks[0].details is not None
assert "statuses=enqueued%2Cprocessing" in tasks[0].details["originalFilter"]


async def test_cancel_tasks(test_client):
task = await cancel_tasks(test_client.http_client, uids=["1", "2"])
tasks = await get_tasks(test_client.http_client, types=["taskCancelation"])
Expand All @@ -16,12 +28,16 @@ async def test_cancel_tasks(test_client):
assert "uids=1%2C2" in tasks[0].details["originalFilter"]


async def test_cancel_every_task(test_client):
task = await cancel_tasks(test_client.http_client, statuses=["enqueued", "processing"])
async def test_cancel_task_no_params(test_client):
task = await cancel_tasks(test_client.http_client)
tasks = await get_tasks(test_client.http_client, types="taskCancelation")

assert task.task_uid is not None
assert task.index_uids is None
assert task.status in {"enqueued", "processing", "succeeded"}
assert task.task_type == "taskCancelation"
assert tasks[0].details is not None
assert "statuses=enqueued%2Cprocessing" in tasks[0].details["originalFilter"]


async def test_get_tasks(empty_index, small_movies):
Expand Down

0 comments on commit 0ee2374

Please sign in to comment.