Skip to content

Track organization artifacts cleanup #8418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions readthedocs/organizations/migrations/0006_add_assets_cleaned.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.2.24 on 2021-08-17 14:54

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('organizations', '0005_historicalorganization_historicalteam'),
]

operations = [
migrations.AddField(
model_name='historicalorganization',
name='artifacts_cleaned',
field=models.BooleanField(default=False, help_text='Artifacts are cleaned out from storage', verbose_name='Artifacts Cleaned'),
),
migrations.AddField(
model_name='organization',
name='artifacts_cleaned',
field=models.BooleanField(default=False, help_text='Artifacts are cleaned out from storage', verbose_name='Artifacts Cleaned'),
),
]
5 changes: 5 additions & 0 deletions readthedocs/organizations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ class Organization(models.Model):
help_text='Docs and builds are disabled for this organization',
default=False,
)
artifacts_cleaned = models.BooleanField(
_('Artifacts Cleaned'),
help_text='Artifacts are cleaned out from storage',
default=False,
)
max_concurrent_builds = models.IntegerField(
_('Maximum concurrent builds allowed for this organization'),
null=True,
Expand Down
20 changes: 19 additions & 1 deletion readthedocs/organizations/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from django.db.models.signals import pre_delete
from django.dispatch import receiver

from readthedocs.builds.models import Version
from readthedocs.builds.constants import BUILD_STATE_FINISHED
from readthedocs.builds.models import Build, Version
from readthedocs.builds.signals import build_complete
from readthedocs.organizations.models import (
Organization,
Team,
Expand All @@ -16,6 +18,8 @@
)
from readthedocs.projects.models import Project

from .tasks import mark_organization_assets_not_cleaned as mark_organization_assets_not_cleaned_task

log = logging.getLogger(__name__)


Expand Down Expand Up @@ -74,3 +78,17 @@ def remove_organization_completely(sender, instance, using, **kwargs):
version.delete()

projects.delete()


@receiver(build_complete, sender=Build)
def mark_organization_assets_not_cleaned(sender, build, **kwargs):
"""
Mark the organization assets as not cleaned if there is a new build.

This signal triggers a Celery task because the `build_complete` signal is
fired by the builder and it does not have access to the database. So, we
trigger a Celery task that will be executed in the web and mark the
organization assets as not cleaned.
"""
if build['state'] == BUILD_STATE_FINISHED:
mark_organization_assets_not_cleaned_task.delay(build['id'])
25 changes: 25 additions & 0 deletions readthedocs/organizations/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Organization's tasks related."""

import logging

from readthedocs.builds.models import Build
from readthedocs.worker import app


log = logging.getLogger(__name__)


@app.task(queue='web')
def mark_organization_assets_not_cleaned(build_pk):
"""Mark an organization as `artifacts_cleaned=False`."""
try:
build = Build.objects.get(pk=build_pk)
except Build.DoesNotExist:
log.info("Build does not exist. build=%s", build_pk)
return

organization = build.project.organizations.first()
if organization:
log.info("Marking organization as not cleaned. organization=%s", organization.slug)
organization.artifacts_cleaned = False
organization.save()