Skip to content

Commit 5b7cdff

Browse files
authored
Merge pull request #19 from xwu2git/master
handle SIGTERM to gracefully exit; fix office->recording link;
2 parents 97bf32d + 9601de1 commit 5b7cdff

File tree

16 files changed

+87
-18
lines changed

16 files changed

+87
-18
lines changed

analytics/feeder/main.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from db_ingest import DBIngest
44
from db_query import DBQuery
5+
from signal import signal, SIGTERM
56

67
from watchdog.observers import Observer
78
from watchdog.events import FileSystemEventHandler
@@ -14,10 +15,7 @@
1415
from threading import Timer
1516
from probe import probe, run
1617
import time
17-
import datetime
1818
import os
19-
import sys
20-
import signal
2119
import tempfile
2220
import shutil
2321
import re
@@ -114,12 +112,13 @@ def stop(self):
114112
logger.info(" ### Stopping Feeder ### ")
115113

116114
self._threadflag = False
117-
self.mqttclient.loop_stop()
118-
self.observer.stop()
119115

120116
logger.debug("Unregistering algorithm from DB")
121117
self.db_alg.delete(self.alg_id)
122118

119+
self.mqttclient.loop_stop()
120+
self.observer.stop()
121+
123122
def startmqtt(self):
124123
self.mqttclient = mqtt.Client("feeder_" + self.alg_id)
125124
self.mqttclient.connect(self.mqtthost)
@@ -345,6 +344,15 @@ def ingest(self):
345344

346345
if __name__ == '__main__':
347346
smtc_feeder = Feeder()
347+
348+
def quit_nicely(signum, sigframe):
349+
try:
350+
smtc_feeder.stop()
351+
except Exception as e:
352+
print("quit exception"+str(e))
353+
exit(143)
354+
signal(SIGTERM, quit_nicely)
355+
348356
try:
349357
smtc_feeder.start()
350358
except KeyboardInterrupt:

