From 3d9e16f98c1a998f2060a103f0818d8da14c0e34 Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Sun, 29 Oct 2023 08:26:06 +0100 Subject: [PATCH] Periodic reconciler only on change (#649) Signed-off-by: Christian Berendt --- osism/tasks/reconciler.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/osism/tasks/reconciler.py b/osism/tasks/reconciler.py index b56f6f04..de3884ee 100644 --- a/osism/tasks/reconciler.py +++ b/osism/tasks/reconciler.py @@ -29,7 +29,7 @@ def celery_init_worker(**kwargs): @app.on_after_configure.connect def setup_periodic_tasks(sender, **kwargs): sender.add_periodic_task( - settings.INVENTORY_RECONCILER_SCHEDULE, run.s(), expires=10 + settings.INVENTORY_RECONCILER_SCHEDULE, run_on_change.s(), expires=10 ) @@ -48,6 +48,23 @@ def run(self): lock.release() +@app.task(bind=True, name="osism.tasks.reconciler.run_on_change") +def run_on_change(self): + lock = Redlock( + key="lock_osism_tasks_reconciler_run_on_change", + masters={redis}, + auto_release_time=60, + ) + + if lock.acquire(timeout=20): + p = subprocess.Popen( + "/run.sh", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT + ) + p.wait() + + lock.release() + + @app.task(bind=True, name="osism.tasks.reconciler.sync_inventory_with_netbox") def sync_inventory_with_netbox(self): lock = Redlock(