-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(notifications): u#3965 add a periodic task and a command to dele…
…te read notifications
- Loading branch information
Showing
11 changed files
with
188 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# -*- coding: utf-8 -*- | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
# | ||
# Copyright (c) 2023-present Kaleidos INC | ||
|
||
from pydantic import BaseSettings | ||
|
||
|
||
class NotificationsSettings(BaseSettings): | ||
CLEAN_READ_NOTIFICATIONS_CRON: str = "30 * * * *" # default: every hour at minute 30. | ||
MINUTES_TO_STORE_READ_NOTIFICATIONS: int = 2 * 60 # 120 minutes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# -*- coding: utf-8 -*- | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
# | ||
# Copyright (c) 2023-present Kaleidos INC | ||
|
||
from datetime import timedelta | ||
|
||
import typer | ||
from taiga.base.utils import pprint | ||
from taiga.base.utils.concurrency import run_async_as_sync | ||
from taiga.base.utils.datetime import aware_utcnow | ||
from taiga.conf import settings | ||
from taiga.notifications import services as notifications_services | ||
|
||
cli = typer.Typer( | ||
name="Taiga Notifications commands", | ||
help="Manage the notifications system of Taiga.", | ||
add_completion=True, | ||
) | ||
|
||
|
||
@cli.command(help="Clean read notifications. Remove entries from DB.") | ||
def clean_read_notifications( | ||
minutes_to_store_read_notifications: int = typer.Option( | ||
settings.NOTIFICATIONS.MINUTES_TO_STORE_READ_NOTIFICATIONS, | ||
"--minutes", | ||
"-m", | ||
help="Delete all notification read before the specified minutes", | ||
), | ||
) -> None: | ||
total_deleted = run_async_as_sync( | ||
notifications_services.clean_read_notifications( | ||
before=aware_utcnow() - timedelta(minutes=minutes_to_store_read_notifications) | ||
) | ||
) | ||
|
||
color = "red" if total_deleted else "white" | ||
pprint.print(f"Deleted [bold][{color}]{total_deleted}[/{color}][/bold] notifications.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# -*- coding: utf-8 -*- | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
# | ||
# Copyright (c) 2023-present Kaleidos INC | ||
|
||
import logging | ||
from datetime import timedelta | ||
|
||
from taiga.base.utils.datetime import aware_utcnow | ||
from taiga.conf import settings | ||
from taiga.notifications import services as notifications_services | ||
from taiga.tasksqueue.manager import manager as tqmanager | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@tqmanager.periodic(cron=settings.NOTIFICATIONS.CLEAN_READ_NOTIFICATIONS_CRON) # type: ignore | ||
@tqmanager.task | ||
async def clean_read_notifications(timestamp: int) -> int: | ||
total_deleted = await notifications_services.clean_read_notifications( | ||
before=aware_utcnow() - timedelta(minutes=settings.NOTIFICATIONS.MINUTES_TO_STORE_READ_NOTIFICATIONS) | ||
) | ||
|
||
logger.info( | ||
"deleted notifications: %s", | ||
total_deleted, | ||
extra={"deleted": total_deleted}, | ||
) | ||
|
||
return total_deleted |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72c6974
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage Report