Skip to content

Commit

Permalink
Add lock to run the contacts activities squash once
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Sep 26, 2024
1 parent 2207ae9 commit dec7102
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ureport/polls/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def polls_stats_squash():
from ureport.stats.models import PollStats

r = get_redis_connection()
key = "squesh_stats_lock"
key = "squash_stats_lock"

lock_timeout = 60 * 60 * 2

Expand Down
13 changes: 12 additions & 1 deletion ureport/stats/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import time
from datetime import timedelta

from django_redis import get_redis_connection

from django.utils import timezone

from dash.orgs.models import Org
Expand Down Expand Up @@ -70,7 +72,16 @@ def delete_old_contact_activities(org, since, until):
def squash_contact_activities_counts():
from .models import ContactActivityCounter

ContactActivityCounter.squash()
r = get_redis_connection()
key = "squash_contact_activity_counts_lock"

lock_timeout = 60 * 60

if r.get(key):
logger.info("Skipping squashing contact activity counts as it is still running")
else:
with r.lock(key, timeout=lock_timeout):
ContactActivityCounter.squash()


@app.task(name="stats.rebuild_contacts_activities_counts")
Expand Down

0 comments on commit dec7102

Please sign in to comment.