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"+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]); }); }); diff --git a/web/cloud/Dockerfile b/web/cloud/Dockerfile index e152392f1..519eae1f5 100644 --- a/web/cloud/Dockerfile +++ b/web/cloud/Dockerfile @@ -5,7 +5,7 @@ RUN yum -y install epel-release && yum -y install nginx python36-tornado python3 COPY *.conf /etc/nginx/ COPY *.py /home/ -CMD ["/bin/bash","-c","/home/main.py&/usr/sbin/nginx"] +CMD ["/home/main.py"] EXPOSE 8080 #### diff --git a/web/cloud/main.py b/web/cloud/main.py index f6f8a685e..db56cb5e9 100755 --- a/web/cloud/main.py +++ b/web/cloud/main.py @@ -6,6 +6,8 @@ from hint import HintHandler from redirect import RedirectHandler from stats import StatsHandler +from subprocess import Popen +from signal import signal, SIGTERM, SIGQUIT app = web.Application([ (r'/api/search',SearchHandler), @@ -21,4 +23,15 @@ parse_command_line() print("Listening to " + options.ip + ":" + str(options.port)) app.listen(options.port, address=options.ip) - ioloop.IOLoop.instance().start() + + tornado1=ioloop.IOLoop.instance(); + nginx1=Popen(["/usr/sbin/nginx"]) + + # set SIGTERM/SIGQUIT handler + def quit_nicely(signum, frame): + nginx1.send_signal(SIGQUIT) + tornado1.add_callback(tornado1.stop) + + signal(SIGTERM, quit_nicely) + tornado1.start() + nginx1.wait() diff --git a/web/local/Dockerfile b/web/local/Dockerfile index c4d5ffe38..519eae1f5 100644 --- a/web/local/Dockerfile +++ b/web/local/Dockerfile @@ -5,7 +5,7 @@ RUN yum -y install epel-release && yum -y install nginx python36-tornado python3 COPY *.conf /etc/nginx/ COPY *.py /home/ -CMD ["/bin/bash","-c","/usr/sbin/nginx&/home/main.py"] +CMD ["/home/main.py"] EXPOSE 8080 #### diff --git a/web/local/main.py b/web/local/main.py index 6c38bfcba..48d87861f 100755 --- a/web/local/main.py +++ b/web/local/main.py @@ -4,6 +4,8 @@ from tornado.options import define, options, parse_command_line from workload import WorkloadHandler from db_ingest import DBIngest +from subprocess import Popen +from signal import signal, SIGTERM, SIGQUIT import socket import os @@ -31,4 +33,14 @@ print("Listening to " + options.ip + ":" + str(options.port)) app.listen(options.port, address=options.ip) - ioloop.IOLoop.instance().start() + tornado1=ioloop.IOLoop.instance(); + nginx1=Popen(["/usr/sbin/nginx"]) + + def quit_nicely(signum, frame): + tornado1.add_callback(tornado1.stop) + nginx1.send_signal(SIGQUIT) + db.delete(r["_id"]) + + signal(SIGTERM, quit_nicely) + tornado1.start() + nginx1.wait()