Skip to content

Commit

Permalink
OAuth: resync RemoteRepository weekly for active users
Browse files Browse the repository at this point in the history
Trigger a daily task to compare user's last login isoweekday with today's
isoweekday for all active users. If they matches, we resync the
`RemoteRepository` for this user.

This logic is the same as "resync `RemoteRepository` once a week per each user".

We consider active users those that have logged in at least once in the last 90
days.

Related: #8229
Related: #9409
  • Loading branch information
humitos committed Jul 7, 2022
1 parent dc62d61 commit d76f145
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
28 changes: 28 additions & 0 deletions readthedocs/oauth/tasks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"""Tasks for OAuth services."""

import datetime

import structlog
from allauth.socialaccount.providers import registry as allauth_registry
from django.contrib.auth.models import User
from django.db.models.functions import ExtractIsoWeekDay
from django.utils import timezone
from django.utils.translation import gettext_lazy as _

from readthedocs.core.permissions import AdminPermission
Expand Down Expand Up @@ -104,6 +108,30 @@ def sync_remote_repositories_organizations(organization_slugs=None):


@app.task(queue='web')
def sync_active_users_remote_repositories():
"""
Sync ``RemoteRepository`` for active users.
We consider active users those that logged in at least once in the last 90 days.
This task is thought to be executed daily. It checks the weekday of the
last login of the user with today's weekday. If they match, the re-sync is
triggered. This logic guarantees us the re-sync to be done once a week per user.
"""
today_weekday = timezone.now().isoweekday()
three_months_ago = timezone.now() - datetime.timedelta(days=90)
users = User.objects.annotate(weekday=ExtractIsoWeekDay("last_login")).filter(
last_login__gt=three_months_ago,
socialaccount__isnull=False,
weekday=today_weekday,
)

log.info("Re-syncing RemoteRepository for active users.", users=users.count())
for user in users:
sync_remote_repositories.delay(user.pk)


@app.task(queue="web")
def attach_webhook(project_pk, user_pk, integration=None):
"""
Add post-commit hook on project import.
Expand Down
5 changes: 5 additions & 0 deletions readthedocs/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,11 @@ def TEMPLATES(self):
'schedule': crontab(minute=0, hour=1),
'options': {'queue': 'web'},
},
'every-day-resync-remote-repositories': {
'task': 'readthedocs.oauth.tasks.sync_active_users_remote_repositories',
'schedule': crontab(minute=0, hour=5),
'options': {'queue': 'web'},
}
}

MULTIPLE_BUILD_SERVERS = [CELERY_DEFAULT_QUEUE]
Expand Down

0 comments on commit d76f145

Please sign in to comment.