From 08b8a1b7ea47b13c7e82178adc1f0de4ade1f9ee Mon Sep 17 00:00:00 2001 From: Aravinda VK Date: Wed, 26 Oct 2016 15:51:17 +0530 Subject: [PATCH 204/206] eventsapi: Auto reload Webhooks data when modified glustereventsd depends on reload signal to reload the Webhooks configurations. But if reload signal missed, no events will be sent to newly added Webhook. Added auto reload based on webhooks file mtime. Before pushing events to Webhooks, reloads webhooks configurations if previously recorded mtime is different than current mtime. > Reviewed-on: http://review.gluster.org/15731 > Reviewed-by: Prashanth Pai > Smoke: Gluster Build System > NetBSD-regression: NetBSD Build System > CentOS-regression: Gluster Build System BUG: 1384316 Change-Id: I83a41d6a52d8fa1d70e88294298f4a5c396d4158 Signed-off-by: Aravinda VK Reviewed-on: https://code.engineering.redhat.com/gerrit/91515 Reviewed-by: Atin Mukherjee --- events/src/utils.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/events/src/utils.py b/events/src/utils.py index dadd9ae..2e7bf0e 100644 --- a/events/src/utils.py +++ b/events/src/utils.py @@ -25,6 +25,7 @@ import eventtypes # Webhooks list _webhooks = {} +_webhooks_file_mtime = 0 # Default Log Level _log_level = "INFO" # Config Object @@ -109,10 +110,12 @@ def load_webhooks(): Load/Reload the webhooks list. This function will be triggered during init and when SIGUSR2. """ - global _webhooks + global _webhooks, _webhooks_file_mtime _webhooks = {} if os.path.exists(WEBHOOKS_FILE): _webhooks = json.load(open(WEBHOOKS_FILE)) + st = os.lstat(WEBHOOKS_FILE) + _webhooks_file_mtime = st.st_mtime def load_all(): @@ -130,6 +133,8 @@ def publish(ts, event_key, data): if NodeID is None: NodeID = get_node_uuid() + autoload_webhooks() + message = { "nodeid": NodeID, "ts": int(ts), @@ -143,6 +148,20 @@ def publish(ts, event_key, data): pass +def autoload_webhooks(): + global _webhooks_file_mtime + try: + st = os.lstat(WEBHOOKS_FILE) + except OSError: + st = None + + if st is not None: + # If Stat is available and mtime is not matching with + # previously recorded mtime, reload the webhooks file + if st.st_mtime != _webhooks_file_mtime: + load_webhooks() + + def plugin_webhook(message): # Import requests here since not used in any other place import requests -- 2.9.3