From c1dc0c7b86cdc309edeb593022ab1d5ef2ced888 Mon Sep 17 00:00:00 2001 From: Hao Liu <44379968+TheRealHaoLiu@users.noreply.github.com> Date: Mon, 10 Jun 2024 14:10:57 -0400 Subject: [PATCH] Periodically sync from share resource provider (#15264) * Periodically sync from share resource provider - add periodic task `periodic_resource_sync` run once every 15 min - if `RESOURCE_SERVER` is not configured sync will not run - only 1 node example RESOURCE_SERVER configuration ``` RESOURCE_SERVER = { "URL": "", "SECRET_KEY": "", "VALIDATE_HTTPS": , } RESOURCE_SERVICE_PATH = ``` --- awx/main/tasks/system.py | 17 +++++++++++++++++ awx/settings/defaults.py | 1 + 2 files changed, 18 insertions(+) diff --git a/awx/main/tasks/system.py b/awx/main/tasks/system.py index bca9d16c05..aa65e706c1 100644 --- a/awx/main/tasks/system.py +++ b/awx/main/tasks/system.py @@ -36,6 +36,9 @@ import ansible_runner.cleanup # dateutil from dateutil.parser import parse as parse_date +# django-ansible-base +from ansible_base.resource_registry.tasks.sync import SyncExecutor + # AWX from awx import __version__ as awx_application_version from awx.main.access import access_registry @@ -964,3 +967,17 @@ def deep_copy_model_obj(model_module, model_name, obj_pk, new_obj_pk, user_pk, p permission_check_func(creater, copy_mapping.values()) if isinstance(new_obj, Inventory): update_inventory_computed_fields.delay(new_obj.id) + + +@task(queue=get_task_queuename) +def periodic_resource_sync(): + if not getattr(settings, 'RESOURCE_SERVER', None): + logger.debug("Skipping periodic resource_sync, RESOURCE_SERVER not configured") + return + + with advisory_lock('periodic_resource_sync', wait=False) as acquired: + if acquired is False: + logger.debug("Not running periodic_resource_sync, another task holds lock") + return + + SyncExecutor().run() diff --git a/awx/settings/defaults.py b/awx/settings/defaults.py index 9db1312ad7..72d2813d4a 100644 --- a/awx/settings/defaults.py +++ b/awx/settings/defaults.py @@ -492,6 +492,7 @@ CELERYBEAT_SCHEDULE = { 'cleanup_images': {'task': 'awx.main.tasks.system.cleanup_images_and_files', 'schedule': timedelta(hours=3)}, 'cleanup_host_metrics': {'task': 'awx.main.tasks.host_metrics.cleanup_host_metrics', 'schedule': timedelta(hours=3, minutes=30)}, 'host_metric_summary_monthly': {'task': 'awx.main.tasks.host_metrics.host_metric_summary_monthly', 'schedule': timedelta(hours=4)}, + 'periodic_resource_sync': {'task': 'awx.main.tasks.system.periodic_resource_sync', 'schedule': timedelta(minutes=15)}, } # Django Caching Configuration