1
0
mirror of https://github.com/ansible/awx.git synced 2024-10-31 15:21:13 +03:00

Reduce API response times by caching migration flag

This commit is contained in:
AlanCoding 2019-11-04 22:28:49 -05:00
parent 04c535e3f9
commit 5433af6716
No known key found for this signature in database
GPG Key ID: FD2C3C012A72926B
3 changed files with 24 additions and 5 deletions

View File

@ -1,8 +1,17 @@
from django.apps import AppConfig from django.apps import AppConfig
from django.db.models.signals import pre_migrate
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
def raise_migration_flag(**kwargs):
from awx.main.tasks import set_migration_flag
set_migration_flag.delay()
class MainConfig(AppConfig): class MainConfig(AppConfig):
name = 'awx.main' name = 'awx.main'
verbose_name = _('Main') verbose_name = _('Main')
def ready(self):
pre_migrate.connect(raise_migration_flag, sender=self)

View File

@ -18,6 +18,7 @@ from django.db import IntegrityError, connection
from django.utils.functional import curry from django.utils.functional import curry
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.apps import apps from django.apps import apps
from django.core.cache import cache
from django.utils.deprecation import MiddlewareMixin from django.utils.deprecation import MiddlewareMixin
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.urls import reverse, resolve from django.urls import reverse, resolve
@ -213,8 +214,11 @@ class URLModificationMiddleware(MiddlewareMixin):
class MigrationRanCheckMiddleware(MiddlewareMixin): class MigrationRanCheckMiddleware(MiddlewareMixin):
def process_request(self, request): def process_request(self, request):
executor = MigrationExecutor(connection) if cache.get('migration_in_progress', False):
plan = executor.migration_plan(executor.loader.graph.leaf_nodes()) executor = MigrationExecutor(connection)
if bool(plan) and \ plan = executor.migration_plan(executor.loader.graph.leaf_nodes())
getattr(resolve(request.path), 'url_name', '') != 'migrations_notran': if not bool(plan):
return redirect(reverse("ui:migrations_notran")) logger.info('Detected that migration finished, migration page taken down.')
cache.delete('migration_in_progress')
elif getattr(resolve(request.path), 'url_name', '') != 'migrations_notran':
return redirect(reverse("ui:migrations_notran"))

View File

@ -263,6 +263,12 @@ def apply_cluster_membership_policies():
logger.debug('Cluster policy computation finished in {} seconds'.format(time.time() - started_compute)) logger.debug('Cluster policy computation finished in {} seconds'.format(time.time() - started_compute))
@task(queue='tower_broadcast_all', exchange_type='fanout')
def set_migration_flag():
logger.debug('Received migration-in-progress signal, will serve redirect.')
cache.set('migration_in_progress', True)
@task(queue='tower_broadcast_all', exchange_type='fanout') @task(queue='tower_broadcast_all', exchange_type='fanout')
def handle_setting_changes(setting_keys): def handle_setting_changes(setting_keys):
orig_len = len(setting_keys) orig_len = len(setting_keys)