Skip to content

Refactor project documentation syncing tasks #3143

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 1 commit into from
Oct 5, 2017
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
110 changes: 73 additions & 37 deletions readthedocs/projects/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,14 @@ def run_build(self, docker=False, record=True):
except SoftTimeLimitExceeded:
raise BuildEnvironmentError(_('Build exited due to time out'))

# Web Server Tasks
# Finalize build and update web servers
if build_id:
finish_build.delay(
version_pk=self.version.pk,
build_pk=build_id,
hostname=socket.gethostname(),
html=outcomes['html'],
search=outcomes['search'],
localmedia=outcomes['localmedia'],
pdf=outcomes['pdf'],
epub=outcomes['epub'],
self.update_app_instances(
html=bool(outcomes['html']),
search=bool(outcomes['search']),
localmedia=bool(outcomes['localmedia']),
pdf=bool(outcomes['pdf']),
epub=bool(outcomes['epub']),
)

if self.build_env.failed:
Expand Down Expand Up @@ -333,6 +330,51 @@ def update_documentation_type(self):
api_v2.project(self.project.pk).put(project_data)
self.project.documentation_type = ret

def update_app_instances(self, html=False, localmedia=False, search=False,
pdf=False, epub=False):
"""Update application instances with build artifacts
This triggers updates across application instances for html, pdf, epub,
downloads, and search. Tasks are broadcast to all web servers from here.
"""
# Update version if we have successfully built HTML output
try:
if html:
version = api_v2.version(self.version.pk)
version.patch({
'active': True,
'built': True,
})
except HttpClientError as e:
log.error('Updating version failed, skipping file sync: version=%s',
self.version.pk, exc_info=True)
else:
# Broadcast finalization steps to web application instances
broadcast(
type='app',
task=sync_files,
args=[
self.project.pk,
self.version.pk,
],
kwargs=dict(
hostname=socket.gethostname(),
html=html,
localmedia=localmedia,
search=search,
pdf=pdf,
epub=epub,
)
)

# Delayed tasks
# TODO these should be chained on to the broadcast calls. The
# broadcast calls could be lumped together into a promise, and on
# task result, these next few tasks can be updated, also in a
# chained fashion
fileify.delay(self.version.pk, commit=self.build.get('commit'))
update_search.delay(self.version.pk, commit=self.build.get('commit'))

def setup_environment(self):
"""
Build the virtualenv and install the project into it.
Expand Down Expand Up @@ -541,41 +583,35 @@ def update_imported_docs(version_pk):

# Web tasks
@task(queue='web')
def finish_build(version_pk, build_pk, hostname=None, html=False,
localmedia=False, search=False, pdf=False, epub=False):
"""Build Finished, do house keeping bits"""
version = Version.objects.get(pk=version_pk)
build = Build.objects.get(pk=build_pk)

if html:
version.active = True
version.built = True
version.save()
def sync_files(project_pk, version_pk, hostname=None, html=False,
localmedia=False, search=False, pdf=False, epub=False):
"""Sync build artifacts to application instances
This task broadcasts from a build instance on build completion and
performs synchronization of build artifacts on each application instance.
"""
# Clean up unused artifacts
if not pdf:
broadcast(type='app', task=clear_pdf_artifacts, args=[version.pk])
clear_pdf_artifacts(version_pk)
if not epub:
broadcast(type='app', task=clear_epub_artifacts, args=[version.pk])
clear_epub_artifacts(version_pk)

# Sync files to the web servers
broadcast(type='app', task=move_files, args=[version_pk, hostname],
kwargs=dict(
html=html,
localmedia=localmedia,
search=search,
pdf=pdf,
epub=epub,
))
move_files(
version_pk,
hostname,
html=html,
localmedia=localmedia,
search=search,
pdf=pdf,
epub=epub
)

# Symlink project on every web
broadcast(type='app', task=symlink_project, args=[version.project.pk])
# Symlink project
symlink_project(project_pk)

# Update metadata
broadcast(type='app', task=update_static_metadata, args=[version.project.pk])

# Delayed tasks
fileify.delay(version.pk, commit=build.commit)
update_search.delay(version.pk, commit=build.commit)
update_static_metadata(project_pk)


@task(queue='web')
Expand Down
5 changes: 3 additions & 2 deletions readthedocs/restapi/views/model_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ def sync_versions(self, request, **kwargs):
})


class VersionViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
class VersionViewSet(viewsets.ModelViewSet):

permission_classes = [APIRestrictedPermission]
renderer_classes = (JSONRenderer,)
serializer_class = VersionSerializer
model = Version
Expand Down