Skip to content

Commit

Permalink
Merge pull request #19 from xwu2git/master
Browse files Browse the repository at this point in the history
handle SIGTERM to gracefully exit; fix office->recording link;
  • Loading branch information
xwu2git authored Jul 23, 2019
2 parents 97bf32d + 9601de1 commit 5b7cdff
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 18 deletions.
18 changes: 13 additions & 5 deletions analytics/feeder/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion maintenance/cleanup/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions maintenance/cleanup/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/python3

from db_query import DBQuery
from signal import signal, SIGTERM
import time
import os

Expand All @@ -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<now-"+str(retention_time*1000)):
for r in db.search("time<now-"+str(retention_time*1000), size=500):
# delete the record
db.delete(r["_id"])

Expand All @@ -26,4 +33,3 @@
os.remove(r["_source"]["path"])
except Exception as e:
print(str(e))

2 changes: 1 addition & 1 deletion maintenance/where_indexing/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions maintenance/where_indexing/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/python3

from db_query import DBQuery
from signal import signal, SIGTERM
import os
import time

Expand All @@ -12,6 +13,12 @@
office=list(map(float, os.environ["OFFICE"].split(",")))
dbhost=os.environ["DBHOST"]

def quit_nicely(signum, sigframe):
exit(143)

# set signal to quit nicely
signal(SIGTERM, quit_nicely)

dbq=DBQuery(index=indexes[0],office=office,host=dbhost)
dba=DBQuery(index=indexes[1],office=office,host=dbhost)
while True:
Expand Down
2 changes: 1 addition & 1 deletion sensor/onvif/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ RUN wget ${ZEEP_REPO} && \
rm v${ONVIF_ZEEP_VER}.tar.gz

COPY *.py /home/
CMD ["/bin/bash","-c","/home/main.py"]
CMD ["/home/main.py"]
WORKDIR /home
EXPOSE 80

Expand Down
5 changes: 5 additions & 0 deletions sensor/onvif/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import subprocess
from onvif import ONVIFCamera, ONVIFError
import xml.etree.ElementTree as ET
from signal import SIGTERM, signal

from db_query import DBQuery
from db_ingest import DBIngest
Expand Down Expand Up @@ -317,4 +318,8 @@ def discover_all_onvif_cameras():
time.sleep(60)

if __name__ == "__main__":
def quit_nicely(signum, sigframe):
exit(143)
signal(SIGTERM, quit_nicely)

discover_all_onvif_cameras()
2 changes: 1 addition & 1 deletion sensor/simulation/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM ubuntu:18.04
RUN apt-get update && apt-get install -y -q python3-requests vlc && rm -rf /var/lib/apt/lists/*
COPY *.py /home/
ENTRYPOINT ["/bin/bash","-c","/home/main.py"]
CMD ["/home/main.py"]

####
ARG USER
Expand Down
9 changes: 9 additions & 0 deletions sensor/simulation/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/python3

from db_ingest import DBIngest
from signal import signal, SIGTERM
import subprocess
import socket
import math
Expand Down Expand Up @@ -61,6 +62,14 @@ def geo_point(origin, distance, tc):
"status": "idle",
})

def quit_nicely(signum, sigframe):
try:
db.delete(r["_id"])
except Exception as e:
print("quit exception: "+str(e))
exit(143)
signal(SIGTERM, quit_nicely)

# run rtspatt
while True:
simulated_root="/mnt/simulated"
Expand Down
2 changes: 1 addition & 1 deletion trigger/health/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM centos:7.6.1810

RUN yum install -y epel-release && yum install -y 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
Expand Down
9 changes: 9 additions & 0 deletions trigger/health/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from db_ingest import DBIngest
from db_query import DBQuery
from signal import SIGTERM, signal
import datetime
import time
import os
Expand All @@ -18,6 +19,14 @@
})
print(rt,flush=True)

def quit_nicely(signum, sigframe):
try:
dbt.delete(rt["_id"])
except Exception as e:
print("quit exception: "+str(e))
exit(143)
signal(SIGTERM, quit_nicely)

dbs=DBQuery(index="sensors",office=office,host=dbhost)
dba=DBQuery(index="algorithms",office=office,host=dbhost)
dbat=DBIngest(index="alerts",office=office,host=dbhost)
Expand Down
2 changes: 1 addition & 1 deletion volume/html/js/office.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ $("#pg-office").on(":initpage", function(e, queries, office) {
var line=$("<tr><td>"+v._source.name+"</td><td>"+v._id+"</td><td>"+source+"</td><td>"+v._source.status+"</td><td>"+latency+"</td><td>"+performance+"</td><td>"+v._source.skip+" frame(s)</td></tr>");
tbody.append(line);
line.find("a").click(function () {
selectPage("recording",['sensor="'+v._source.source+'"']);
selectPage("recording",['sensor="'+v._source.source+'"',office]);
});
});

Expand Down
2 changes: 1 addition & 1 deletion web/cloud/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

####
Expand Down
15 changes: 14 additions & 1 deletion web/cloud/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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()
2 changes: 1 addition & 1 deletion web/local/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

####
Expand Down
14 changes: 13 additions & 1 deletion web/local/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()

0 comments on commit 5b7cdff

Please sign in to comment.