aboutsummaryrefslogtreecommitdiffstats
path: root/policyhandler/step_timer.py
diff options
context:
space:
mode:
authorAlex Shatov <alexs@att.com>2018-03-13 17:50:51 -0400
committerAlex Shatov <alexs@att.com>2018-03-13 17:50:51 -0400
commita2b262994a62286e1c98fd9939115c6e64ed27ee (patch)
treef6ef7056c70a34485d1ed047a296b89bb3354ee2 /policyhandler/step_timer.py
parentb9b955c0c2875effc1ce02d5576f0d2a59284c5d (diff)
2.3.0 policy-handler - periodically catch_up
- periodically catchup - interval is configurable = max_skips defines the number of times the catch_up message that is identical to previous one can be skipped - do not catchup more often than the interval even between the manual catchup and auto catchup - do not send the same catchup message twice in a row to the deployment-handler but not exceed a hard limit on catchup max_skips - catchup if the deployment-handler instance is changed Change-Id: I9a3fcc941e8a9e553abb3952dd882c37e0f5fdfe Signed-off-by: Alex Shatov <alexs@att.com> Issue-ID: DCAEGEN2-389
Diffstat (limited to 'policyhandler/step_timer.py')
-rw-r--r--policyhandler/step_timer.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/policyhandler/step_timer.py b/policyhandler/step_timer.py
new file mode 100644
index 0000000..6e2b3f6
--- /dev/null
+++ b/policyhandler/step_timer.py
@@ -0,0 +1,63 @@
+# ================================================================================
+# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ============LICENSE_END=========================================================
+#
+# ECOMP is a trademark and service mark of AT&T Intellectual Property.
+
+"""periodically callback"""
+
+from threading import Event, Thread
+
+
+class StepTimer(Thread):
+ """call on_time after interval number of seconds, then wait to continue"""
+ def __init__(self, name, interval, on_time, *args, **kwargs):
+ Thread.__init__(self, name=name)
+ self._interval = interval
+ self._on_time = on_time
+ self._args = args
+ self._kwargs = kwargs
+
+ self._timeout = Event()
+ self._paused = Event()
+ self._continue = Event()
+ self._finished = Event()
+
+ def next(self):
+ """continue with the next timeout"""
+ self._paused.clear()
+ self._continue.set()
+
+ def pause(self):
+ """pause the timer"""
+ self._paused.set()
+
+ def stop(self):
+ """stop the timer if it hasn't finished yet"""
+ self._finished.set()
+ self._timeout.set()
+ self._continue.set()
+
+ def run(self):
+ """loop until stopped=finished"""
+ while True:
+ self._timeout.wait(self._interval)
+ if self._finished.is_set():
+ break
+ self._timeout.clear()
+ self._continue.clear()
+ if not self._paused.is_set():
+ self._on_time(*self._args, **self._kwargs)
+ self._continue.wait()