From b7ebffbda9ba784ccfae6d1a90766d5310cdaa15 Mon Sep 17 00:00:00 2001 From: Aravinda VK Date: Wed, 26 Oct 2016 15:51:17 +0530 Subject: 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. BUG: 1388862 Change-Id: I83a41d6a52d8fa1d70e88294298f4a5c396d4158 Signed-off-by: Aravinda VK 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 --- events/src/utils.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'events') diff --git a/events/src/utils.py b/events/src/utils.py index 6f695c37eba..e69d6577ff0 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 @@ -117,10 +118,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(): @@ -138,6 +141,8 @@ def publish(ts, event_key, data): if NodeID is None: NodeID = get_node_uuid() + autoload_webhooks() + message = { "nodeid": NodeID, "ts": int(ts), @@ -151,6 +156,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 -- cgit