Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix processing of scheduled sheet coupon assignment tasks #2565

Merged
merged 3 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions sheets/tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Sheets app tasks"""
import json
import logging
from datetime import datetime, timedelta
from itertools import chain, repeat
Expand Down Expand Up @@ -112,15 +111,11 @@ def _get_scheduled_assignment_task_ids(file_id):
# provided the given file_id as a kwarg, add its task id to the list
for scheduled in chain.from_iterable(app.control.inspect().scheduled().values()):
task_metadata = scheduled["request"]
if task_metadata["name"] == task_name:
# NOTE: Celery provides metadata for scheduled tasks, and the args/kwargs passed to
# that task are stored as serialized strings (e.g.: "{'file_id': '123'}"). Here the kwargs
# are being parsed as JSON after coercing the string to use double-quotes.
task_kwargs = json.loads(
task_metadata.get("kwargs", "{}").replace("'", '"')
)
if task_kwargs.get("file_id") == file_id:
already_scheduled_task_ids.append(task_metadata["id"])
if (
task_metadata["name"] == task_name
and task_metadata.get("kwargs", {}).get("file_id") == file_id
):
already_scheduled_task_ids.append(task_metadata["id"])
return already_scheduled_task_ids


Expand Down
39 changes: 38 additions & 1 deletion sheets/tasks_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
"""Tests for sheets app views"""
from sheets.tasks import handle_unprocessed_coupon_requests
import pytest

from sheets.tasks import (
handle_unprocessed_coupon_requests,
_get_scheduled_assignment_task_ids,
)


def test_handle_unprocessed_coupon_requests(mocker):
Expand All @@ -11,3 +16,35 @@ def test_handle_unprocessed_coupon_requests(mocker):
)
handle_unprocessed_coupon_requests.delay()
patched_req_handler.return_value.process_sheet.assert_called_once()


@pytest.mark.parametrize("same_name", [True, False])
@pytest.mark.parametrize("same_id", [True, False])
def test_get_scheduled_assignment_task_ids(mocker, same_name, same_id):
"""The expected task ids should be returned"""
mock_app_control = mocker.patch("sheets.tasks.app.control")
file_id = "correct_file_id" if same_id else "different_id"
task_name = (
"sheets.tasks.process_coupon_assignment_sheet"
if same_name
else "sheets.tasks.other_sheet"
)
task_id = "123-456-7890"
mock_app_control.inspect.return_value.scheduled.return_value.values.return_value = [
[
{
"request": {
"id": task_id,
"name": task_name,
"args": [],
"kwargs": {
"file_id": file_id,
"change_date": "2023-02-06T21:35:11.280503+00:00",
},
}
}
]
]
assert _get_scheduled_assignment_task_ids("correct_file_id") == (
[task_id] if (same_name and same_id) else []
)