From 9e7d56fe625c8060bd686dc2afad34231758b491 Mon Sep 17 00:00:00 2001 From: xwu2git <3871873+xwu2git@users.noreply.github.com> Date: Mon, 22 Jul 2019 22:00:45 -0700 Subject: [PATCH 1/2] gracefully exit by handling SIGTERM and keep database consistent --- analytics/feeder/main.py | 18 +++++++++++++----- maintenance/cleanup/Dockerfile | 2 +- maintenance/cleanup/main.py | 12 +++++++++--- maintenance/where_indexing/Dockerfile | 2 +- maintenance/where_indexing/main.py | 7 +++++++ sensor/onvif/Dockerfile | 2 +- sensor/onvif/main.py | 5 +++++ sensor/simulation/Dockerfile | 2 +- sensor/simulation/main.py | 9 +++++++++ trigger/health/Dockerfile | 2 +- trigger/health/main.py | 9 +++++++++ web/cloud/Dockerfile | 2 +- web/cloud/main.py | 15 ++++++++++++++- web/local/Dockerfile | 2 +- web/local/main.py | 14 +++++++++++++- 15 files changed, 86 insertions(+), 17 deletions(-) diff --git a/analytics/feeder/main.py b/analytics/feeder/main.py index 208ec4805..56e3cd526 100644 --- a/analytics/feeder/main.py +++ b/analytics/feeder/main.py @@ -2,6 +2,7 @@ from db_ingest import DBIngest from db_query import DBQuery +from signal import signal, SIGTERM from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler @@ -14,10 +15,7 @@ from threading import Timer from probe import probe, run import time -import datetime import os -import sys -import signal import tempfile import shutil import re @@ -114,12 +112,13 @@ def stop(self): logger.info(" ### Stopping Feeder ### ") self._threadflag = False - self.mqttclient.loop_stop() - self.observer.stop() logger.debug("Unregistering algorithm from DB") self.db_alg.delete(self.alg_id) + self.mqttclient.loop_stop() + self.observer.stop() + def startmqtt(self): self.mqttclient = mqtt.Client("feeder_" + self.alg_id) self.mqttclient.connect(self.mqtthost) @@ -345,6 +344,15 @@ def ingest(self): if __name__ == '__main__': smtc_feeder = Feeder() + + def quit_nicely(signum, sigframe): + try: + smtc_feeder.stop() + except Exception as e: + print("quit exception"+str(e)) + exit(143) + signal(SIGTERM, quit_nicely) + try: smtc_feeder.start() except KeyboardInterrupt: diff --git a/maintenance/cleanup/Dockerfile b/maintenance/cleanup/Dockerfile index 436f05ca8..705f917e6 100644 --- a/maintenance/cleanup/Dockerfile +++ b/maintenance/cleanup/Dockerfile @@ -4,7 +4,7 @@ FROM centos:7.6.1810 RUN yum -y install epel-release && yum -y install python36-requests python36-ply && rm -rf /var/cache/yum/* COPY *.py /home/ -CMD ["/bin/bash","-c","/home/main.py"] +CMD ["/home/main.py"] #### ARG USER diff --git a/maintenance/cleanup/main.py b/maintenance/cleanup/main.py index 4f2f57bc4..421012105 100755 --- a/maintenance/cleanup/main.py +++ b/maintenance/cleanup/main.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 from db_query import DBQuery +from signal import signal, SIGTERM import time import os @@ -10,13 +11,19 @@ office=list(map(float, os.environ["OFFICE"].split(","))) dbhost=os.environ["DBHOST"] -while True: +def quit_nicely(signum, sigframe): + exit(143) + +# set signal to quit nicely +signal(SIGTERM, quit_nicely) + +while True: time.sleep(service_interval) print("Searching...",flush=True) for index in indexes: db=DBQuery(index=index,office=office,host=dbhost) - for r in db.search("time Date: Mon, 22 Jul 2019 22:33:08 -0700 Subject: [PATCH 2/2] office -> recording fix --- volume/html/js/office.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/volume/html/js/office.js b/volume/html/js/office.js index e533c9997..6dea288fd 100644 --- a/volume/html/js/office.js +++ b/volume/html/js/office.js @@ -29,7 +29,7 @@ $("#pg-office").on(":initpage", function(e, queries, office) { var line=$(""+v._source.name+""+v._id+""+source+""+v._source.status+""+latency+""+performance+""+v._source.skip+" frame(s)"); tbody.append(line); line.find("a").click(function () { - selectPage("recording",['sensor="'+v._source.source+'"']); + selectPage("recording",['sensor="'+v._source.source+'"',office]); }); });