maintenance/cleanup/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ FROM centos:7.6.1810
44
RUN yum -y install epel-release && yum -y install python36-requests python36-ply && rm -rf /var/cache/yum/*
55

66
COPY *.py /home/
7-
CMD ["/bin/bash","-c","/home/main.py"]
7+
CMD ["/home/main.py"]
88

99
####
1010
ARG USER

maintenance/cleanup/main.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python3
22

33
from db_query import DBQuery
4+
from signal import signal, SIGTERM
45
import time
56
import os
67

@@ -10,13 +11,19 @@
1011
office=list(map(float, os.environ["OFFICE"].split(",")))
1112
dbhost=os.environ["DBHOST"]
1213

13-
while True:
14+
def quit_nicely(signum, sigframe):
15+
exit(143)
16+
17+
# set signal to quit nicely
18+
signal(SIGTERM, quit_nicely)
19+
20+
while True:
1421
time.sleep(service_interval)
1522

1623
print("Searching...",flush=True)
1724
for index in indexes:
1825
db=DBQuery(index=index,office=office,host=dbhost)
19-
for r in db.search("time<now-"+str(retention_time*1000)):
26+
for r in db.search("time<now-"+str(retention_time*1000), size=500):
2027
# delete the record
2128
db.delete(r["_id"])
2229

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

maintenance/where_indexing/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ FROM centos:7.6.1810
44
RUN yum -y install epel-release && yum -y install python36-requests python36-ply && rm -rf /var/cache/yum/*
55

66
COPY *.py /home/
7-
CMD ["/bin/bash","-c","/home/main.py"]
7+
CMD ["/home/main.py"]
88

99
####
1010
ARG USER

maintenance/where_indexing/main.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python3
22

33
from db_query import DBQuery
4+
from signal import signal, SIGTERM
45
import os
56
import time
67

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

16+
def quit_nicely(signum, sigframe):
17+
exit(143)
18+
19+
# set signal to quit nicely
20+
signal(SIGTERM, quit_nicely)
21+
1522
dbq=DBQuery(index=indexes[0],office=office,host=dbhost)
1623
dba=DBQuery(index=indexes[1],office=office,host=dbhost)
1724
while True:

sensor/onvif/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ RUN wget ${ZEEP_REPO} && \
2626
rm v${ONVIF_ZEEP_VER}.tar.gz
2727

2828
COPY *.py /home/
29-
CMD ["/bin/bash","-c","/home/main.py"]
29+
CMD ["/home/main.py"]
3030
WORKDIR /home
3131
EXPOSE 80
3232

sensor/onvif/main.py

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import subprocess
99
from onvif import ONVIFCamera, ONVIFError
1010
import xml.etree.ElementTree as ET
11+
from signal import SIGTERM, signal
1112

1213
from db_query import DBQuery
1314
from db_ingest import DBIngest
@@ -317,4 +318,8 @@ def discover_all_onvif_cameras():
317318
time.sleep(60)
318319

319320
if __name__ == "__main__":
321+
def quit_nicely(signum, sigframe):
322+
exit(143)
323+
signal(SIGTERM, quit_nicely)
324+
320325
discover_all_onvif_cameras()

sensor/simulation/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
FROM ubuntu:18.04
33
RUN apt-get update && apt-get install -y -q python3-requests vlc && rm -rf /var/lib/apt/lists/*
44
COPY *.py /home/
5-
ENTRYPOINT ["/bin/bash","-c","/home/main.py"]
5+
CMD ["/home/main.py"]
66

77
####
88
ARG USER

sensor/simulation/main.py

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python3
22

33
from db_ingest import DBIngest
4+
from signal import signal, SIGTERM
45
import subprocess
56
import socket
67
import math
@@ -61,6 +62,14 @@ def geo_point(origin, distance, tc):
6162
"status": "idle",
6263
})
6364

65+
def quit_nicely(signum, sigframe):
66+
try:
67+
db.delete(r["_id"])
68+
except Exception as e:
69+
print("quit exception: "+str(e))
70+
exit(143)
71+
signal(SIGTERM, quit_nicely)
72+
6473
# run rtspatt
6574
while True:
6675
simulated_root="/mnt/simulated"

trigger/health/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ FROM centos:7.6.1810
33

44
RUN yum install -y epel-release && yum install -y python36-requests python36-ply && rm -rf /var/cache/yum/*
55
COPY *.py /home/
6-
CMD ["/bin/bash","-c","/home/main.py"]
6+
CMD ["/home/main.py"]
77

88
####
99
ARG USER

trigger/health/main.py

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from db_ingest import DBIngest
44
from db_query import DBQuery
5+
from signal import SIGTERM, signal
56
import datetime
67
import time
78
import os
@@ -18,6 +19,14 @@
1819
})
1920
print(rt,flush=True)
2021

22+
def quit_nicely(signum, sigframe):
23+
try:
24+
dbt.delete(rt["_id"])
25+
except Exception as e:
26+
print("quit exception: "+str(e))
27+
exit(143)
28+
signal(SIGTERM, quit_nicely)
29+
2130
dbs=DBQuery(index="sensors",office=office,host=dbhost)
2231
dba=DBQuery(index="algorithms",office=office,host=dbhost)
2332
dbat=DBIngest(index="alerts",office=office,host=dbhost)

volume/html/js/office.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ $("#pg-office").on(":initpage", function(e, queries, office) {
2929
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>");
3030
tbody.append(line);
3131
line.find("a").click(function () {
32-
selectPage("recording",['sensor="'+v._source.source+'"']);
32+
selectPage("recording",['sensor="'+v._source.source+'"',office]);
3333
});
3434
});
3535

web/cloud/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ RUN yum -y install epel-release && yum -y install nginx python36-tornado python3
55

66
COPY *.conf /etc/nginx/
77
COPY *.py /home/
8-
CMD ["/bin/bash","-c","/home/main.py&/usr/sbin/nginx"]
8+
CMD ["/home/main.py"]
99
EXPOSE 8080
1010

1111
####

web/cloud/main.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from hint import HintHandler
77
from redirect import RedirectHandler
88
from stats import StatsHandler
9+
from subprocess import Popen
10+
from signal import signal, SIGTERM, SIGQUIT
911

1012
app = web.Application([
1113
(r'/api/search',SearchHandler),
@@ -21,4 +23,15 @@
2123
parse_command_line()
2224
print("Listening to " + options.ip + ":" + str(options.port))
2325
app.listen(options.port, address=options.ip)
24-
ioloop.IOLoop.instance().start()
26+
27+
tornado1=ioloop.IOLoop.instance();
28+
nginx1=Popen(["/usr/sbin/nginx"])
29+
30+
# set SIGTERM/SIGQUIT handler
31+
def quit_nicely(signum, frame):
32+
nginx1.send_signal(SIGQUIT)
33+
tornado1.add_callback(tornado1.stop)
34+
35+
signal(SIGTERM, quit_nicely)
36+
tornado1.start()
37+
nginx1.wait()

web/local/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ RUN yum -y install epel-release && yum -y install nginx python36-tornado python3
55

66
COPY *.conf /etc/nginx/
77
COPY *.py /home/
8-
CMD ["/bin/bash","-c","/usr/sbin/nginx&/home/main.py"]
8+
CMD ["/home/main.py"]
99
EXPOSE 8080
1010

1111
####

web/local/main.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from tornado.options import define, options, parse_command_line
55
from workload import WorkloadHandler
66
from db_ingest import DBIngest
7+
from subprocess import Popen
8+
from signal import signal, SIGTERM, SIGQUIT
79
import socket
810
import os
911

@@ -31,4 +33,14 @@
3133
print("Listening to " + options.ip + ":" + str(options.port))
3234

3335
app.listen(options.port, address=options.ip)
34-
ioloop.IOLoop.instance().start()
36+
tornado1=ioloop.IOLoop.instance();
37+
nginx1=Popen(["/usr/sbin/nginx"])
38+
39+
def quit_nicely(signum, frame):
40+
tornado1.add_callback(tornado1.stop)
41+
nginx1.send_signal(SIGQUIT)
42+
db.delete(r["_id"])
43+
44+
signal(SIGTERM, quit_nicely)
45+
tornado1.start()
46+
nginx1.wait()

0 commit comments

Comments
 (0)