Skip to content

Commit

Permalink
Delete notifications from index after 90 days, fixes alephdata#1746.
Browse files Browse the repository at this point in the history
  • Loading branch information
pudo committed Apr 23, 2021
1 parent ab894be commit 20b4ee7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ api: services
$(COMPOSE) up --abort-on-container-exit api

web: services
$(COMPOSE) up --abort-on-container-exit api ui
$(COMPOSE) up api ui

worker: services
$(COMPOSE) run --rm app aleph worker
Expand Down
6 changes: 3 additions & 3 deletions aleph/index/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def index_notification(event, actor_id, params, channels, sync=False):
return index_safe(index, id_, data, sync=sync)


def delete_notifications(channel, sync=False):
"""Delete entities from a collection."""
query = {"bool": {"filter": [{"term": {"channels": channel}}]}}
def delete_notifications(filter_, sync=False):
"""Delete notifications from a specific channel."""
query = {"bool": {"filter": [filter_]}}
query_delete(notifications_index(), query, sync=sync)
20 changes: 12 additions & 8 deletions aleph/logic/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@
from aleph.model import Event, Events
from aleph.logic.mail import email_role
from aleph.logic.html import html_link
from aleph.logic.util import (
collection_url,
entity_url,
ui_url,
entityset_url,
archive_url,
)
from aleph.logic.util import collection_url, entity_url
from aleph.logic.util import entityset_url, archive_url, ui_url
from aleph.index.notifications import index_notification, delete_notifications
from aleph.index.notifications import notifications_index
from aleph.index.util import unpack_result
Expand All @@ -44,9 +39,18 @@ def publish(event, actor_id=None, params=None, channels=None):
index_notification(event, actor_id, params, channels)


def delete_old_notifications(sync=False):
"""Delete out-dated notifications from the index."""
cutoff = datetime.utcnow() - settings.NOTIFICATIONS_DELETE
filter_ = {"range": {"created_at": {"lt": cutoff}}}
log.debug("Deleting old notifications before: %r", cutoff)
delete_notifications(filter_, sync=sync)


def flush_notifications(obj, clazz=None, sync=False):
"""Delete all notifications in a given channel."""
delete_notifications(channel_tag(obj, clazz=clazz), sync=sync)
filter_ = {"term": {"channels": channel_tag(obj, clazz=clazz)}}
delete_notifications(filter_, sync=sync)


def get_role_channels(role):
Expand Down
3 changes: 3 additions & 0 deletions aleph/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
ROLE_INACTIVE = env.to_int("ALEPH_ROLE_INACTIVE", 6 * 30)
ROLE_INACTIVE = timedelta(days=ROLE_INACTIVE)

# Delete notifications after N days.
NOTIFICATIONS_DELETE = env.to_int("ALEPH_NOTIFICATIONS_DELETE", 3 * 30)
NOTIFICATIONS_DELETE = timedelta(days=NOTIFICATIONS_DELETE)

###############################################################################
# Content processing options
Expand Down
3 changes: 2 additions & 1 deletion aleph/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from aleph.logic.alerts import check_alerts
from aleph.logic.collections import reingest_collection, reindex_collection
from aleph.logic.collections import compute_collections, refresh_collection
from aleph.logic.notifications import generate_digest
from aleph.logic.notifications import generate_digest, delete_old_notifications
from aleph.logic.roles import update_roles
from aleph.logic.export import delete_expired_exports, export_entities
from aleph.logic.processing import index_many
Expand Down Expand Up @@ -90,6 +90,7 @@ def periodic(self):
check_alerts()
generate_digest()
delete_expired_exports()
delete_old_notifications()

def dispatch_task(self, task):
collection = get_dataset_collection_id(task.job.dataset.name)
Expand Down

0 comments on commit 20b4ee7

Please sign in to comment.