From a465e910c4ba7a0aad3c98f73d19b5a8367580b7 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand <benjamin.bertrand@esss.se> Date: Mon, 16 Sep 2019 13:08:11 +0200 Subject: [PATCH] Decouple inventory and core services update The core services update will trigger its own inventory sync on launch. As the job takes some time to run, it avoids blocking the main inventory sync. It also allows to trigger a core services update less frequently by keeping only one in queue. INFRA JIRA-1290 --- app/models.py | 8 ++++---- app/utils.py | 32 +++++++++++++++++--------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/app/models.py b/app/models.py index 68d4577..e904ad4 100644 --- a/app/models.py +++ b/app/models.py @@ -1782,9 +1782,8 @@ def trigger_core_services_update(session): def trigger_inventory_update(session): """Trigger an inventory update in AWX - Update on any AnsibleGroup/Cname/Domain/Host/Network/NetworkScope - modification, but not on Interface as it's triggered with core - services update. + Update on any AnsibleGroup/Cname/Domain/Host/Interface/Network/NetworkScope + modification. Called by before flush hook """ @@ -1795,7 +1794,8 @@ def trigger_inventory_update(session): for kind in ("new", "dirty", "deleted"): for instance in getattr(session, kind): if isinstance( - instance, (AnsibleGroup, Cname, Domain, Host, Network, NetworkScope) + instance, + (AnsibleGroup, Cname, Domain, Host, Interface, Network, NetworkScope), ) and ( (kind == "dirty" and session.is_modified(instance)) or (kind in ("new", "deleted")) diff --git a/app/utils.py b/app/utils.py index b1b120d..190f52c 100644 --- a/app/utils.py +++ b/app/utils.py @@ -233,32 +233,34 @@ def pretty_yaml(value): def trigger_core_services_update(): - """Trigger a job to update the core services on the TN (DNS and DHCP) + """Trigger a job to update the core services (DNS/DHCP/radius) This function should be called every time an interface is created/edited - Always trigger an inventory update first. + The AWX template uses its own inventory that is updated on launch to avoid + blocking the main inventory update when running. + There is no need to trigger an inventory update. + + We can have one running job + one in queue to apply the latest changes. + Make sure that we don't have more than one in queue. """ - # Start by triggering an inventory update - inventory_task = trigger_inventory_update() - # If there is already a core service update depending on this inventory update, - # there is no need to trigger a new one - for reverse_dependency in inventory_task.reverse_dependencies: - if reverse_dependency.name == "trigger_core_services_update": - current_app.logger.info( - f'Already one "trigger_core_services_update" {reverse_dependency} ' - f"depending on the inventory update {inventory_task}. " - "No need to trigger a new one." - ) - return None + waiting_task = current_user.get_task_waiting("trigger_core_services_update") + if waiting_task is not None: + current_app.logger.info( + 'Already one "trigger_core_services_update" task waiting. No need to trigger a new one.' + ) + return waiting_task job_template = current_app.config["AWX_CORE_SERVICES_UPDATE"] resource = current_app.config.get("AWX_CORE_SERVICES_UPDATE_RESOURCE", "job") kwargs = { "func": "launch_awx_job", "job_template": job_template, "resource": resource, - "depends_on": inventory_task.id, } + started = current_user.get_task_started("trigger_core_services_update") + if started: + # There is already one running task. Trigger a new one when it's done. + kwargs["depends_on"] = started.id current_app.logger.info(f"Launch new job to update core services: {job_template}") task = current_user.launch_task( "trigger_core_services_update", queue_name="normal", **kwargs -- GitLab