diff --git a/dynamic_logging/models.py b/dynamic_logging/models.py index 005d1a8..2cbd370 100644 --- a/dynamic_logging/models.py +++ b/dynamic_logging/models.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import datetime +import hashlib import json import logging import logging.config @@ -165,6 +166,12 @@ def config(self, val): } self.config_json = json.dumps(res) + def get_hash(self): + h = hashlib.sha256() + h.update((u'%s' % self.pk).encode('utf-8')) + h.update(self.config_json.encode('utf-8')) + return h.digest() + def apply(self, trigger=None): """ apply the current config to the global logging system. diff --git a/dynamic_logging/scheduler.py b/dynamic_logging/scheduler.py index 2ebb9e4..996015f 100644 --- a/dynamic_logging/scheduler.py +++ b/dynamic_logging/scheduler.py @@ -29,6 +29,8 @@ def __init__(self): """ :type: Trigger """ + self.current_config_hash = None + self.start_thread = True """ a bool to prevent the threads to start, for testing purpose @@ -220,8 +222,14 @@ def wake(self, trigger, date): self.set_next_wake(next_trigger, at) def apply(self, trigger): - logger.debug('applying %s', trigger, extra={'trigger': trigger, 'config': trigger.config.config_json}) - trigger.apply() + hash_config = trigger.config.get_hash() + if self.current_config_hash == hash_config: + logger.debug("not applying currently active config %s", trigger, + extra={'trigger': trigger, 'config': trigger.config.config_json}) + else: + logger.debug('applying %s', trigger, extra={'trigger': trigger, 'config': trigger.config.config_json}) + trigger.apply() + self.current_config_hash = hash_config self.current_trigger = trigger self.trigger_applied.set() diff --git a/dynamic_logging/tests.py b/dynamic_logging/tests.py index 1cee6df..6e9f1d6 100644 --- a/dynamic_logging/tests.py +++ b/dynamic_logging/tests.py @@ -670,6 +670,22 @@ def test_loggers_creation(self): }, }) + def test_apply_by_scheduler_same_config(self): + called = [] + + c = Config(name="lol", config_json='{}') + t = Trigger(name='lol', config=c, start_date=None, end_date=None) + c.apply = lambda tt: called.append(tt) + main_scheduler.apply(t) + self.assertEqual(called, [t]) + main_scheduler.apply(t) + self.assertEqual(called, [t]) + c.config_json = '' + main_scheduler.apply(t) + self.assertEqual(called, [t, t]) + main_scheduler.apply(t) + self.assertEqual(called, [t, t]) + class TestTag(TestCase): def test_display_config_current_auto(self): diff --git a/requirements.txt b/requirements.txt index f3ece01..befab18 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,5 @@ sphinx tox isort flake8 +pika -r test_requirements.txt