Skip to content

Commit

Permalink
🚚(celery) rename celery tasks by adding a _task suffix
Browse files Browse the repository at this point in the history
Renaming `warn_inactive_users` and `delete_inactive_users` tasks to resp.
`warn_inactive_users_task` and  `delete_inactive_users_task` for clarity.
  • Loading branch information
wilbrdt committed Oct 14, 2024
1 parent 905df50 commit 8bfb2fe
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src/app/mork/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from pydantic import BaseModel

from mork.celery.deletion_tasks import delete_inactive_users
from mork.celery.emailing_tasks import warn_inactive_users
from mork.celery.deletion_tasks import delete_inactive_users_task
from mork.celery.emailing_tasks import warn_inactive_users_task


@unique
Expand All @@ -25,8 +25,8 @@ class TaskStatus(str, Enum):
class TaskType(str, Enum):
"""Possible task types."""

EMAILING = "emailing"
DELETION = "deletion"
EMAIL_INACTIVE_USERS = "email_inactive_users"
DELETE_INACTIVE_USERS = "delete_inactive_users"


class TaskCreate(BaseModel):
Expand All @@ -43,6 +43,6 @@ class TaskResponse(BaseModel):


TASK_TYPE_TO_FUNC = {
TaskType.EMAILING: warn_inactive_users,
TaskType.DELETION: delete_inactive_users,
TaskType.EMAIL_INACTIVE_USERS: warn_inactive_users_task,
TaskType.DELETE_INACTIVE_USERS: delete_inactive_users_task,
}
2 changes: 1 addition & 1 deletion src/app/mork/celery/deletion_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


@app.task
def delete_inactive_users():
def delete_inactive_users_task():
"""Celery task to delete inactive users accounts."""
db = OpenEdxDB()
threshold_date = datetime.now() - settings.DELETION_PERIOD
Expand Down
2 changes: 1 addition & 1 deletion src/app/mork/celery/emailing_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


@app.task
def warn_inactive_users():
def warn_inactive_users_task():
"""Celery task to warn inactive users by email."""
db = OpenEdxDB()

Expand Down
7 changes: 5 additions & 2 deletions src/app/mork/tests/api/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def test_tasks_auth(http_client: AsyncClient):


@pytest.mark.anyio
@pytest.mark.parametrize("task_type", ["emailing", "deletion"])
@pytest.mark.parametrize("task_type", ["email_inactive_users", "delete_inactive_users"])
async def test_create_task(
http_client: AsyncClient, auth_headers: dict, task_type: str
):
Expand Down Expand Up @@ -74,7 +74,10 @@ async def test_get_available_tasks(http_client: AsyncClient, auth_headers: dict)
response_data = response.json()
assert response.status_code == 200
assert response.headers["allow"] == "POST"
assert sorted(response_data.get("task_types")) == ["deletion", "emailing"]
assert sorted(response_data.get("task_types")) == [
"delete_inactive_users",
"email_inactive_users",
]


@pytest.mark.anyio
Expand Down
14 changes: 7 additions & 7 deletions src/app/mork/tests/celery/test_deletion_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from mork.celery.deletion_tasks import (
delete_email_status,
delete_inactive_users,
delete_inactive_users_task,
delete_user,
delete_user_task,
)
Expand All @@ -21,8 +21,8 @@
from mork.models import EmailStatus


def test_delete_inactive_users(edx_db, monkeypatch):
"""Test the `delete_inactive_users` function."""
def test_delete_inactive_users_task(edx_db, monkeypatch):
"""Test the `delete_inactive_users_task` function."""
# 2 users that did not log in for 3 years
EdxAuthUserFactory.create(
last_login=Faker().date_time_between(end_date="-3y"),
Expand Down Expand Up @@ -51,7 +51,7 @@ def test_delete_inactive_users(edx_db, monkeypatch):
"mork.celery.deletion_tasks.delete_user_task", mock_delete_user_task
)

delete_inactive_users()
delete_inactive_users_task()

mock_group.assert_called_once_with(
[
Expand All @@ -61,8 +61,8 @@ def test_delete_inactive_users(edx_db, monkeypatch):
)


def test_delete_inactive_users_with_batch_size(edx_db, monkeypatch):
"""Test the `warn_inactive_users` function."""
def test_delete_inactive_users_task_with_batch_size(edx_db, monkeypatch):
"""Test the `warn_inactive_users_task` function."""
# 2 users that did not log in for 3 years
EdxAuthUserFactory.create(
last_login=Faker().date_time_between(end_date="-3y"),
Expand All @@ -85,7 +85,7 @@ def test_delete_inactive_users_with_batch_size(edx_db, monkeypatch):
# Set batch size to 1
monkeypatch.setattr("mork.celery.deletion_tasks.settings.EDX_QUERY_BATCH_SIZE", 1)

delete_inactive_users()
delete_inactive_users_task()

mock_group.assert_has_calls(
[
Expand Down
14 changes: 7 additions & 7 deletions src/app/mork/tests/celery/test_emailing_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
check_email_already_sent,
mark_email_status,
send_email_task,
warn_inactive_users,
warn_inactive_users_task,
)
from mork.edx.factories.auth import EdxAuthUserFactory
from mork.exceptions import EmailAlreadySent, EmailSendError
from mork.factories import EmailStatusFactory


def test_warn_inactive_users(edx_db, monkeypatch):
"""Test the `warn_inactive_users` function."""
def test_warn_inactive_users_task(edx_db, monkeypatch):
"""Test the `warn_inactive_users_task` function."""
# 2 users that did not log in for 3 years
EdxAuthUserFactory.create(
last_login=Faker().date_time_between(end_date="-3y"),
Expand Down Expand Up @@ -50,7 +50,7 @@ def test_warn_inactive_users(edx_db, monkeypatch):
"mork.celery.emailing_tasks.send_email_task", mock_send_email_task
)

warn_inactive_users()
warn_inactive_users_task()

mock_group.assert_called_once_with(
[
Expand All @@ -60,8 +60,8 @@ def test_warn_inactive_users(edx_db, monkeypatch):
)


def test_warn_inactive_users_with_batch_size(edx_db, monkeypatch):
"""Test the `warn_inactive_users` function."""
def test_warn_inactive_users_task_with_batch_size(edx_db, monkeypatch):
"""Test the `warn_inactive_users_task` function."""
# 2 users that did not log in for 3 years
EdxAuthUserFactory.create(
last_login=Faker().date_time_between(end_date="-3y"),
Expand All @@ -86,7 +86,7 @@ def test_warn_inactive_users_with_batch_size(edx_db, monkeypatch):
# Set batch size to 1
monkeypatch.setattr("mork.celery.emailing_tasks.settings.EDX_QUERY_BATCH_SIZE", 1)

warn_inactive_users()
warn_inactive_users_task()

mock_group.assert_has_calls(
[
Expand Down

0 comments on commit 8bfb2fe

Please sign in to comment.