Skip to content

Commit

Permalink
Merge pull request #1245 from rapidpro/squash-contact-activities-coun…
Browse files Browse the repository at this point in the history
…ts-with-lock

Add lock to run the contacts activities squash once
  • Loading branch information
norkans7 authored Sep 26, 2024
2 parents 2207ae9 + 7ca4a84 commit a6430de
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 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
2 changes: 1 addition & 1 deletion ureport/utils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def squash(cls):

time_taken = time.time() - start

logging.debug("Squashed %d distinct sets of %s in %0.3fs" % (num_sets, cls.__name__, time_taken))
logging.info("Squashed %d distinct sets of %s in %0.3fs" % (num_sets, cls.__name__, time_taken))

@classmethod
@abstractmethod
Expand Down

0 comments on commit a6430de

Please sign in to comment.