Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

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

16 files changed

Lines changed: 87 additions & 18 deletions

File tree

analytics/feeder/main.py

Lines changed: 13 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 9 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)