Skip to content

Commit d8e99b7

Browse files
authored
Merge pull request #8418 from readthedocs/humitos/assets-cleanup
2 parents 01e0d77 + c95224f commit d8e99b7

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 2.2.24 on 2021-08-17 14:54
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('organizations', '0005_historicalorganization_historicalteam'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='historicalorganization',
15+
name='artifacts_cleaned',
16+
field=models.BooleanField(default=False, help_text='Artifacts are cleaned out from storage', verbose_name='Artifacts Cleaned'),
17+
),
18+
migrations.AddField(
19+
model_name='organization',
20+
name='artifacts_cleaned',
21+
field=models.BooleanField(default=False, help_text='Artifacts are cleaned out from storage', verbose_name='Artifacts Cleaned'),
22+
),
23+
]

readthedocs/organizations/models.py

+5
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ class Organization(models.Model):
7676
help_text='Docs and builds are disabled for this organization',
7777
default=False,
7878
)
79+
artifacts_cleaned = models.BooleanField(
80+
_('Artifacts Cleaned'),
81+
help_text='Artifacts are cleaned out from storage',
82+
default=False,
83+
)
7984
max_concurrent_builds = models.IntegerField(
8085
_('Maximum concurrent builds allowed for this organization'),
8186
null=True,

readthedocs/organizations/signals.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from django.db.models.signals import pre_delete
88
from django.dispatch import receiver
99

10-
from readthedocs.builds.models import Version
10+
from readthedocs.builds.constants import BUILD_STATE_FINISHED
11+
from readthedocs.builds.models import Build, Version
12+
from readthedocs.builds.signals import build_complete
1113
from readthedocs.organizations.models import (
1214
Organization,
1315
Team,
@@ -16,6 +18,8 @@
1618
)
1719
from readthedocs.projects.models import Project
1820

21+
from .tasks import mark_organization_assets_not_cleaned as mark_organization_assets_not_cleaned_task
22+
1923
log = logging.getLogger(__name__)
2024

2125

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

7680
projects.delete()
81+
82+
83+
@receiver(build_complete, sender=Build)
84+
def mark_organization_assets_not_cleaned(sender, build, **kwargs):
85+
"""
86+
Mark the organization assets as not cleaned if there is a new build.
87+
88+
This signal triggers a Celery task because the `build_complete` signal is
89+
fired by the builder and it does not have access to the database. So, we
90+
trigger a Celery task that will be executed in the web and mark the
91+
organization assets as not cleaned.
92+
"""
93+
if build['state'] == BUILD_STATE_FINISHED:
94+
mark_organization_assets_not_cleaned_task.delay(build['id'])

readthedocs/organizations/tasks.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Organization's tasks related."""
2+
3+
import logging
4+
5+
from readthedocs.builds.models import Build
6+
from readthedocs.worker import app
7+
8+
9+
log = logging.getLogger(__name__)
10+
11+
12+
@app.task(queue='web')
13+
def mark_organization_assets_not_cleaned(build_pk):
14+
"""Mark an organization as `artifacts_cleaned=False`."""
15+
try:
16+
build = Build.objects.get(pk=build_pk)
17+
except Build.DoesNotExist:
18+
log.info("Build does not exist. build=%s", build_pk)
19+
return
20+
21+
organization = build.project.organizations.first()
22+
if organization:
23+
log.info("Marking organization as not cleaned. organization=%s", organization.slug)
24+
organization.artifacts_cleaned = False
25+
organization.save()

0 commit comments

Comments
 (0)